Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is code:

#!/usr/bin env 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)

Ouput:

image description

Here is code:

#!/usr/bin env 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)

Ouput:Outtput:

image description

Here is code:

#!/usr/bin env python #!/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