【问题标题】:Detect Biggest Rectangle in the Image using Java Opencv [SOLVED]使用 Java Opencv 检测图像中的最大矩形 [已解决]
【发布时间】:2020-07-24 00:10:30
【问题描述】:

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

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

原图

输出图像

请在下面找到代码。我现在不知道如何检测中心正方形的端点。我尝试使用 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);
        }
    }

现在只需通过查看每个柜台的面积来获得最大的。

谢谢。 解决方案图片:

【问题讨论】:

  • 展示你目前的作品。
  • 我已经添加了代码部分。还添加了原始图像。
  • @ThorbjørnRavnAndersen 你能帮我解决这个算法吗?
  • 对不起,我不知道。
  • 我已经使用 findContours 解决了它。我怎样才能结束这个问题。

标签: java opencv image-processing hough-transform opencv-contour


【解决方案1】:
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);
    }
}

输出:

【讨论】:

    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 2023-03-12
    • 2021-07-03
    • 2019-11-29
    • 2012-05-21
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    相关资源
    最近更新 更多