0
votes

I tried to split RGB image (http://www.kavim.com/uploads/signs/tn__51A2376.JPG) into seperate channels, threshold each channel and merge it back together resulting in something like this.

enter image description here

What I fail to understand is, why, if I try to convert the merged image into grayscale I get this color threshold output (and not a grayscale image instead).

    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

1 Answers

0
votes

The problem was that, I was trying to convert object set as inputFrame.rgba() to gray.

For this I had to create another mat set as inputFrame.gray() and then try to convert it.

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

This now works as it should!