Ask Your Question

Revision history [back]

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);

    cv::waitKey(0);

    return 0;
}

I hope this helps