Извлеките зеленые прямоугольники с картинки и сохраните в папку - PullRequest
0 голосов
/ 12 февраля 2020

код для извлечения зеленых прямоугольников с картинки "Python -Interview.jpg" и копирования их в папку. Я пытался не работать, пожалуйста, предоставьте какое-то решение с этим кодом

            # Import relevant libraries
   ''' image = cv.imread(fpath, -1)
    # convert to gray and binarize
    gray_img = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    binary_img = cv.adaptiveThreshold(gray_img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 9, 9)
     # note: erosion and dilation works on white forground
    binary_img = cv.bitwise_not(binary_img)
     # dilate the image
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (1,1))
    dilated_img = cv.morphologyEx(binary_img, cv.MORPH_DILATE, kernel,iterations=1)

    # find contours, discard contours which do not belong to a rectangle
    (cnts, _) = cv.findContours(dilated_img.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
    sq_cnts = []  # contours of interest to us
    for cnt in cnts:
        approx = cv.approxPolyDP(cnt,0.01*cv.arcLength(cnt,True),True)
        if len(approx) == 4:
            (x, y, w, h) = cv.boundingRect(cnt)
    #///////// fill remaining code here

    for i in range(len(sq_cnts)):
        # find squares
        (x, y, w, h) = cv.boundingRect(sq_cnts[i])
        newimg = image[y:y+h,x:x+w] # crop the image   
        # Write the image  
    '''     

Изображение

enter image description here

Ответы [ 2 ]

2 голосов
/ 13 февраля 2020

Вы можете достичь этого просто используя minAreaRect . Который я также использовал тот же метод в этом ответ от меня . Я написал в C ++, потому что моя среда основана на C ++. Преобразование в Python не является большой проблемой. Вот мой код и результаты:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace cv;
using namespace std;

int main()
{

    Mat src; Mat src_gray;
    RNG rng(12345);
    int counter = 0;
    /// Load source image and convert it to gray
    src = imread( "/ur/image/directory/Python-Interview.jpg", 1 );
    Mat original = src.clone();
    /// Convert image to gray and blur it
    cvtColor( src, src_gray, CV_BGR2GRAY );

    imshow("dd",src_gray);

    Mat threshold_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Detect edges using Threshold
    threshold( src_gray, threshold_output, 138, 100, THRESH_BINARY );
    /// Find contours
    findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    /// Find the rotated rectangles for each contour
    vector<RotatedRect> minRect( contours.size() );

    for( int i = 0; i < contours.size(); i++ )
        minRect[i] = minAreaRect( Mat(contours[i]) );

    /// Draw contours + rotated rects
    Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
    Mat result_zero = Mat::zeros( threshold_output.size(), CV_8UC3 );

    for( int i = 0; i< contours.size(); i++ )
    {
        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
        // detect contours
        drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
        // detect rectangle for each contour
        Point2f rect_points[4]; minRect[i].points( rect_points );

        double length_1 = cv::norm(cv::Mat(rect_points[0]),cv::Mat(rect_points[1]));
        double length_2 = cv::norm(cv::Mat(rect_points[1]),cv::Mat(rect_points[2]));

        int temp1 = (int)length_1;
        int temp2 = (int)length_2;

        //25 and 35 are the pixel lengths of those small rectangles. By using this if scope I am filtering them
        if(temp1>25 && temp1<35 && temp2>25 && temp2<35 )
        {

            int min_x = rect_points[0].x;
            int min_y = rect_points[0].y;
            for( int j = 0; j < 4; j++ )
            {

                if(rect_points[j].x<min_x)
                    min_x = rect_points[j].x;

                if(rect_points[j].y<min_y)
                    min_y = rect_points[j].y;

                line( result_zero, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );

            }

            Rect my_roi(min_x,min_y,temp1,temp2);
            Mat copy = original(my_roi);
            imwrite("/ur/target/copy/image/directory/image" + to_string(counter) + ".jpg",copy);
            counter++;
        }
    }

    imshow( "Result", result_zero );

    waitKey(0);
    return(0);
}

Результат:

enter image description here

В конце ваш каталог будет выглядеть так:

enter image description here

1 голос
/ 19 февраля 2020
import cv2 as cv

image = cv.imread(r"----Path--\img1.jpg", -1)

gray_img = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
binary_img = cv.adaptiveThreshold(gray_img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 9, 9)


binary_img = cv.bitwise_not(binary_img)


kernel = cv.getStructuringElement(cv.MORPH_RECT, (1,1))
dilated_img = cv.morphologyEx(binary_img, cv.MORPH_DILATE, kernel,iterations=1)


(cnts, _)= cv.findContours(dilated_img.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
print("Number of countours = "+ str(len(cnts)))


sq_cnts = [] 

for cnt in cnts:
    approx = cv.approxPolyDP(cnt,0.01*cv.arcLength(cnt,True),True)

    if len(approx) == 4:
      (x, y, w, h) = cv.boundingRect(cnt)

      aspectRatio = float(w) / h
      print (aspectRatio,"Aspectratio") 

      if aspectRatio > 1.0 and aspectRatio <= 1.08:
          cv.drawContours(image,[cnt], 0, (0,255,0), 3)
          sq_cnts.append(cnt)

          for i in range(len(sq_cnts)):

            (x, y, w, h) = cv.boundingRect(sq_cnts[i])
            newimg = image[y:y+h,x:x+w]   


            cv.imwrite(str(i)+'.jpg',newimg)
            cv.imshow('img.jpg', newimg)


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