Adaptive threshold segmentation of OPENCV: OTSU method and TRIANGLE method

Adaptive threshold segmentation of OPENCV: OTSU method and TRIANGLE method

In the process of using opencv to do machine vision projects, adaptive threshold segmentation was used. At first, a picture with a dark background and a bright target (as shown in the left picture below) was used for adaptive threshold segmentation. Later, a background was used for Bright and dark pictures (as shown in the right picture below) are segmented by adaptive threshold.

                          

Using the same adaptive threshold segmentation method for these two images, but different segmentation effects are obtained:

1, OTSU method

The OTSU law is called the Great Law. There are many summaries on the Internet for its definition and realization process. You can check it out if you are interested. The following shows the results of using the OTSU method to segment two images:

Code:

	Mat img_1 = imread("C:\\Users\\SUNSONG\\Desktop\\图片\\Image_2.BMP");
	Mat img_2 = imread("C:\\Users\\SUNSONG\\Desktop\\图片\\Image_13.BMP");
	Mat gray_img1, thresh_img1,gray_img2, thresh_img2;
	cvtColor(img_1, gray_img1, COLOR_BGR2GRAY);
	cvtColor(img_2, gray_img2, COLOR_BGR2GRAY);
	threshold(gray_img1, thresh_img1, 0, 255, THRESH_OTSU);
	threshold(gray_img2, thresh_img2, 0, 255, THRESH_OTSU);
	imshow("测试图1", thresh_img1);
	imshow("测试图2", thresh_img2);

result:

Note: OTSU method can be used with other binarization, such as threshold(img, 0, 255, THRESH_OTSU + THRESH_BINARY), which means that the threshold is calculated by the OTSU algorithm and then THRESH_BINARYsegmented according to the rules.

2、TRIANGLE法

The TRIANGLE method is called the triangulation method. For specific explanation, please refer to this article: Triangulation of Image Processing and Image Binarization

Code:

The implementation code is as shown in the OTSU method, except that the threshold segmentation method is changed from THRESH_OTSU to THRESH_TRIANGLE.

result:

3. Summary

To extract bright targets in the background, the TRIANGLE method is better than the OTSU method, while extracting dark targets in the bright background, the OTSU method is better than the TRIANGLE method. How to use it depends on the specific usage.

Since I have just come into contact with OPENCV, I still don't understand it thoroughly, so I can only make the above-mentioned simple summary.

Guess you like

Origin blog.csdn.net/Kevin_Sun777/article/details/111193589