Ask Your Question
0

Finding rectangle contours in an image

asked 2020-06-01 09:56:56 -0600

dds gravatar image

updated 2020-06-01 11:17:16 -0600

supra56 gravatar image

i'm trying to find the length and width in pixels of the rectangle outlined by the thin black line in the attached .jpg/image. the L and W of the image/jpg is slightly bigger than the black line rectangle. C:\Users\Daniel_Scippa\Desktop\image.jpg

any guidance and/or direction would be appreciated. let me know

edit retag flag offensive close merge delete

Comments

1

The width and height of the rectangle correspond to the width and height of the bounding rect of the contour. Here's what I would do: - Contour detection of the outer contour - Evaluation of the Bounding Rect

Visionmop gravatar imageVisionmop ( 2020-06-02 14:50:21 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-06-03 07:13:17 -0600

supra56 gravatar image

updated 2020-06-03 07:16:12 -0600

Here is code:

#!/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

font = cv2.FONT_HERSHEY_SIMPLEX    

frame = cv2.imread('text.jpg')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7, 7), 0.5)
edge = cv2.Canny(blur, 0, 50, 3)

contours, hierarchy = cv2.findContours(edge, cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)


for contour, hier in zip(contours, hierarchy):
    (x,y,w,h) = cv2.boundingRect(contour)
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.putText(frame, ('width = {}, height = {}'.format(w, h)),
                (x+30, y+30),
                font,
                1,
                (0, 255, 0),
                2,
                cv2.LINE_AA)

cv2.imshow('Measure Size', frame)
cv2.waitKey(0)

Outtput:

image description

edit flag offensive delete link more

Comments

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

supra56 gravatar imagesupra56 ( 2020-06-03 07:15:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-06-01 09:56:56 -0600

Seen: 13,284 times

Last updated: Jun 03 '20