1 | initial version |
This solution is inspired by the Sepia filter that you can find in an OpenCV4Android Sample "Image Manipulations".
It might be a bit slow, because it does more processing than it should for a grayscale conversion, but it does not need to make image copies like other solutions.
// Because you mentioned changing parameter from yuv to rgba
// you don't need to change it, you can convert it like this:
Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 3);
// define some rectangle to be the roi
Rec roi = new Rect( arguments );
// This kernel tries to apply this formula:
// gray pixel = (red + green + blue) / 3 => each channel has a weight of 1/3 = 0.333%
mGrayKernel = new Mat(3, 3, CvType.CV_32F);
mGrayKernel.put(0, 0, /* R */0.333f, 0.333f, 0.333f);
mGrayKernel.put(1, 0, /* G */0.333f, 0.333f, 0.333f);
mGrayKernel.put(2, 0, /* B */0.333f, 0.333f, 0.333f);
Core.transform(mRgba.submat(roi), mRgba.submat(roi), mGrayaKernel);
Let us know if you have any problem with this, or if you prefer @karlphillip's solution in Java.
2 | No.2 Revision |
This solution is inspired by the Sepia filter that you can find in an OpenCV4Android Sample "Image Manipulations".
It might be a bit slow, because it does more processing than it should for a grayscale conversion, but it does not need to make image copies like other solutions.
// Because you mentioned changing parameter from yuv to rgba
// you don't need to change it, you can convert it like this:
Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 3);
// define some rectangle to be the roi
Rec roi = new Rect( arguments );
// This kernel tries to apply this formula:
// gray pixel = (red + green + blue) / 3 => each channel has a weight of 1/3 = 0.333%
mGrayKernel = new Mat(3, 3, CvType.CV_32F);
mGrayKernel.put(0, 0, /* R */0.333f, 0.333f, 0.333f);
mGrayKernel.put(1, 0, /* G */0.333f, 0.333f, 0.333f);
mGrayKernel.put(2, 0, /* B */0.333f, 0.333f, 0.333f);
Core.transform(mRgba.submat(roi), mRgba.submat(roi), mGrayaKernel);
mGrayKernel);
Let us know if you have any problem with this, or if you prefer @karlphillip's solution in Java.