Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Sorry for delay. Here is code:

#!/usr/bin env python 37
#OpenCV 4.3.0, Raspberry Pi 3/B/4B, Buster,v10.
#Date : 29th, April, 2020

import cv2
#325, 287 pixel

cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX    

while  cap.isOpened():
    rval, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (7, 7), 1.5, 1.5)
    edge = cv2.Canny(blur, 0, 50, 3)
    contours, hierarchy = cv2.findContours(edge, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    try: hierarchy = hierarchy[0]
    except: hierarchy = []
    for contour, hier in zip(contours, hierarchy):
        (x,y,w,h) = cv2.boundingRect(contour)
        if (w >= 150 and h >= 150):
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(frame, ('width = %d, height = %d' % (w, h)),
                        (x, y), font, 1, (0, 255, 0), 2, cv2.LINE_AA)


    cv2.imshow('Lines', frame)
    c = cv2.waitKey(1)  
    if c == 27 or c ==10:
         break

cap.release() 
cv2.destroyAllWindows()