2/28/2022

cv2, findContours, find object and print x,y,w,h

Find white color object and print x,y,w,h

see example source code

..

#open image
img = cv2.imread("./test.jpg")

#binary
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)

#contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

#print x,y,w,h for each object
w_list = []
h_list = []
for v in contours:
x,y,w,h = cv2.boundingRect(v)
w_list.append(w)
h_list.append(h)
print(x,y,w,h)

..

66 193 19 14
--
66 171 19 14
--



www.marearts.com

thank you.

πŸ™‡πŸ»‍♂️