Ask Your Question

Mat type CV_8U but ByteBuffer values range from -128 to 127

asked 2019-06-13 03:08:36 -0600

tgpsantos gravatar image

updated 2019-06-13 03:39:42 -0600

Hello guys,

I have a frame which has its type = 24. According to this table this means the frame is CV_8U, thus its values should be around 0 and 255.

I wanted to confirm that in fact the values I'm getting are 0-255, so I did the following: byte[] buff= new byte[frame.cols() * frame.rows() * 4]; //nº total de valores (3 canais)


    Log.d("frame type:","frame_type:"+frame.type());

    for(int i=0; i<frame.cols(); i++) {
        for(int j=0; j<frame.cols(); j++) {
            if (buff[i]>max_pixel) {
                max_pixel=buff[i]; } 
            else if(buff[i]<min_pixel){
    Log.d("max_pixel", "max_pixel:"+max_pixel);
    Log.d("min_pixel", "min_pixel:"+min_pixel);

What I got from this was: (1) min_pixel = -128 (2) max_pixel = 127

Is this normal for a ByteBuffer? Because it seems like its type is a CV_8S instead

Thanks for the help!

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2019-06-13 03:23:33 -0600

berak gravatar image

updated 2019-06-13 03:30:59 -0600

it's not really an opencv problem, more a basic java one --

java's byte type is actually signed (-127..127) ! (and there is no unsigned char equivalent in java, so your array gets "silently" casted to signed char under the hood.

please try NOT to iterate over pixels over a whole image from java, it's quite an opencv - antipattern, slow & error prone.

if you absolutely HAVE to do arithmetics on that data, using manual loops (again, don't !), you need to convert EVERY SINGLE BYTE to uchar before your operation, and back to signed afterwards

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-06-13 03:08:36 -0600

Seen: 446 times

Last updated: Jun 13 '19