【发布时间】:2020-03-26 12:50:36
【问题描述】:
我想使用 OpenCV 在 android 中获取特定颜色范围内的像素。
这就是我初始化 imageReader 的方式(我使用的是 RGBA):
imageReader = ImageReader.newInstance(screenWidth, screenHeight, PixelFormat.RGBA_8888, 2);
这就是我处理来自 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();
但是在:
Imgproc.cvtColor(hsv, rgba, Imgproc.COLOR_HSV2RGB, 4);
我得到错误:
cv::Exception: OpenCV(4.1.2) ...
> Invalid number of channels in input image:
> 'VScn::contains(scn)'
> where
> 'scn' is 1
hsv 用作该行的输入参数,但在转换时我总是明确说明我使用了多少个通道,而对于hsv,我总是使用三个。
为什么会出现这个错误?
【问题讨论】:
-
that 有没有进一步领先?
-
rgba 格式通常用于显示,因此首先使用 cv::COLOR_RGBA2RGB 将其转换为 rgb(错误是由于使用错误的标志将 4 通道转换为 3 通道)----> 然后只需使用cv::COLOR_RGB2HSV
标签: java android opencv image-processing hsv