Search code examples
javac++opencvjai

Java JAI BufferedImage versus C++ OpenCV Mat imread?


In Java:

I am reading an image using JAI:

 BufferedImage image = javax.imageio.ImageIO.read(new File("path to JPG image"));

Then, I look at the rgb value of the pixel (0,2):

System.out.println("pixel[0][2]="+(new Color(image.getRGB(2, 0))));

In C++ OpenCV:

Mat image = imread("path to the same JPG image");
image.convertTo(image, CV_32S);
cout <<" r value of pixel[0][2] "<< image.at<Vec3i>(0, 2)[2] << "\n";

The values are different: r value in Java is 156 and in C++ is 155. Why?


Solution

  • I think this has to do with the format of the image, not with Java or OpenCV. JPEG is lossy compression, so when decoding the data you may get different outputs for the same image. That will depend on the decoder you are using to read the image information. The issue you are experimenting is similar to the one described in the below question.

    Reading jpg file in OpenCV vs C# Bitmap