Binarization and grayscale of images

Source: Internet
Author: User


The binarization of an image sets the gray value of the pixel on the image to 0 or 255, that is, the entire image shows a significant black and white effect.

Use appropriate thresholds to select 256 gray-scale images with brightness levels to obtain a binarization image that still reflects the overall and local features of the image. Binary Images play a very important role in digital image processing. First, binarization is conducive to further image processing, making the image simple and reducing the amount of data, it can highlight the outlines of objects of interest. Secondly, we need to process and analyze binary images. First, we need to binarization gray images to obtain binary images.

All pixels whose gray scale is greater than or equal to the threshold value are determined to belong to a specific object. The gray scale value is 255. Otherwise, these pixels are excluded from the object area and the gray scale value is 0, indicates the background or the area of the exception object.

The following procedure can achieve image binarization:

/*************************************** * Function Name: * cvbinaryex * parameter: * imgsrc-image for sharpening * returned value: * true is returned for successful sharpening; otherwise, false is returned. * description: ** binarization of an image ******************************** * ***/bool cvbinaryex (iplimage * imgsrc) {iplimage * IMG = cvcreateimage (cvgetsize (imgsrc), imgsrc-> depth, imgsrc-> nchannels); cvscalar s; int sum = 0; For (INT I = 0; I  height; I ++) {for (Int J = 0; j  width; j ++) {S = cvget2d (imgsrc, I, j); sum = (S. val [0] + S. val [1] + S. val [2])/3; if (sum> 128) {S. val [0] = S. val [1] = S. val [2] = 255; cvset2d (imgsrc, I, j, S);} else {S. val [0] = S. val [1] = S. val [2] = 0; cvset2d (imgsrc, I, j, S) ;}} return true ;}

Grayscale grayscale

Grayscale is an image that only contains brightness information and color information. A black-and-white image is a grayscale image. It is characterized by a continuous change in brightness from dark to bright. To represent a grayscale image, you need to quantify the brightness value.

Benefits of using Grayscale Images:

① The values of RGB are the same.

② Image data is the color palette index value, which is the actual RGB value, that is, the brightness value.

③ Because it is a 256-color palette, one byte in the image data represents a pixel, Which is neat.

Therefore, grayscale images are used for image processing.

Convert an RGB image to a grayscale image:

IplImage *src= cvLoadImage("C:\\home.bmp", -1);IplImage *dest ;cvCvtColor(Src, dest, CV_RGB2GRAY);


Then convert the Dest Of The grayscale image to a binarization image (stored in SRC). You can directly use the cvthreshold function:

cvThreshold(dest, src, 1, 255, CV_THRESH_BINARY); 

Generally, the image is dimmed first, then binarization, and then edge processing is performed...

Grayscale --> binarization --> edge extraction>

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.