简体   繁体   中英

Why does my filtered image(should be binary) still contain colorful parts?(OpenCV in C++)

I do difference between two images and compare the result with a "standard" image. By using the command threshold , the pixels whose values are below 0 should be black while those with positive values be white. However, after the process the image still contain colorful pixels like red, yellow and so on.

threshold(Initial, Final, 0, 255, THRESH_BINARY);

I failed to figure out the origin of colorful pixels, anyone has some ideas? Thanks a lot.

Did you grayscale the image first? If you didn't, you will see all sorts of RGB colours in the image once you binarize it.

// C
IplImage *im_rgb  = cvLoadImage("image.jpg");
IplImage *im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
cvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);

// C++
Mat im_rgb  = imread("image.jpg");
Mat im_gray;
cvtColor(im_rgb,im_gray,CV_RGB2GRAY);

As far as I know it, you have to convert it into grayscale to binarize it to get just Black and White pixels.

According to http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#threshold

The function applies fixed-level thresholding to a single-channel array

so you need to convert your image to grayscale first to get an accurate conversion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM