Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to detect colored rectangle from image in android?

hello all, i want detect specific colored rectangle from paper sheet which indicate border of paper.so i want detect that rectangle and crop it so i can do further operation on sheet.

i tried this code..

        Mat rgbMat = new Mat();
        Utils.bitmapToMat(resultBitmap1, rgbMat);
        Mat grayMat = new Mat(resultBitmap1.getHeight(), resultBitmap1.getWidth(),
                CvType.CV_8U, new Scalar(1));
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();

        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 2);
        Imgproc.threshold(grayMat, grayMat, 100, 255, Imgproc.THRESH_BINARY);
        Core.bitwise_not(grayMat, grayMat);

        Imgproc.findContours(grayMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        pointList=new ArrayList<Point>();

        for(int i=0; i<contours.size(); i++)
        {
            Rect rect = Imgproc.boundingRect(contours.get(i));
            double k = (rect.height+0.0)/rect.width;
            if (0.9<k && k<1.1)
            {
                Point tmppnt=new Point(rect.x,rect.y);
                pointList.add(tmppnt);
                Imgproc.drawContours(rgbMat, contours, i, new Scalar(255, 0, 0),3);
            }
        }

        Utils.matToBitmap(rgbMat, resultBitmap1);
        sourceImageView.setImageBitmap(resultBitmap1);

but i can't detect with this code..this is sample sheet image description

please help me..

thanks

How to detect colored rectangle from image in android?

hello all, i want detect specific colored rectangle from paper sheet which indicate border of paper.so i want detect that rectangle and crop it so i can do further operation on sheet.

i tried this code..

        Mat rgbMat = new Mat();
        Utils.bitmapToMat(resultBitmap1, rgbMat);
        Mat grayMat = new Mat(resultBitmap1.getHeight(), resultBitmap1.getWidth(),
                CvType.CV_8U, new Scalar(1));
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();

        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 2);
        Imgproc.threshold(grayMat, grayMat, 100, 255, Imgproc.THRESH_BINARY);
        Core.bitwise_not(grayMat, grayMat);

        Imgproc.findContours(grayMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        pointList=new ArrayList<Point>();

        for(int i=0; i<contours.size(); i++)
        {
            Rect rect = Imgproc.boundingRect(contours.get(i));
            double k = (rect.height+0.0)/rect.width;
            if (0.9<k && k<1.1)
            {
                Point tmppnt=new Point(rect.x,rect.y);
                pointList.add(tmppnt);
                Imgproc.drawContours(rgbMat, contours, i, new Scalar(255, 0, 0),3);
            }
        }

        Utils.matToBitmap(rgbMat, resultBitmap1);
        sourceImageView.setImageBitmap(resultBitmap1);

but i can't detect with this code..this is sample sheet image description

i Want only inside blue point image.

image description

please help me..

thanks

How to detect colored rectangle from image in android?

hello all, i want detect specific colored rectangle from paper sheet which indicate border of paper.so i want detect that rectangle and crop it so i can do further operation on sheet.

i tried this code..

        Mat rgbMat = new Mat();
        Utils.bitmapToMat(resultBitmap1, rgbMat);
        Mat grayMat = new Mat(resultBitmap1.getHeight(), resultBitmap1.getWidth(),
                CvType.CV_8U, new Scalar(1));
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();

        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 2);
        Imgproc.threshold(grayMat, grayMat, 100, 255, Imgproc.THRESH_BINARY);
        Core.bitwise_not(grayMat, grayMat);

        Imgproc.findContours(grayMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        pointList=new ArrayList<Point>();

        for(int i=0; i<contours.size(); i++)
        {
            Rect rect = Imgproc.boundingRect(contours.get(i));
            double k = (rect.height+0.0)/rect.width;
            if (0.9<k && k<1.1)
            {
                Point tmppnt=new Point(rect.x,rect.y);
                pointList.add(tmppnt);
                Imgproc.drawContours(rgbMat, contours, i, new Scalar(255, 0, 0),3);
            }
        }

        Utils.matToBitmap(rgbMat, resultBitmap1);
        sourceImageView.setImageBitmap(resultBitmap1);

but i can't detect with this code..this is sample sheet image description

i Want only inside blue point image.

image description

please help me..

thanks