We have always used the cvthreshold function to convert a set of images into a black-and-white profile after binarization. However, this method can be used only when the gray-scale values of images are equal and modular, instead of being separated by different gray-scale values of the first line, it is like a zebra crossing. The general solution is:
- Filter images based on the situation. As for the filtering method and number of times, you should debug the images based on your own wishes and actual conditions;
- Use the Sobel operator to perform X and Y derivation, and debug the number of times based on your own;
- Close or open operations are used to handle the issue, so that no key points need to be concentrated together;
- Set the ROI Area for expansion or corrosion. Remove unnecessary points.
Below is a code:
// Extract image foreground. cpp: defines the entry point of the console application. /// * ============================================ ==================================================== Name: image binarization contour time: 2013.08 Description: binarization a set of images with different brightness, black and white outlines ==================================================== ========================================= */# include" stdafx. H "# include" CV. H "# include" highgui. H "# include" cxcore. H "int _ tmain (INT argc, _ tchar * argv []) {// declare several image pointers, which are used to load the local image and the processed image iplimage * SRC, * dst1, * dst2, * dst3; src = cvloadimage (". /1.jpg", 0); // load the dst1 = cvcreateimage (cvgetsize (SRC), ipl_depth_16s, 1); dst2 = cvcreateimage (cvgetsize (SRC), ipl_depth_8u, 1 ); dst3 = cvloadimage (". /1.jpg", 0); // perform Median Filter for (INT I = 0; I <2; I ++) {cvsmooth (SRC, SRC, 1, 3, 0, 0, 0);} // use the Sobel operator to evaluate cvsobel (SRC, dst1, 1, 0, 3); cvconvertscaleabs (dst1, dst2, 1.8, 0 ); // use linear transformation to convert the input array elements into eight-bit unsigned integer cvthreshold (dst2, dst2, 5,255, cv_thresh_binary); // binarization threshold value cvsmooth (dst2, dst2, cv_median, 3, 0, 0, 0); // median filter // perform a closed operation to stitch the split to cvdilate (dst2, dst2, null, 2 ); cverode (dst2, dst2, null, 4 ); // ================================================ ============================================/// use the settings that do not require black spots to be ROI, use the expansion function to remove unnecessary black spots. ==================================== cvsetimageroi (SRC, cvrect (280,210, 20, 20); cvdilate (SRC, SRC, null, 10); cvresetimageroi (SRC); cvsetimageroi (dst2, cvrect (280,210, 20, 20 )); cvdilate (dst2, dst2, null, 10); cvresetimageroi (dst2); cvsetimageroi (dst2, cvrect (290,170, 30, 30); cvdilate (dst2, dst2, null, 10); cvresetimageroi (dst2); cvsetimageroi (dst2, cvrect (80,200, 30, 30); cvdilate (dst2, dst2, null, 10); cvresetimageroi (dst2 ); cvsetimageroi (dst2, cvrect (160,180, 30, 30); cvdilate (dst2, dst2, null, 10); cvresetimageroi (dst2, cvsetimageroi (100,130, 50,50); cvdilate (dst2, dst2, null, 10); average (dst2); cvsetimageroi (dst2, cvrect (210,150, 55,55); cvdilate (dst2, dst2, null, 10); cvresetimageroi (dst2); // declare and display the original image and the processed image window cvnamedwindow ("dst2"); cvnamedwindow ("dst3"); cvmovewindow ("dst2 ", 460, 60); cvmovewindow ("dst3", 60, 60); // cvshowimage ("dst3", dst3); cvshowimage ("dst2", dst2 ); cvsaveimage ("2.jpg", dst2); cvsaveimage (" 3.jpg", dst3); cvwaitkey (0); // destroy all windows and release the memory occupied by the image cvdestroyallwindows (); cvreleaseimage (& SRC); cvreleaseimage (& dst1); cvreleaseimage (& dst2); cvreleaseimage (& dst3); Return 0 ;}
The original images, gray images, and black and white outlines are as follows: