# Intelligent Control System to Irrigate Orchids Based on Visual Recognition and 3D Positioning
## 2 System Design and Methodology
### Construction of the 3D Coordinate System  
The binocular visual system is established for the 3D coordinate system in this work. The distribution principle is to retrieve images of an object from different positions based on the binocular (dual lens). As illustrated in Figure 4, the true distance Z, between the object and the lens, can be calculated as follows:  
\[Z=\frac{b*f}{X_{L}-X_{R}}=\frac{b*f}{d} \tag{1}\]  
where \(d=(X_{L}-X_{R})\) is the disparity, \(X_{L}\) and \(X_{R}\) are the \(x\)-coordinates on the left and right images, respectively, \(b\) is the length of the base line (distance between the optical axes of both cameras), and \(f\) is the focal length of the camera.  
The image coordinates (\(u\), \(v\)) can be reprojected to the world coordinates (X, Y, Z) through the 4 \(\times\) 4 reprojection matrix (\(Q\)) in order to obtain the actual 3D coordinates, which can be related according to Equation (2).  
\[Q\left[\begin{array}{c}u\\ v\\ d\\ 1\end{array}\right]=\left[\begin{array}{c}X\\ Y\\ Z\\ W\end{array}\right] \tag{2}\]  
where \(d\) is the disparity and \(W\) is the distance parameters of homogeneous coordinates.  
Figure 4: Binocular ranging principle [18].  
The most widely used algorithm to match the feature point is Semi-Globe Block Matching (SGBM) [19; 20; 21], which obtains the Sum of Absolute Differences Window (SAD) to calculate the cost [22], as well as takes the left-camera image as the reference and the right-camera image as the target to perform pixel feature matching on the same epipolar line. Finally, the obtained parallax is also utilized by the SGBM algorithm to carry out the stereo vision. In this work, the left- and right-camera images are processed in grayscale, and then the feature matching is performed through the SGBM algorithm.  
In the OpenCV function library [23], the function _cv2.StereoSGBM_create()_ performs the execution of the SGBM algorithm. There are no fixed values for the parameters of SGBM to output the grayscale depth map, which further requires _cv2.reprojectImageTo3D()_ to get the true 3D coordinates corresponding to the active pixel. After inputting the grayscale depth map and the reprojection matrix Q, the function _cv2.reprojectImageTo3D()_ can output a depth map, each pixel of which tells 3D coordinates in the workspace with respect to the origin, i.e., the optical center of the left lens in Figure 4.  
In order to make it convenient to observe the outputted depth map, the original noisy gray-scale depth map is converted into the pseudo-color depth map, which reduces the noises using the weighted least square (WLS) filter. It is a kind of edge-preserving filter that can smooth the whole image at the same time. OpenCV provides the _cv2.applyColormap()_ function to convert gray-scale graphs to 12 color maps. Here, _COLORMAP_JET_ mode is selected for the conversion process. On this mode, Figure 5 displays a pseudo-color map, where pixels with deeper red and deeper blue colors hold higher and lower gray values, respectively, in the original grayscale graph. Moreover, at the same pixel, the deeper red the color is, the closer the physical point is from the optical center of the left lens in Figure 4.