• Notice regarding image hosts and attachments. See here.

Images

Soldato
Joined
22 Oct 2005
Posts
2,912
Location
Moving...
I'm doing a java project to do with stereo vision. I'm having problems with regards to getting a colour value from a particular pixel in an image. I need to compare particular pixels between 2 images so to do this i have stored the pixels value in a 2D array using the line of code:

pixels[j] = bufferedImage.getRGB(i,j); (pixels is an integer 2d array)

But this stores a strange value which i dont understand: -5394273
This pixel's value in photoshop is:
R-82
G-60
B-73
# - 523c49 (it's a very dark purple).


What am I doing wrong? Is there another function i could use insetad of getRGB()? I can read the image in as a grayscale image in if that would make things easier.

Thanks for any advice.
 
I've thought about using getRed etc, but I cant seem to get i to work. This is my method at the moment, the commented out line is how i thought I could use it but it gives the error 'cannot find symbol getRed'.

Code:
//Create a 2Darray of pixels of the image
    public void getPixels() {
        
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        pixels = new int[width][height];
        
        for(int i=0; i<width; i++) {
            for(int j=0; j<height; j++) {
                pixelsLeft[i][j] = bufferedImage.getRGB(i,j);
                //pixelsLeft[i][j] = bufferedImage.getRed(i,j);
            }
        }
    }

I'm including the libraries:
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.lang.*;
So i think i've got the right libraries and such :confused:

Sorry for my noobiness but my lecturer is foreign and I cant understand what he says so I cant go to him for help :(
 
mpledge52 said:
I've thought about using getRed etc, but I cant seem to get i to work. This is my method at the moment, the commented out line is how i thought I could use it but it gives the error 'cannot find symbol getRed'.
You could try just shifting and masking to get the RGB components. Something like:

Code:
int v = bufferedImage.getRGB(i,j);
int b = v & 255;
int g = (v>>8)&255;
int r = (v>>16)&255;
Dunno if it will work as you want though.
 
DaveF said:
You could try just shifting and masking to get the RGB components. Something like:

Code:
int v = bufferedImage.getRGB(i,j);
int b = v & 255;
int g = (v>>8)&255;
int r = (v>>16)&255;
Dunno if it will work as you want though.

Am I right in thinking that if you have a grayscale image, the red, green and blue are the same? And i'm really sorry for all the questions, but what does v&255 do? I would search on the web myself but my internet is unbelievably slow and the only only thing that seems to work is OCUK. Nice work guys!!!
 
mpledge52 said:
Am I right in thinking that if you have a grayscale image, the red, green and blue are the same?
Unless you're doing something really wierd with the colour space, yes.

'&' = logical and. So v&255 is the least significant byte of v.
 
Thanks very much for your help Dave, it seems to bring up more sensible values now. However these values are still quite a way off from the colour values of the pixel in photoshop, is this normal? Surely they should be the same, if not very close?
 
With images, you'll see people talk about colour spaces. Loosely put, this is all about the concept that R,G,B is "just a set of numbers" that don't necessarily translate into colour intensities in the way you might think.

For example, with a typical CRT monitor (with no gamma correction), you will find that R=0.5 results in a pixel that emits much less than half the light of a pixel with R=1. Some pieces of software use quite extensive algorithms to try to deal with this.

I don't know enough about Photoshop colour spaces to know if that's the issue here (or indeed whether Java may do something similar), but it's a possibility. Or it may be there's something else going on. As long as the values seem to make sense, I wouldn't worry too much.

One check that might reassure you is to try setting the RGB values (in Java), and then reading them back using the code I suggested. If the values match, no problem. If they don't, then more investigation might be needed.
 
Back
Top Bottom