我正在使用Asus xtion pro live cam。而我实际上是使用深度图像来检测球。更具体的openni2 ros包我订阅了/ camera / depth_registered / image主题。
在下图中,您可以看到2个球,但只检测到其中一个 用opencv查找轮廓算法....
1)为什么只能通过查找轮廓检测到一个球?
2)有没有人知道像71这样的深度注册值是否意味着距离是0.71米?或者如何将灰度值转换为以米为单位的距离?
3)如果有人对代码有一些很好的改进,请评论:)谢谢!
这是我当前的python / opencv2示例代码:
使用 python depth_detection.py启动代码2_balls.jpg
import cv2
import numpy as np
import sys
print "{} {}".format("Analyze this file: ", sys.argv[1])
im = cv2.imread(sys.argv[1])
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imshow('image',imgray)
cv2.waitKey(50)
# Thresholding - delete all points outside 60cm - 150cm
ret, thresh = cv2.threshold(imgray, 60, 150, 0)
cv2.imshow('thresh',thresh)
cv2.waitKey(50)
kernel = np.ones((7,7),np.uint8)
erosion = cv2.erode(thresh, kernel, iterations = 4)
dilation = cv2.dilate(erosion, kernel, iterations = 4)
cv2.imshow('dilation', dilation)
cv2.waitKey(50)
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print len(contours)
cv2.drawContours(im, contours, -1, (255,255,0), 3)
cv2.imshow('contours',im)
cv2.waitKey(50)
(x,y),radius = cv2.minEnclosingCircle(contours[8])
center = (int(x),int(y))
px = im[x, y]
print "Center Position in pixel: {} distance_value: {}".format(center, px)
radius = int(radius)
cv2.circle(im,center,radius,(255,0,0),2)
cv2.circle(im,center,1,(255,0,255),2)
cv2.imshow('result',im)
cv2.waitKey(5000)
cv2.destroyAllWindows()
PS:我想要发现的第二个球直接位于第一个被检测到的球后面:)