Reputation: 1006
I want to get the pixels within a specific color range in android using OpenCV.
This is how i initialize the imageReader (i am using RGBA):
imageReader = ImageReader.newInstance(screenWidth, screenHeight, PixelFormat.RGBA_8888, 2);
This is how i process an image from the imageReader:
Image image = reader.acquireLatestImage();
//Create a Mat using 4 channels (since RGBA uses 4 channels) and fill it with the image-data.
Mat rgba = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
rgba.put(0, 0, bytes);
//Range of colors to be detected:
Scalar lower = new Scalar(10, 10, 100);
Scalar upper = new Scalar(100, 100, 255);
//Create a Mat using 3 channels (since HSV uses 3 channels)
Mat hsv = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
//Convert from source RGBA to destination HSV, the 3 specifes the channels for the destination Mat.
Imgproc.cvtColor(rgba, hsv, Imgproc.COLOR_RGB2HSV, 3);
//Do the filtering
Core.inRange(hsv, lower, upper, hsv);
//Convert back to RGBA (now i use 4 channels since the destination is RGBA)
Imgproc.cvtColor(hsv, rgba, Imgproc.COLOR_HSV2RGB, 4);
image.close();
But at:
Imgproc.cvtColor(hsv, rgba, Imgproc.COLOR_HSV2RGB, 4);
I get the error:
cv::Exception: OpenCV(4.1.2) ...
> Invalid number of channels in input image:
> 'VScn::contains(scn)'
> where
> 'scn' is 1
hsv
is used as the input-argument at that line, but when converting I have always been explicit in how many channels im using, and for hsv
I always use three.
Why am i getting this error?
Upvotes: 0
Views: 957
Reputation: 41765
After
//Do the filtering
Core.inRange(hsv, lower, upper, hsv);
the matrix hsv
is of type CV_8UC1
, and represents the binary single channel mask created by the inRange
function.
So :
//Do the filtering
Mat mask = new Mat(hsv.rows(), hsv.cols(), CvType.CV_8U, new Scalar(0));
Core.inRange(hsv, lower, upper, mask);
// Set to (0,0,0) all pixels that are 0 in the mask, i.e. not in range
Core.bitwise_not( mask, mask);
hsv.setTo(new Scalar(0,0,0), mask);
//Convert back to RGBA (now i use 4 channels since the destination is RGBA)
Imgproc.cvtColor(hsv, rgba, Imgproc.COLOR_HSV2RGB, 4);
Upvotes: 2