Ask Your Question

Negative Values When Getting Pixels in Java API

asked 2013-03-04 21:31:56 -0600

tuva gravatar image

The pixel values I'm getting are negative.

Mat src = Highgui.imread("image.png", Highgui.CV_LOAD_IMAGE_COLOR);
byte[] onePixelData = new byte[3]; 
src.get(x, y, onePixelData);
System.out.println(Arrays.toString(onePixelData)); // outputs [-5, -102, 5]

Am I doing something wrong or is it because Java doesn't support unsigned data types? What's the best way to fix this?

edit retag flag offensive close merge delete


You can fix this by doing an "& 0xFF" with each entry.

tuva gravatar imagetuva ( 2013-06-02 21:23:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-03-04 21:37:27 -0600

awknaust gravatar image

You are correct, its because java bytes are thought of as signed, whereas in the image they are unsigned, in [0, 255]

edit flag offensive delete link more

Question Tools


Asked: 2013-03-04 21:31:56 -0600

Seen: 1,679 times

Last updated: Mar 04 '13