将合并的RGB图像转换为灰度OPENCV ANDROID

时间:2017-04-16 21:37:10

标签: android opencv merge split grayscale

我尝试拆分RGB图像(http://www.kavim.com/uploads/signs/tn__51A2376.JPG) 进入单独的通道,对每个通道进行阈值并将其合并在一起,从而产生类似的结果。

enter image description here

我无法理解的是,为什么,如果我尝试将合并后的图像转换为灰度,我会得到此颜色阈值输出(而不是灰度图像)。

    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;
        }
    }

1 个答案:

答案 0 :(得分:0)

问题在于,我试图将对象集转换为inputFrame.rgba()为灰色。

为此我必须创建另一个mat设置为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);

现在可以正常使用了!

相关问题