簡體   English   中英

使用 Java Opencv 檢測圖像中的最大矩形 [已解決]

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

如何使用 java 中的 opencv 檢測最大正方形的四個角點(在圖像的中心)

我已經使用 findContours 解決了這個問題。

原始圖像

在此處輸入圖像描述

Output 圖像

在此處輸入圖像描述

請在下面找到代碼。 我現在不知道如何檢測中心正方形的端點。 我嘗試使用 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);
        }
    }

現在只需通過查看每個櫃台的面積來獲得最大的。

謝謝。 解決方案圖片:

在此處輸入圖像描述

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:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM