## 4 Real-Time Elbow and T-Junction Detection and Estimation (RT-ETDE) Framework
### Camera Pose and Straight Pipe Estimation Based on Linear-Time Point Sampling  
Camera pose and straight pipe estimation is an important preprocessing step in the RT-ETDE framework. Camera pose estimation in the straight pipe allows the transformation of the point cloud to SCS, which is necessary to remove the effect of the misalignment between the camera and the straight pipe. On the other hand, the straight pipe estimation provides the elliptic cylinder model of the pipe, which can be used to separate points on this pipe and points on the elbow and T-junction.  
This process uses a portion of the point cloud closer to the 3D ToF camera, reserving the remainder for ETDE, as illustrated in Figure 5a. We apply the camera pose and pipe diameter estimation scheme based on the elliptic cylinder fitting in [17]. The scheme estimates the 7-tuple \(\beta=(D_{\text{max}},D_{\text{min}},\theta_{x},\theta_{y},\theta_{z},d_{y},d_{z})\) where \(D_{\text{max}},D_{\text{min}}\) are the major and minor axes of the elliptic cylinder representing the straight pipe, and \(\theta_{x},\theta_{y},\theta_{z},d_{y},d_{z}\) are the camera pose in the SCS, consisting of roll, pitch, yaw, y-displacement, and z-displacement. This scheme finds the optimal pose that transforms the point cloud to SCS and fits to the elliptic cylinder represented by optimal \(D_{\text{max}}\) and \(D_{\text{min}}\).  
The estimation scheme in [17] uses the entire point cloud for fitting, which is unsuitable for real-time operation. To address this issue, we present a linear-time point sampling scheme. Preliminary analysis revealed that for the same number of points \(M\), the optimal point selection possesses two properties: (1) points are divided into two planes that are parallel to \(Y_{C}Z_{C}\)-plane and positioned farthest from each other, and (2) in each band, \(M/2\) points are evenly distributed across circumferential angles around the pipe cross-section. However, selecting points closest to a plane and evenly distributed over circumferential angles requires sorting, which cannot be achieved in linear time. To overcome this, we present a near-optimal linear-time point sampling scheme. We expand the plane into a band with thickness \(\delta_{B}\) along \(X_{C}\), as illustrated in Figure 5a. Any point on this band is considered to belong to the plane. Then, we divide the circumferential angle into cells and select a single point in each cell, as illustrated in Figure 5b. As the \(M\) increases, points in cells converge to an even distribution around the circumferential angles. Given the bands and cells, point sampling becomes the problem of selecting the points in \(M\) cells in two bands. To minimize the processing time, we choose the first available point in each cell during a single iteration through the point cloud. The iteration is terminated as soon as \(M\) points are selected, ensuring linear-time complexity.  
These sampled points are used for pose and straight pipe estimation using the scheme in [17] to find the camera pose and the straight pipe model. Based on the pose \(\theta_{x},\theta_{y},\theta_{z},d_{y},d_{z}\), each point \((x_{C},y_{C},z_{C})\) in the 3D ToF camera point cloud in the camera  
Figure 5: Point sampling for pose and straight pipe estimation. (**a**) Two bands of point cloud. (**b**) Selecting single point in each cell.  
coordinate system is first transformed into the SCS (\(x_{S},y_{S},z_{S}\)) and is ready for detection and estimation.