user3160152
user3160152

Reputation: 51

Convert a submat from RGB to GRAY OpenCV

I'm working on an android program that should detect a face in a live video, and convert the facial region to gray-scale. I successfully manage to create a border around the detected facial region, and using the function shown below, i've managed to create a submat that shows the face on the top left of the main Mat. My plan is to convert this submat to gray-scale and then place it over the actual face, unless someone has a more efficient approach?

The code for the submat can be seen below. When i add the line:

Imgproc.cvtColor(mRgba2,mRgba2,Imgproc.COLOR_RGB2GRAY);

There are no errors, but the stretched face on the topleft corner isn't shown. I figured this is because mRgba2 is now a different type, so i tried adding

Imgproc.cvtColor(mRgba2,mRgba2,Imgproc.COLOR_RGB2GRAY);
Imgproc.cvtColor(mRgba2,mRgba2,Imgproc.COLOR_GRAY2RGB);

to convert mRgba2 back to RGB but this didn't work either.

public void processFrame(Mat face) { 

    Mat mRgba2 = face.clone();
    Imgproc.resize(face, mRgba2, new Size (400, 200));
    Mat submat = mRgba.submat(0, 200, 0, 400);
    mRgba2.copyTo(submat);
}

enter image description here

Upvotes: 0

Views: 3096

Answers (1)

Micka
Micka

Reputation: 20160

Don't use in-place conversion

Imgproc.cvtColor(mRgba2,mRgba2,Imgproc.COLOR_RGB2GRAY);

instead try to use a second variable:

Mat mGray;
Imgproc.cvtColor(mRgba2,mGray,Imgproc.COLOR_RGB2GRAY);

But are you sure that RGB is the right type? Your variable is named Rgba, so maybe you'll need Imgproc.COLOR_RGBA2GRAY ?

Upvotes: 1

Related Questions