[OpenCV Getting Started Guide] Article 4 binarization of images

Source: Internet
Author: User
[OpenCV Getting Started Guide] Article 4 binarization of images

In the last article [OpenCV Getting Started Guide], the third part of the content of the paper, the content of the content. Compared with edge detection, contour detection sometimes better reflects the image content. To detect the contour of an image, you must first binarization the image. The binarization of the image is to set the gray value of the pixel on the image to 0 or 255, this will make the entire image show a clear black and white effect. In digital image processing, binary images play a very important role. The binarization of images greatly reduces the amount of data in the image, thus highlighting the outline of the target.

OpenCV Getting Started Guide series Article address: http://blog.csdn.net/morewindows/article/category/1291764

1. Key functions

The following describes the key function cvThreshold () for binarization of images in OpenCV ().

Function: Performs Edge Detection on the Image Using the Canny method.

Function prototype:

Void cvThreshold (

Const CvArr * src,

CvArr * dst,

Double threshold,

Double max_value,

Int threshold_type

);

Function Description:

The first parameter indicates the input image, which must be a single channel grayscale image.

The second parameter indicates the output edge image, which is a single-channel black-and-white image.

The third parameter indicates the threshold.

The fourth parameter indicates the maximum value.

The fifth parameter represents the calculation method.

You can find the definition of the calculation method in imgproc \ types_c.h of OpenCV.

/* Threshold types */

Enum

{

CV_THRESH_BINARY = 0,/* value = value> threshold? Max_value: 0 */

CV_THRESH_BINARY_INV = 1,/* value = value> threshold? 0: max_value */

CV_THRESH_TRUNC = 2,/* value = value> threshold? Threshold: value */

CV_THRESH_TOZERO = 3,/* value = value> threshold? Value: 0 */

CV_THRESH_TOZERO_INV = 4,/* value = value> threshold? 0: value */

CV_THRESH_MASK = 7,

CV_THRESH_OTSU = 8/* use Otsu algorithm to choose the optimal threshold value; combine the flag with one of the above CV_THRESH _ * values */

};

Annotations are clearly written, so they are no longer expressed in Chinese.

 

Ii. Sample Code

The following shows the complete source code for binarization of the image:

// Image binarization // By MoreWindows (http://blog.csdn.net/MoreWindows) # include <opencv2/opencv. hpp> using namespace std; # pragma comment (linker, "/subsystem: \" windows \ "/entry: \" mainCRTStartup \ "") IplImage * g_pGrayImage = NULL; iplImage * g_pBinaryImage = NULL; const char * pstrWindowsBinaryTitle = "Binary Graph (http://blog.csdn.net/MoreWindows)"; void on_trackbar (int pos) {// convert to Binary Graph cvThreshold (g_pGrayImage, g_pBinaryImage, pos, 255, CV_THRESH_BINARY); // cvShowImage (pstrWindowsBinaryTitle, g_pBinaryImage);} int main (int argc, char ** argv) {const char * pstrWindowsSrcTitle = "source image (http://blog.csdn.net/MoreWindows)"; const char * pstrWindowsToolBarName = "Binary Graph threshold "; // load the source image IplImage * pSrcImage = cvLoadImage ("002.jpg", CV_LOAD_IMAGE_UNCHANGED) from the file; // convert it to a grayscale image g_pGrayImage = cvCreateImage (cvGetSize (pSrcImage), watermark, 1 ); cvCvtColor (pSrcImage, g_pGrayImage, rows); // create a two-value graph g_pBinaryImage = cvCreateImage (cvGetSize (g_pGrayImage), rows, 1); // display the original cvNamedWindow (rows, CV_WINDOW_AUTOSIZE ); cvShowImage (sequence, pSrcImage); // create a two-value graph window cvNamedWindow (sequence, CV_WINDOW_AUTOSIZE); // sliding int nThreshold = 0; cvCreateTrackbar (sequence, sequence, & nThreshold, 254, on_trackbar); on_trackbar (1); cvWaitKey (0); cvDestroyWindow (rows); cvDestroyWindow (pstrWindowsBinaryTitle); cvReleaseImage (& pSrcImage); cvReleaseImage (& g_pGrayImage ); cvReleaseImage (& g_pBinaryImage); return 0 ;}

The running result is as follows. You can download the source file and Program (Release version, which can be used without installing OpenCV), debug the threshold value on your own, and see if the generated Binary Map has changed.

 

OpenCV also has a cvAdaptiveThreshold () function, which uses the Otsu algorithm (greater law or the maximum inter-class variance method) (Note 1) to calculate a global threshold, then perform binarization based on this threshold. Of course, you can directly use the cvkan () function in the last article "[OpenCV Getting Started Guide] The third article" Canny edge detection "to binarization the image (How do you think of passing parameters ?).

 

Now, the binarization of the image is introduced here, welcome to the following two articles: [OpenCV Getting Started Guide] Chapter 5 contour detection and [OpenCV Getting Started Guide] section 6 contour detection. Thank you.

 

 

Note 1. When cvThreshold () is called, The CV_THRESH_OTSU parameter is used to automatically generate a threshold value.

 

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8239678

OpenCV Getting Started Guide series Article address: http://blog.csdn.net/morewindows/article/category/1291764

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.