将合并后的 RGB 图像转换为灰度 OPENCV ANDROID
Convert merged RGB image to grayscale OPENCV ANDROID
我尝试拆分 RGB 图像 (http://www.kavim.com/uploads/signs/tn__51A2376.JPG)
进入单独的通道,对每个通道设置阈值并将其合并回一起,从而产生类似这样的结果。
我不明白的是,为什么,如果我尝试将合并后的图像转换为灰度图像,我会得到这个颜色阈值输出(而不是灰度图像)。
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat rgba = inputFrame.rgba();
Size sizeRgba = rgba.size();
Mat grayInnerWindow1;
Mat rgbaInnerWindow;
int rows = (int) sizeRgba.height;
int cols = (int) sizeRgba.width;
int left = cols / 8;
int top = rows / 8;
int width = cols * 3 / 4;
int height = rows * 3 / 4;
rgbaInnerWindow = rgba.submat(top, top + height, left, left + width);
ArrayList<Mat> channels = new ArrayList<Mat>(3);
Mat src1= Mat.zeros(rgbaInnerWindow.size(),CvType.CV_8UC3);
Core.split(rgbaInnerWindow, channels);
Mat b = channels.get(0);
Imgproc.threshold(b, b, 0, 70, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
Mat g = channels.get(1);
Imgproc.threshold(g, g, 0, 70, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
Mat r = channels.get(2);
Imgproc.threshold(r, r, 90, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
Core.merge(channels, src1);
Imgproc.medianBlur(src1, src1, 3);
Imgproc.threshold(src1,rgbaInnerWindow,0, 255, Imgproc.THRESH_BINARY);
Imgproc.cvtColor(rgbaInnerWindow,src1, Imgproc.COLOR_BGR2GRAY);
rgbaInnerWindow.release();
return rgba;
}
}
问题是,我试图将设置为 inputFrame.rgba() 的对象转换为灰色。
为此,我必须创建另一个垫子设置为 inputFrame.gray(),然后尝试转换它。
Mat rgb = inputFrame.rgba();
Mat gray = inputFrame.gray();
Mat grayInnerWindow = gray.submat(top, top + height, left, left + width);
Mat rgbaInnerWindow = rgba.submat(top, top + height, left, left + width);
//SOME CODE
Imgproc.cvtColor(rgbaInnerWindow,grayInnerWindow, Imgproc.COLOR_BGR2GRAY);
现在可以正常使用了!
我尝试拆分 RGB 图像 (http://www.kavim.com/uploads/signs/tn__51A2376.JPG) 进入单独的通道,对每个通道设置阈值并将其合并回一起,从而产生类似这样的结果。
我不明白的是,为什么,如果我尝试将合并后的图像转换为灰度图像,我会得到这个颜色阈值输出(而不是灰度图像)。
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat rgba = inputFrame.rgba();
Size sizeRgba = rgba.size();
Mat grayInnerWindow1;
Mat rgbaInnerWindow;
int rows = (int) sizeRgba.height;
int cols = (int) sizeRgba.width;
int left = cols / 8;
int top = rows / 8;
int width = cols * 3 / 4;
int height = rows * 3 / 4;
rgbaInnerWindow = rgba.submat(top, top + height, left, left + width);
ArrayList<Mat> channels = new ArrayList<Mat>(3);
Mat src1= Mat.zeros(rgbaInnerWindow.size(),CvType.CV_8UC3);
Core.split(rgbaInnerWindow, channels);
Mat b = channels.get(0);
Imgproc.threshold(b, b, 0, 70, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
Mat g = channels.get(1);
Imgproc.threshold(g, g, 0, 70, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
Mat r = channels.get(2);
Imgproc.threshold(r, r, 90, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
Core.merge(channels, src1);
Imgproc.medianBlur(src1, src1, 3);
Imgproc.threshold(src1,rgbaInnerWindow,0, 255, Imgproc.THRESH_BINARY);
Imgproc.cvtColor(rgbaInnerWindow,src1, Imgproc.COLOR_BGR2GRAY);
rgbaInnerWindow.release();
return rgba;
}
}
问题是,我试图将设置为 inputFrame.rgba() 的对象转换为灰色。
为此,我必须创建另一个垫子设置为 inputFrame.gray(),然后尝试转换它。
Mat rgb = inputFrame.rgba();
Mat gray = inputFrame.gray();
Mat grayInnerWindow = gray.submat(top, top + height, left, left + width);
Mat rgbaInnerWindow = rgba.submat(top, top + height, left, left + width);
//SOME CODE
Imgproc.cvtColor(rgbaInnerWindow,grayInnerWindow, Imgproc.COLOR_BGR2GRAY);
现在可以正常使用了!