Display image over detected rectangle (Android)

asked 2019-06-18 18:51:47 -0500

I'm trying to overlay on the live frame a little image previosuly selected by the user. I already have the image's path from a previous activity. My problem is that I am not able to show the image on the frame.

I'm trying to detect a rectangle on the frame, and over the rectangle display the image selected. I could detect the rectangle, but now i can't display the image on any part of the frame.

The following is my code from the onCameraFrame function. (inputFrame is the live frame)

    Mat gray = inputFrame.gray();
    Mat dst = inputFrame.rgba();

    Imgproc.pyrDown(gray, dsIMG, new Size(gray.cols() / 2, gray.rows() / 2));
    Imgproc.pyrUp(dsIMG, usIMG, gray.size());

    Imgproc.Canny(usIMG, bwIMG, 0, threshold);

    Imgproc.dilate(bwIMG, bwIMG, new Mat(), new Point(-1, 1), 1);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    cIMG = bwIMG.clone();

    Imgproc.findContours(cIMG, contours, hovIMG, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);


    for (MatOfPoint cnt : contours) {

        MatOfPoint2f curve = new MatOfPoint2f(cnt.toArray());

        Imgproc.approxPolyDP(curve, approxCurve, 0.02 * Imgproc.arcLength(curve, true), true);

        int numberVertices = (int) approxCurve.total();

        double contourArea = Imgproc.contourArea(cnt);

        if (Math.abs(contourArea) < 100) {
            continue;
        }

        //Rectangle detected
        if (numberVertices >= 4 && numberVertices <= 6) {

            List<Double> cos = new ArrayList<>();

            for (int j = 2; j < numberVertices + 1; j++) {
                cos.add(angle(approxCurve.toArray()[j % numberVertices], approxCurve.toArray()[j - 2], approxCurve.toArray()[j - 1]));
            }

            Collections.sort(cos);

            double mincos = cos.get(0);
            double maxcos = cos.get(cos.size() - 1);

            if (numberVertices == 4 && mincos >= -0.3 && maxcos <= 0.5) {

                 //Here is where I could recognize the rectangle, and I would try to display the image.
                Mat a = imread(img_path);

                Mat bSubmat = dst.submat(0, dst.rows() -1 , 0, dst.cols()-1);
                a.copyTo(bSubmat);


            }
        }
    }
    return dst;
}

I've beenreading other posts but I couldn't find a solution (Or maybe yes, but I've implemented it wrong). As far as I understood, I should create a ROI on the exact position where I need to display the image, and after that I can copy it to the frame and display it.

If you could help me know how is the correct way to display the image, it would help me a lot. Then, I would investigate how to display it on the same position as the rectangle is. Any recommendation or point to start is a great help for me. Thanks!

edit retag flag offensive close merge delete