【发布时间】:2015-07-13 13:05:57
【问题描述】:
我在 python 中使用 opencv(cv2 模块)来识别视频中的对象。在每一帧中,我想提取一个特定的区域,也就是轮廓。向opencv docs学习后,我有如下代码sn-p:
# 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 我已经编辑了它,希望这可以帮助你理解我的问题:-)
-
您确认
contours包含任何内容了吗? -
@MikeC 是的,
contours很好,我可以用它检测新对象。但我未能提取特定区域
标签: python opencv image-processing matplotlib