extract bounding boxes opencv

Solutions on MaxInterview for extract bounding boxes opencv by the best coders in the world

showing results for - "extract bounding boxes opencv"
Clinton
26 Apr 2019
1import cv2
2
3im = cv2.imread('c:/data/ph.jpg')
4gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
5contours, hierarchy = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2:]
6idx =0 
7for cnt in contours:
8    idx += 1
9    x,y,w,h = cv2.boundingRect(cnt)
10    roi=im[y:y+h,x:x+w]
11    cv2.imwrite(str(idx) + '.jpg', roi)
12    #cv2.rectangle(im,(x,y),(x+w,y+h),(200,0,0),2)
13cv2.imshow('img',im)
14cv2.waitKey(0)    
15