Ask Your Question

imread, imwrite does not give similar image

asked 2018-05-30 03:31:58 -0500

charles1208 gravatar image

I have the following code:

string src = "a.jpg";
string dest = "a_out.jpg";

cv::Mat im = cv::imread(src, CV_LOAD_IMAGE_COLOR);
cv::imwrite(dest, im);
cv::Mat im2 = cv::imread(dest, CV_LOAD_IMAGE_COLOR);


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 get a return that is false. Is there something wrong my reading/writing of the images?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-05-30 03:50:40 -0500

berak gravatar image

updated 2018-05-30 05:05:15 -0500

it's not your code.

.jpg is a "lossy" format, you'll never get back exactly, what you wrote.

if you need that kind of exactness, stick with lossless codecs, like .png or uncompressed images, like .bmp

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-05-30 03:31:58 -0500

Seen: 74 times

Last updated: May 30 '18