简体   繁体   中英

RGB colors in java Vs VB.net

I'm no native English speaker, so please excuses any translation errors.

I'm not really having a coding problem. It's more of a conceptual question.

I wrote two times the same piece of code translating an image into a list of RGB values. (1 combination of 3 values for each pixel).

I wrote the code first in VB.net using:

Dim bmp As New Bitmap(File)
For x As Integer = 0 To w - 1 
    For y As Integer = 0 To h - 1
        Dim c As Color = bmp.GetPixel(x, y)
        Dim Red as integer = c.R
        Dim Green as integer = c.G
        Dim Blue as integer = c.B
    Next y
next x

Afterwards I wrote the following in Java:

BufferedImage image = ImageIO.read(new File(File))
for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
        int pixel = image.getRGB(i,j);
        int Red = ((pixel >> 16) & 0xff);
        int Green = ((pixel >> 8) & 0xff);
        int Blue = ((pixel) & 0xff);
    }
}

My expectation would be to get the same values from both pieces of code, since they use the same image. I tried it on an image (270x320) which was a photograph (so a full spectrum of colors). To my surprise I saw there where small differences in the RGB values between the VB.net and Java codes.

If I compare the java(red)'s versus the VB.net(red)'s, the java(green)'s versus the VB.net(green)'s and the java(blue)'s versus the VB.net(blue)'s I compare 270x320x3 = 259.2k combinations. The differences between the integers gotten from the VB.net and from the Java code are as followed:

  • No difference: 250178 (96.5%)
  • One difference: 7426 (2.9%)
  • Two difference: 1582 (0.6%)
  • Three difference: 14 (0.0%)
  • Four or more diff.: 0 (0.0%)

Can anybody explain to me where this difference comes from? Has it to do with the way of reading the colors, the way of buffering the image, or with something like anti-aliasing?

Really curious what the reason is, thx in advance

As mentioned by others, the difference is caused by JPEG's lossy compression.

You should be testing these methods with a lossless format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM