Ask Your Question
0

How do I get 0-255 RGB values from an RGBA mat?

asked 2020-02-27 09:06:05 -0600

I'm using OpenCv in android studio and I want to get the 0-255 RGB value of a pixel from inputFrame, then draw the pixel bigger (filling a rectangle with the same color) using Imgproc.

Here is my code (Java):

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat mRgba = inputFrame.rgba(); //convert camera image to mat

    int pixelSize = 5;
    int pixelNum = 50;
    int sourceX = 300; //where to get RGB values from
    int sourceY = 300;

    Mat dataMat = new Mat();
    Imgproc.cvtColor(mRgba,dataMat,Imgproc.COLOR_RGBA2RGB); //convert rgba mat to rgb

    //supposed to draw large pixels the same color as the source pixels
    for(int y = 0; y < pixelNum;y+=1) {
        for (int x = 0; x < pixelNum; x+=1) {
            byte vals[] = new byte[3];
            dataMat.get(sourceY+y, sourceX+x, vals); //get source rgb values
            Imgproc.rectangle(mRgba, new Point(x*pixelSize, y*pixelSize), new Point((x+1)*pixelSize, (y+1)*pixelSize), new Scalar(vals[0],vals[1],vals[2]), -1); //draw large pixels
        }
    }

    Imgproc.rectangle(mRgba, new Point(sourceX, sourceY), new Point(sourceX + pixelNum, sourceY+pixelNum), new Scalar(0,50,200), 1); //draw blue rectangle to show source pixels

    return mRgba;
}

The code is supposed to take the pixels from the blue square and draw them in the top left, basically magnifying them. Except the colors don't match, but I cannot figure out why.

image description

edit retag flag offensive close merge delete

Comments

I don't know what's wrong.I would test with known pixel values to see where things fail. A more robust wat to "zoom" would be resize(), with nearest neighbour method - less code, less loops, less bugs...

mvuori gravatar imagemvuori ( 2020-02-28 01:35:11 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-03-02 18:39:34 -0600

lnx gravatar image

for any image rgb=rgba[:,:,0:3]

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-27 09:06:05 -0600

Seen: 4,777 times

Last updated: Feb 27 '20