Определить самый большой прямоугольник на изображении, используя Java Opencv [решено] - PullRequest
0 голосов
/ 11 апреля 2020

Как я могу определить четыре угловые точки самого большого квадрата (в центре изображения), используя opencv в java

Я решил это с помощью 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

1 Ответ

0 голосов
/ 12 апреля 2020
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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...