Search code examples
matlabopencvjpegopencv3.0imread

OpenCV vs Matlab : Different Values on pixels with imread


I have encountered a problem with the function imread() in Matlab (2014) and OpenCV (3.0) on Windows 7 with jpg files.

I don't have the same values by reading the same file jpg and the same pixel.

Here are my 2 codes : (OpenCV code followed by the Matlab code) and the values I have (mode debug to see in OpenCV, keyboard in Matlab)

#include <opencv2\opencv.hpp>
#include <cstdio>

using namespace cv;
using namespace std;

int main()
{
     Mat img = imread("test.jpg");

     uchar pb = img.at<Vec3b>(0, 0).val[0];
     uchar pg = img.at<Vec3b>(0, 0).val[1];
     uchar pr = img.at<Vec3b>(0, 0).val[2];

     int d = img.depth();

     int t = img.type();
}

Values :

     pixel [0,0] = (147,174,204); // = index(1,1) in the image.
     d = 0;
     t = 16;

Code Matlab :

img = imread('test.jpg');

img(1,1,:)

whos img

Values :

ans(:,:,1) =
148

ans(:,:,2) =
174

ans(:,:,3) =
201

Name         Size                   Bytes  Class    Attributes
img       1920x2560x3            14745600  uint8     

Have you any idea why values are different?

I have seen on another post a problem like this but the person did not have the same depth by reading a tiff. Here as you can see I have the same depth !

Thank you in advance and sorry for any English mistake.

PS: I have test with other pixels too, same results : closed results but not exactly equals.


Solution

  • For people who would read this topic this is the final explanation:

    It comes from the version of libjpeg. The version 6b (OpenCV used this one before 2.4.11) works in the same way as Matlab 2014b. From version 8 of libjpeg, I had the other results I mentioned above.

    To solve my problem (I used some difference of image and background to create a mask and my problem was that I had some snow in the image with OpenCV (without libjeg version 6b), I compiled OpenCV 3.0 with libjpeg 6b. (I also had to import 2 runtime libraries and put it in my project, found freely on the web).

    I did not report bug on OpenCV. To be honest, I did not manage, I did not understand how to do in their website even I tried...