i using opencv (cv2 module) in python recognize objects in video. in each frame, want extract particular region aka, contour. after learning opencv docs, have following code snippet:
# np numpy module, contours expected results, # frame each frame of video # iterate through contours. contour in contours: # compute bounding box contour, draw # on frame, , update text. x, y, w, h = cv2.boundingrect(contour) # find mask , build histogram object. mask = np.zeros(frame.shape[:2], np.uint8) mask[y:h, x:w] = 255 masked_img = cv2.bitwise_and(frame, frame, mask = mask) obj_hist = cv2.calchist([masked_img], [0], none, [256], [0, 256]) however, when use matplotlib show masked_img, returns dark image. obj_hist has 1 element number greater 0, first one. wrong?
the problem way setting values in mask. line:
mask[y:h, x:w] = 255 you trying slice each dimension of image using y:h , x:w set mask. left of colon starting row or column, , right of colon denotes end row or column. given start @ y, need offset h using same reference y... same goes x , w.
doing slicing right value of colon less left not modify array in way, , that's why aren't getting output not modifying mask when it's zeroes.
you meant do:
mask[y:y+h, x:x+w] = 255 this set proper region given cv2.boundingrect white (255).
Comments
Post a Comment