你必须明白,探测人体并不是那么简单,因为很难区分背景和身体。也就是说,如果你有一个简单的背景,像上传的图像,你可以尝试应用一些图像转换(如应用二进制阈值,otsu。。。查看opencv文档-OpenCV documentation)以使您的ROI“脱颖而出”,因此您可以使用cv2.findContours()进行检测,这与绘制圆形、正方形等的轮廓相同。您甚至可以应用cv2.Canny()(Canny edge detection)来检测图像中的大量边缘,然后搜索轮廓。下面是一个你的图像的例子(如果你的身体周围没有红色的轮廓,效果会更好)。步骤在代码的注释中描述。请注意,这是非常基本的东西,在大多数情况下不起作用,因为人体检测是一个非常困难和广泛的问题。在
示例:import cv2
# Read image and convert it to grayscale.
img = cv2.imread('human.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Search for edges in the image with cv2.Canny().
edges = cv2.Canny(img,150,200)
# Search for contours in the edged image with cv2.findContour().
_, contours, hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# Filter out contours that are not in your interest by applying size criterion.
for cnt in contours:
size = cv2.contourArea(cnt)
if size > 100:
cv2.drawContours(img, [cnt], -1, (255,0,0), 3)
# Display the image.
cv2.imshow('img', img)
结果:
下面是OpenCV文档中关于这个主题的另一个有用的链接:Background Subtraction。希望它能帮你一点忙,给你一个如何处理的想法。干杯!在