Ask Your Question

OpenCV does not detect the my rectangle [closed]

asked 2020-06-04 07:52:22 -0500

Dtractus gravatar image

updated 2020-06-05 04:20:54 -0500

Hello, im new on OpenCV.

I Have a picture like this;

image description

I wrote code like this;

        image = cv2.imread("photos/testtt.png")
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        edged = cv2.Canny(image, 170, 490)
        (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        idx = 0
        for c in cnts:
            x,y,w,h = cv2.boundingRect(c)
            if w>50 and h>50:
                cv2.imwrite(str(idx) + '.png', new_img)

There are rectangles in the photo, but this code I wrote does not detect these rectangles. I guess I'm not sure, as it leaves very thinly.

I would be very grateful if you could help me, good day everyone.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by supra56
close date 2020-06-06 04:27:08.240717


You are missing cv2.rectangle or probably cv2.DrawContours

supra56 gravatar imagesupra56 ( 2020-06-04 08:26:48 -0500 )edit

@supra56 , thanks but when i use drawContours, draws the outer rectangle of the image. it does not detect rectangles inside.

Dtractus gravatar imageDtractus ( 2020-06-04 10:14:53 -0500 )edit

You don't needed to do that. Just leave it out.

supra56 gravatar imagesupra56 ( 2020-06-04 14:16:39 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-06-04 14:04:52 -0500

supra56 gravatar image

updated 2020-06-05 22:39:58 -0500

@Dtractus . Sorry for delay. I had to do some work in my backyard.. I modified your code. Somehow you don't needed this if w>50 and h>50:. This may occurred problem.

    #!/usr/bin/python 37
    #OpenCV 4.3.0, Raspberry Pi 3/B/4B-w/4/8GB RAM, Buster,v10.
    #Date: 3rd, June, 2020

    import cv2

    # Load the image
    img = cv2.imread('photos/testtt.png')

    # convert to grayscale
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    edged = cv2.Canny(img, 170, 490)
    # Apply adaptive threshold
    thresh = cv2.adaptiveThreshold(edged, 255, 1, 1, 11, 2)
    thresh_color = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)

    # apply some dilation and erosion to join the gaps - change iteration to detect more or less area's
    thresh = cv2.dilate(thresh,None,iterations = 15)
    thresh = cv2.erode(thresh,None,iterations = 15)

    # Find the contours
    contours,hierarchy = cv2.findContours(thresh,

    # For each contour, find the bounding rectangle and draw it
    for cnt in contours:
        x,y,w,h = cv2.boundingRect(cnt)



image description

As I noticed that you have 2 rectangles on image. I cant go further, because I haven't research on OpenCV 4 yet. Here is code:

#!/usr/bin/python 37
#OpenCV 4.3.0, Raspberry Pi 3/B/4B-w/4/8GB RAM, Buster,v10.
#Date: 5th, June, 2020

import cv2

# read and scale down image
img = cv2.pyrDown(cv2.imread('text3.jpg', cv2.IMREAD_UNCHANGED))

# threshold image
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
                                  11, 55, cv2.THRESH_BINARY)

thresh = cv2.dilate(threshed_img, None, iterations = 5)
thresh = cv2.erode(threshed_img, None, iterations = 5)

contours, hier = cv2.findContours(thresh,

for c in contours:
    # get the bounding rect
    x, y, w, h = cv2.boundingRect(c)
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), thickness=1, lineType=8, shift=0)

cv2.imshow('contours', img)

while(cv2.waitKey()is not ord('q')):



image description

edit flag offensive delete link more


For OpenCV 3.x add this ret, contours, hierarchy = cv2.findContours(....

supra56 gravatar imagesupra56 ( 2020-06-04 14:13:14 -0500 )edit

My apologized. I've forgotten about this:

cv2.imwrite(str(idx) + '.png', new_img)
supra56 gravatar imagesupra56 ( 2020-06-04 14:36:21 -0500 )edit

Hello, thanks for code but I just want to get those rectangles(gray areas) I mentioned. Because there can be different writings in those areas, right and left. So it takes all of them one by one, but I want to get the rectangle as a whole. I will then get the articles in it with pytesseract. @supra56

Dtractus gravatar imageDtractus ( 2020-06-04 14:46:26 -0500 )edit

oh you want to do text detection? Google for it - most modern solutions are cnn based. But if your environment does not involve many different combinations - opencv is easier.

holger gravatar imageholger ( 2020-06-04 16:23:27 -0500 )edit

@Dtractus. You want to draw rectangle around gray area? Can you post original image?

supra56 gravatar imagesupra56 ( 2020-06-04 20:18:35 -0500 )edit

Hello again @supra56 i edited image on first post. Really thank you for helping.

Dtractus gravatar imageDtractus ( 2020-06-05 04:21:38 -0500 )edit

Actually, you merely have one grey area rectangle and the rest is none.

supra56 gravatar imagesupra56 ( 2020-06-05 06:02:40 -0500 )edit

You haven't answered my question. Do you want to draw around grey area rectangle?

supra56 gravatar imagesupra56 ( 2020-06-05 06:23:58 -0500 )edit

My goal is to choose only these gray rectangles. Thank you very much for the code you wrote, but it does not work in this image. There is more than one gray rectangle in the picture, but the contrast is too low. It is understood when zoom is done :( @supra56

Dtractus gravatar imageDtractus ( 2020-06-05 06:27:42 -0500 )edit

By the way yes, i want draw each gray area rectangle and save them. @supra56

Dtractus gravatar imageDtractus ( 2020-06-05 06:28:49 -0500 )edit

Question Tools

1 follower


Asked: 2020-06-04 07:51:30 -0500

Seen: 1,887 times

Last updated: Jun 05 '20