我在Python中使用opencv(cv2模塊)來識別視頻中的對象。在每一幀中,我想提取一個特定的區域,也就是輪廓。從opencv docs學習後,我有下面的代碼片段:如何在Python中使用opencv使用輪廓遮罩視頻幀
# np is numpy module, contours are expected results,
# frame is each frame of the video
# Iterate through the contours.
for contour in contours:
# Compute the bounding box for the contour, draw
# it on the frame, and update the text.
x, y, w, h = cv2.boundingRect(contour)
# Find the mask and build a histogram for the 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])
然而,當我使用matplotlib
展現masked_img
,它返回一個黑暗的圖像。 obj_hist
只有一個數字大於0
的元素,這是第一個元素。哪裏不對?
請閱讀[問] – boardrider
@boardrider我已編輯它,希望這可以幫助您瞭解我的問題:-) – user4394476
您是否已驗證「輪廓」包含任何內容? –