简体   繁体   中英

Improve rectangle contour detection in image using OpenCV

I am trying to detect the rectangular boxes in the given image

Original image: 原始图像 but the image is not good enough to detect rectangles, how can i improve it and detect all the rectangles in image?

I tried to convert the image into binary image using canny edge detection and applied dilation ,bilateral filter then the output is this:

二进制图像

I tried to apply all the morphologyEx, sobel then to i was not able to detect all rectangles in the image. If i am able to find all the boundary of rectangle then i can detect all rectangles using find countours but how can i improve image to detect all the rectangles.

The code for the given conversion is given below

img =  cv2.imread("givenimage.png",0)
img = cv2.resize(img,(1280,720))
edges = cv2.Canny(img,100,200)
kernal = np.ones((2,2),np.uint8)
dilation = cv2.dilate(edges, kernal , iterations=2)
bilateral = cv2.bilateralFilter(dilation,9,75,75)
contours, hireracy = cv2.findContours(bilateral,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i,contour in enumerate(contours):
    approx = cv2.approxPolyDP(contour, 0.01*cv2.arcLength(contour,True),True)   
    if len(approx) ==4:
        X,Y,W,H = cv2.boundingRect(approx)
        aspectratio = float(W)/H
        if aspectratio >=1.2 :
            box = cv2.rectangle(img, (X,Y), (X+W,Y+H), (0,0,255), 2)
            cropped = img[Y: Y+H, X: X+W]
            cv2.drawContours(img, [approx], 0, (0,255,0),5)
            x = approx.ravel()[0]
            y = approx.ravel()[1]
            cv2.putText(img, "rectangle"+str(i), (x,y),cv2.FONT_HERSHEY_COMPLEX, 0.5, (0,255,0))
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output of the following program detects only 8 rectangles:

产量

but i need to detect all the rectangles present in the image

1) Can I increase the thickness of the image for all the black pixels in this:

原始图像

2) Can I dilate all the pixel region between the white boundary of the

二进制图像

Here's a simple approach:

  • Convert image to grayscale and Gaussian blur
  • Perform canny edge detection
  • Find contours and draw rectangles

Canny edge detection

在此输入图像描述

Results

在此输入图像描述

import cv2

image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
canny = cv2.Canny(blurred, 120, 255, 1)

# Find contours
cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

# Iterate thorugh contours and draw rectangles around contours
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)

cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.imwrite('canny.png', canny)
cv2.imwrite('image.png', image)
cv2.waitKey(0)

Your thoughts are right, but on first stage you can use threshold operation. Then find contours. Then minAreaRect operation on founded contours.

Edit:

Result and code(c++):

在此输入图像描述

Mat img = imread("/Users/alex/Downloads/MyPRI.png", IMREAD_GRAYSCALE);
Mat img2;
threshold(img, img2, 220, 255, THRESH_BINARY);

Mat element = getStructuringElement(MORPH_CROSS, Size(3, 3), Point(1, 1));
erode(img2, img2, element); // without it find contours fails on some rects

imshow("img", img);
imshow("img2", img2);
waitKey();


// preprocessing done, search rectanges
vector<vector<Point> > contours;

vector<Vec4i> hierarchy;
findContours(img2, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

vector<RotatedRect> rects;
for (int i = 0; i < contours.size(); i++) {
    if (hierarchy[i][2] > 0) continue;

    // capture inner contour
    RotatedRect rr = minAreaRect(contours[i]);
    if (rr.size.area() < 100) continue; // too small

    rr.size.width += 8;
    rr.size.height += 8; // expand to outlier rect if needed
    rects.push_back(rr);
}


Mat debugImg;
cvtColor(img, debugImg, CV_GRAY2BGR);
for (RotatedRect rr : rects) {
    Point2f points[4];
    rr.points(points);
    for (int i = 0; i < 4; i++) {
        int ii = (i + 1) % 4;
        line(debugImg, points[i], points[ii], CV_RGB(255, 0, 0), 2);
    }
}
imshow("debug", debugImg);
waitKey();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM