Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cvtColor vs reading image directly

I have the following:

string input = "test.jpg";

cv::Mat tmp = cv::imread(input, CV_LOAD_IMAGE_COLOR);
cv::Mat img1;
cv::cvtColor(tmp, img1, CV_BGR2GRAY);

cv::Mat img2 = cv::imread(input, cv::IMREAD_GRAYSCALE); 

isMatEqual(img1, img2);

where isMatEqual():

bool isMatEqual(const cv::Mat & a, const cv::Mat & b)
{
    if ( (a.rows != b.rows) || (a.cols != b.cols) )
        return false;
    cv::Scalar s = sum( a - b );
    return (s[0]==0) && (s[1]==0) && (s[2]==0);
}

However I am not getting an equality when comparing the 2 images. If I am forced to read the image in first and then do a cvtColor() transform to grayscale, what would be a correct parameter to set such that img1 == img2?

cvtColor vs reading image directly

I have the following:

string input = "test.jpg";

cv::Mat tmp = cv::imread(input, CV_LOAD_IMAGE_COLOR);
cv::Mat img1;
cv::cvtColor(tmp, img1, CV_BGR2GRAY);

cv::Mat img2 = cv::imread(input, cv::IMREAD_GRAYSCALE); 

isMatEqual(img1, img2);

where isMatEqual():

bool isMatEqual(const cv::Mat & a, const cv::Mat & b)
{
    if ( (a.rows != b.rows) || (a.cols != b.cols) )
        return false;
    cv::Scalar s = sum( a - b );
    return (s[0]==0) && (s[1]==0) && (s[2]==0);
}

However I am not getting an equality when comparing the 2 images. If I am forced to read the image in first and then do a cvtColor() transform to grayscale, what would be a correct parameter to set such that img1 == img2?

Edit: I saw a similar question: https://stackoverflow.com/questions/7461075/opencv-image-conversion-from-rgb-to-grayscale-using-imread-giving-poor-results but I am unable to reproduce the answer given with my input.