使用Java Opencv检测图像中的最大矩形[已解决]

问题描述 投票:-1回答:1

如何使用java中的opencv检测最大正方形(在图像的中心)的四个角点

我已经使用findContours解决了这个问题。

原始图像

enter image description here

输出图像

enter image description here

请在下面找到代码。我现在不知道如何检测中心正方形的终点。我尝试使用HoughLinesP检测线,但它仅返回1条垂直线,而不是全部提供4条线。

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

解决方案:

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

现在只需查看每个柜台的面积即可获得最大的收益。

谢谢。解决方案图片:

enter image description here

java opencv image-processing hough-transform opencv-contour
1个回答
0
投票
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);
    }
}

[[输出问题中添加的图像]

© www.soinside.com 2019 - 2024. All rights reserved.