Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - How to find white bordered rectangle in an image?

I'm trying to get the coordinate of the white-bordered rectangle in image. The traditional approach is to find contours to find all the rectangles present in the image but I want to get only with certain bordered color like in the below picture image

How can I get the coordinates of white bordered rectangle only using opencv? any suggestions on this very helpful, thanks

Edit: I tried suggestion given by guivi using python as below

import cv2
import numpy as np
import random as rng
image = cv2.imread('strawberry.png')
threshold = 100
grayscale= cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
canny_output = cv2.Canny(grayscale, threshold, threshold * 2)

ret, thresh= cv2.threshold(grayscale,200,255,cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(canny_output,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
contours_poly = [None]*len(contours)
#boundRect = [None]*len(contours)
boundRect = list()
centers = [None]*len(contours)
radius = [None]*len(contours)
for i, c in enumerate(contours):
    contours_poly[i] = cv2.approxPolyDP(c, 3, True)
    if len(contours_poly[i]) == 4:
        boundRect.append(cv2.boundingRect(contours_poly[i]))
    #boundRect[i] = cv.boundingRect(contours_poly[i])
    #centers[i], radius[i] = cv.minEnclosingCircle(contours_poly[i])


drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)

print(len(boundRect))
#for i in range(len(contours)):
#    color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
#    cv.drawContours(drawing, contours_poly, i, color)
#    cv.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), 
#      (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
#cv.circle(drawing, (int(centers[i][0]), int(centers[i][1])), int(radius[i]), color, 2)
for i in range(len(boundRect)):
    if boundRect[i][2]/boundRect[i][3] > 2:
        color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
        cv2.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), 
            (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
        print(f'Rectangle X:{boundRect[i][0]}, Y:{boundRect[i][1]}, W:{boundRect[i][2]}, H:{boundRect[i][3]}')

but I couldn't find position of rectangle in image as @guivi got using c++ instead I got the some other 4 rectangle coordinates as shown below image img_op

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do as you are thinking more or less:

  1. load the image.
  2. convert to gray scale.
  3. apply some threshold.
  4. Calculate the contours.
  5. Look at the contours and check for shapes.
  6. segregate rectangles and filter by aspect ratio.
  7. you are done!

I used C++ to achieve this but I am sure you can convert it to python.

Mat image = imread("st.bmp");

if (image.empty())
    return EXIT_FAILURE;

Mat gray;
cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
vector<vector<Point>> contoursgray;
vector<Vec4i> hierarchygray;
threshold(gray, gray, 200, 255, cv::THRESH_BINARY_INV);
findContours(gray, contoursgray, hierarchygray, RETR_TREE, CHAIN_APPROX_NONE);
// draw contours on the original image
Mat image_contour_gray = image.clone();
//drawContours( image_contour_gray, contoursgray, -1, Scalar(0, 255, 0), 2);
vector<vector<Point>> contoursgray2;
for (auto points : contoursgray) {
    double peri = cv::arcLength(points, true);
    vector<Point> aprox;
    cv::approxPolyDP(points, aprox, 0.04 * peri, true);
    if (aprox.size() == 4)
    {
        cv::Rect rect = cv::boundingRect(aprox);
        if (rect.width / rect.height > 1.5) {
            std::cout << rect.x << "  " << rect.y << " " << rect.width << " " << rect.height << std::endl;
            contoursgray2.push_back(aprox);                
        }
    }
}
if (contoursgray2.size() > 0) {
    drawContours(image_contour_gray, contoursgray2, -1, Scalar(0, 255, 0), 2);
    imshow("Contour detection using gray conversion", image_contour_gray);
}
waitKey(0);
imwrite("gray.jpg", image_contour_gray);
destroyAllWindows();

This gives me the following image as output: outputimage:

And it also outputs the following rectangles(x, y, w, h) on the command prom:

enter image description here

I have taken the tutorial from opencv webpage and changed the following lines:

contours_poly = [None]*len(contours)
#boundRect = [None]*len(contours)
boundRect = list()
centers = [None]*len(contours)
radius = [None]*len(contours)
for i, c in enumerate(contours):
    contours_poly[i] = cv.approxPolyDP(c, 3, True)
    if len(contours_poly[i]) == 4:
        boundRect.append(cv.boundingRect(contours_poly[i]))
    #boundRect[i] = cv.boundingRect(contours_poly[i])
    #centers[i], radius[i] = cv.minEnclosingCircle(contours_poly[i])


drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)


#for i in range(len(contours)):
#    color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
#    cv.drawContours(drawing, contours_poly, i, color)
#    cv.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), 
#      (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
#cv.circle(drawing, (int(centers[i][0]), int(centers[i][1])), int(radius[i]), color, 2)
for i in range(len(boundRect)):
    if boundRect[i][2]/boundRect[i][3] > 2:
        color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
        cv.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), 
            (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
        print(f'Rectangle X:{boundRect[i][0]}, Y:{boundRect[i][1]}, W:{boundRect[i][2]}, H:{boundRect[i][3]}')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...