[英]Finding separate centroids of each white spots in a frame?
我正在进行手指检测,即使用VGA摄像头的指尖。 我使用了HSV和图像阈值处理,并且能够检测到指尖。
问题:如果只有一个白点(如果我只用一根手指的话),我现在可以在黑白图像中找到白点的质心。 但是如果我用多个手指放置,最终图像中会出现更多白点。 所以我想分别找到每个质心。
我想找到每个白色斑点的所有质心,即,如果我在相机前面放多个手指。 看下面的代码
thresholded_img = cv.CreateImage(cv.GetSize(hsv_img), 8, 1)
cv.InRangeS(hsv_img, (0,0,200), (0,0,255), thresholded_img)
moments = cv.Moments(cv.GetMat(thresholded_img,1), 0)
area = cv.GetCentralMoment(moments, 0, 0)
x = cv.GetSpatialMoment(moments, 1, 0)/area
y = cv.GetSpatialMoment(moments, 0, 1)/area
posY=y
posX=x
这里thresholded_img
是一个黑白图像,其中指尖仅表示为白色,其他所有表示为黑色。
在此代码中,如果thresholded_img
包含单个白点,那么我可以正确获取该点的质心的x
和y
坐标!
但是如何找到该图像中每个白点的质心呢?
但是,如果在阈值图像中有多个白点,则说明找到了错误的质心!
如何更改上面的代码以在单个帧(图像)中查找(x,y坐标)每个白点的单独质心?
我使用您上传的图片测试了以下代码。
我得到以下文本输出:
cv2 version: 2.4.4
centroids: [(580, 547), (437, 546), (276, 545), (115, 545), (495, 425), (334, 424), (174, 424), (24, 423), (581, 304), (437, 303), (277, 303), (117, 302), (495, 182), (334, 181), (174, 181), (25, 181), (581, 60), (438, 59), (277, 59), (117, 59)]
和这张图片:
#!/usr/bin/env python
import cv2
img = cv2.imread('HFOUG.jpg',cv2.CV_LOAD_IMAGE_GRAYSCALE)
_,img = cv2.threshold(img,0,255,cv2.THRESH_OTSU)
h, w = img.shape[:2]
contours0, hierarchy = cv2.findContours( img.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
moments = [cv2.moments(cnt) for cnt in contours0]
# Nota Bene: I rounded the centroids to integer.
centroids = [( int(round(m['m10']/m['m00'])),int(round(m['m01']/m['m00'])) ) for m in moments]
print 'cv2 version:', cv2.__version__
print 'centroids:', centroids
for c in centroids:
# I draw a black little empty circle in the centroid position
cv2.circle(img,c,5,(0,0,0))
cv2.imshow('image', img)
0xFF & cv2.waitKey()
cv2.destroyAllWindows()
另请参阅关于问题Python OpenCV的此答案https://stackoverflow.com/a/9059648/15485- 在二进制图像中查找黑色区域
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.