Tecnologia da Net
Tecnologia da Net

Reputation: 245

OpenCV Java - How to get a region of interest from a document image?

I'm trying to get a region of interest from a document image. The region of interest is the signature of the person responsible for the document. I'm using opencv on android to try to mark the signature region, but I'm having trouble determining which parameters to use so the algorithm understands it's around the first signature.

I want to get the area of interest like so:

enter image description here

my code:

@Override
    public void onClick(View v) {
        
        AsyncTask.execute(() -> {
            bmScaled.recycle();
            Matrix matrix = new Matrix();
            matrix.postRotate(angleFinal);
            original = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true);

            Mat sourceMat = new Mat(original.getWidth(), original.getHeight(), CvType.CV_8UC3);
            List<MatOfPoint> contourList = new ArrayList<>(); //A list to store all the contours
            Mat imgFinal = new Mat();
            Mat hsvMat = new Mat();
            Mat opening = new Mat();
            Mat close = new Mat();
            Mat hierarchy = new Mat();
            Mat roiTmp = sourceMat.clone();

            Utils.bitmapToMat(original, imgFinal);
            Imgproc.cvtColor(imgFinal, hsvMat, Imgproc.COLOR_BGR2HSV);
            Scalar lowerb= new Scalar(85, 50, 40);
            Scalar upperb= new Scalar(135, 255, 255);
            Core.inRange(hsvMat, lowerb, upperb, roiTmp);
            Mat mask = new Mat();
            Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
            Imgproc.morphologyEx(mask, opening, Imgproc.MORPH_OPEN, kernel);
            Imgproc.morphologyEx(opening, close, Imgproc.MORPH_CLOSE, kernel);

            //finding contours
            Imgproc.findContours(close, contourList, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

            //Drawing contours on a new image
            Mat contours = new Mat();
            contours.create(hierarchy.rows(), hierarchy.cols(), CvType.CV_8UC3);
            Random r = new Random();
            for (int i = 0; i < contourList.size(); i++) {
                Imgproc.drawContours(contours, contourList, i, new Scalar(r.nextInt(255), r.nextInt(255), r.nextInt(255)), -1);
            }

            Imgcodecs.imwrite(IMAGE_PATH+"/TESTE.jpg", contours);

        });
    }

Upvotes: 0

Views: 391

Answers (1)

Andrii Omelchenko
Andrii Omelchenko

Reputation: 13343

Because it's hard to determine borders of signature itself, you can try find other "stable markers" on document and determine ROI (signature area) relative to them. For example, you can select as "markers" form elements 1, 2, 3 and 4 (red rectangles) find them on document and determine approximately signature positions by calculating normalized distances between them. Assuming, if normalized width and height of image are 100 units, signature area should be approximately 75 "horizontal units" and 20 "vertical units" and its top-left corner should be approximately on (10, 70) position (from top-left corner of form):

form origins

Several anchors also needed for "normalization" of document scan by with and height and remove picture rotation (if it exists).

Upvotes: 2

Related Questions