How do I get 0-255 RGB values from an RGBA mat?
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.
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...