简体   繁体   中英

Detect Biggest Rectangle in the Image using Java Opencv [SOLVED]

How can I detect the four corner points of the biggest square (at center of the image) using opencv in java

I have solved this using findContours.

Original Image

在此处输入图像描述

Output Image

在此处输入图像描述

Please find the code below. I don't now how to detect the end points of center square. I tried to detect lines using HoughLinesP but it is returning only 1 verticle line instead of giving all the 4 lines.

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    String path = "/Users/saurabhsaluja/Desktop/cimg.jpg";
    Mat img = Imgcodecs.imread(path);

    Mat destination = new Mat(img.rows(),img.cols(),img.type());
    Core.addWeighted(img, 1.3, destination, -0.7, 0, destination);

    Mat cannyOutput = new Mat();
    int threshold = 15;
    Mat srcGray = new Mat();

    Imgproc.cvtColor(destination, srcGray, Imgproc.COLOR_BGR2GRAY);        
    Imgproc.Canny(srcGray, cannyOutput, threshold, threshold* 4);

    Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new  Size(10,10));
    Mat element2 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new  Size(10,10));

    Imgproc.dilate(cannyOutput, cannyOutput, element);
    Imgproc.dilate(cannyOutput, cannyOutput, element2);

    element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new  Size(9,9));
    element2 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new  Size(9,9));

    Imgproc.erode(cannyOutput, cannyOutput, element);
    Imgproc.erode(cannyOutput, cannyOutput, element2);

    Imgcodecs.imwrite("/Users/saurabhsaluja/Desktop/cannyOutput.jpg", cannyOutput); //THE IMAGE YOU ARE LOOKING AT

    Mat lines = new Mat();
    Imgproc.HoughLinesP(cannyOutput, lines, 1, Math.PI / 180, 50, 20, 20);

    for(int i = 0; i < lines.cols(); i++) {
        double[] val = lines.get(0, i);
        Imgproc.line(img, new Point(val[0], val[1]), new Point(val[2], val[3]), new Scalar(0, 0, 255), 2);
    }

    Imgcodecs.imwrite("/Users/saurabhsaluja/Desktop/finalimg.jpg", img);

Solution:

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
    Imgproc.findContours(cannyOutput, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    double inf = 0;
    Rect max_rect = null;
    for(int i=0; i< contours.size();i++){
        Rect rect = Imgproc.boundingRect(contours.get(i));

        double area = rect.area();

        if(inf < area) {
            max_rect = rect;
            inf = area;
            //Imgcodecs.imwrite("/Users/saurabhsaluja/Desktop/input"+i+".jpg", img);
        }

        if(area > 50000) {
            System.out.println(area);
            Imgproc.rectangle(img, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,0),5);
        }
    }

Now just get the biggest by looking area of each counter.

Thanks. Solution Image:

在此处输入图像描述

List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
Imgproc.findContours(cannyOutput, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double inf = 0;
Rect max_rect = null;
for(int i=0; i< contours.size();i++){
    Rect rect = Imgproc.boundingRect(contours.get(i));

    double area = rect.area();

    if(inf < area) {
        max_rect = rect;
        inf = area;
        //Imgcodecs.imwrite("/Users/saurabhsaluja/Desktop/input"+i+".jpg", img);
    }

    if(area > 50000) {
        System.out.println(area);
        Imgproc.rectangle(img, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,0),5);
    }
}

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM