4.2 Preprocess

4.2.1 Contours Finding & Process

The first and most significant part is to find the contours of a shape from an image. Certainly, people cannot make sure that they get 100% success for each detection. Thus, the final process result depends on the contours to a great content of quality of preprocess. The pre-treatment could be classified into three stages, image blurring, contours elicitation and contours process.

During preprocess stage, I want to get a better version of the original image. Normally, first of all, I need to transfer the colorful image in RGB to gray scale format so that computer deal with easily. And then, the Gaussian blur should be applied to smooth the image and reduce the noise. This step is called image blurring.

cvtColor(mat, gray, CV_BGR2GRAY);
blur(gray, gray, Size(3,3));
}
Code Segment - A: Color Convert

The code segment A transfers the original image into the gray image, and blur this image as possible. In this case, I use a 3 x 3 kernel to reduce the noise from this image. This kernel is a small matrix called filter to traverse all image.

The followed picture 4.2.2 and 4.2.3 show the comparison before and after preprocess when user click the ‘detect button’.

Figure 4.2.2: Image before preprocess with 3 color channels in (RGB) pattern
Figure 4.2.3: Gray image after preprocess – 1 Channel (white to black, 0-255)

After the first step of preprocess, I get the gray image of original image. And the next step is to find out the contour of each object in this image. In fact, this step includes two sub-processes, external contours elicitation and internal contours elicitation. The external contours (Canny) elicitation mainly refers to get all of the contours of all shapes. In other words, the result is a big vector of points.

Canny(gray, canny_out, canny_threshold, 90, 3);
Code Segment - B: Code segment of external contours elicitation

For this step, user can also adjust the threshold of process to get different result.

Original image Contours Image Canny threshold = 5
Contours Image Canny threshold = 100 Contours Image Canny threshold = 250
Figure 4.2.5: Contours Image in different threshold

When user click the ‘Contour’ button, it will show the result as shown in picture 4.2.5. Besides, if the position of slide bar changes, the result would be changed respectively.

Now, I have finished the external contours elicitation step, and need to classify these external contours into internal contours further so that build the hierarchical relationship between these points. Thus, I apply internal contours elicitation. FindContours is another one contour elicitation technique as shown in Code Segment C.

findContours(m, temp_contours, hierachy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point())
Code Segment C: indContours (Internal Elicitation)

FindContours is also provided by OpenCV with higher efficiency. The difference between Canny process (external) and findContours (internal) mainly appears in the approach of process. As for canny process, it just find out a series of points from an image represented with a form of discrete. With regard to findContours, this approach not only find out these points, but also can build a hierarchical relationship (CV_CHAIN_APPROX_NONE) with these points.

After these two contours elicitation steps, a hierarchical contours structure is established, and temp_contours is a nested vector consisting of all independent set of points. In other words, all of discreet points is organized into several independent set of points.