Ask Your Question

How to create a binary image mat? [closed]

asked 2012-11-21 15:19:22 -0500

flohei gravatar image

Whats the right way to create a binary image?

I'm trying to convert an ordinary image mat to grayscale and apply a threshold afterwards like this:

// first convert the image to grayscale
cvtColor(imageMat, grayscaleMat, CV_RGB2GRAY);

// then adjust the threshold to actually make it binary
threshold(grayscaleMat, binaryMat, 100, 255, CV_THRESH_BINARY);

Shouldn't that create a mat that does only have 0s and 255s (as uchars) in it? At least that's how I understand it. Unfortunately, its not only 0s and 255s.

What am I doing wrong?

Thanks a lot!

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2017-08-30 16:32:02.406824

2 answers

Sort by » oldest newest most voted

answered 2012-11-21 20:11:39 -0500

This piece of code works for me:

#include <iostream>
#include <opencv2/opencv.hpp>

int main (int argc, char **argv)

    if (argc != 2)
            std::cout << "USE: " << argv[0] << " <file_path>" << std::endl;
            return 1;

    //Our color image
    cv::Mat imageMat = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
    if (imageMat.empty())
            std::cerr << "ERROR: Could not read image " << argv[1] << std::endl;
            return 1;

    //Grayscale matrix
    cv::Mat grayscaleMat (imageMat.size(), CV_8U);

    //Convert BGR to Gray
    cv::cvtColor( imageMat, grayscaleMat, CV_BGR2GRAY );

    //Binary image
    cv::Mat binaryMat(grayscaleMat.size(), grayscaleMat.type());

    //Apply thresholding
    cv::threshold(grayscaleMat, binaryMat, 100, 255, cv::THRESH_BINARY);

    //Show the results
    cv::namedWindow("Output", cv::WINDOW_AUTOSIZE);
    cv::imshow("Output", binaryMat);


    return 0;

I hope this helps

edit flag offensive delete link more


I'm using this with the Cocoa framework and I'm getting the image mat (or the grayscale mat, tried both) from an UIImage instance. Besides that, I'm applying the threshold like you do. When I print every single pixel's value to the console (as uchars) I'm not only getting 0s and 255s. I got that right, that I should only have two different values in binary images, right? I'm starting to question my understanding of the matter… ;)

flohei gravatar imageflohei ( 2012-11-22 18:24:55 -0500 )edit

hehehe You got it right! inside binaryMat there should be ONLY 0s and 255s. Sorry, I have no experience with Cocoa, so I can not be more helpful. But it smells like either some pointer going nuts or a bug in cv::threshold (but I doubt this last one). Good luck with it!

Martin Peris gravatar imageMartin Peris ( 2012-11-22 20:44:22 -0500 )edit

answered 2012-11-21 15:44:58 -0500

Simone gravatar image

Try to use


instead of CV_THRESH_BINARY.


edit flag offensive delete link more


Unfortunately, that does not seem to change anything.

flohei gravatar imageflohei ( 2012-11-21 16:37:51 -0500 )edit

I don't know what else could be. What other values did u get? Can u somehow compare them with the original ones in the grayscaleMat?

Simone gravatar imageSimone ( 2012-11-21 17:11:47 -0500 )edit

I have values like 2, 91, 116, 55, 183, 119, 175, 210, … in there. When I parse the binary mat I do not actually have the grayscale mat anymore. But I think I could keep it to compare it side by side, once it's not in the middle of the night here… ;) Thanks for your assistance anyway!

flohei gravatar imageflohei ( 2012-11-21 17:53:48 -0500 )edit

Question Tools



Asked: 2012-11-21 15:19:22 -0500

Seen: 49,305 times

Last updated: Nov 21 '12