我使用opencv和kinect进行身体追踪和拘留。我使用下面的代码跟踪一个身体并生成一个数字。我的第一个虽然检测不同的pocisions是获得该区域,但后来我发现不同的位置可以有相同的区域。所以我想要获得图形的顶点以获得这些点之间的距离。或者你可以为这个问题推荐什么?
import cv2
import cv
import numpy as np
from freenect import sync_get_depth as get_depth, sync_get_video as get_video
import subprocess
import time
def distancia(depth):
depth = depth.sum()
if depth > 280000000 and depth < 290000000:
print 'Está dentro del rango'
time.sleep(2)
return True
else:
return False
def video(n):
video = "parole /home/carlos/Vídeos/Webcam/" + n + ".webm"
return_code = subprocess.call(video, shell=True)
def open_kinect():
# Parte de Kinect
(depth,_), (rgb,_) = get_depth(), get_video()
d3 = np.dstack((depth,depth,depth)).astype(np.uint8)
da = np.hstack((d3,rgb))
img = np.array(da[::2,::2,::-1])
#img2 = cv.fromarray()
return img, depth
def main():
#cap = cv2.VideoCapture(0)
while 1:
#return_code = subprocess('parole /home/carlos/Vídeos/1.mp4', shell=True)
#ret,img = cap.read()
img, depth = open_kinect()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
ret,thresh1 = cv2.threshold(blur,255,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# Eliminacion de Fondo
# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh1,cv2.MORPH_OPEN,kernel, iterations = 2)
contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
drawing = np.zeros(img.shape,np.uint8)
# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)
# Finding sure foreground area
#dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
dist_transform = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)
# Termina eliminacion de fondo
max_area=0
for i in range(len(contours)):
cnt=contours[i]
area = cv2.contourArea(cnt)
if(area>max_area):
max_area=area
ci=i
cnt=contours[ci]
hull = cv2.convexHull(cnt)
moments = cv2.moments(cnt)
if moments['m00']!=0:
cx = int(moments['m10']/moments['m00']) # cx = M10/M00
cy = int(moments['m01']/moments['m00']) # cy = M01/M00
centr=(cx,cy)
cv2.circle(img,centr,5,[0,0,255],2)
cv2.drawContours(drawing,[cnt],0,(0,255,0),2)
cv2.drawContours(drawing,[hull],0,(0,0,255),2)
cnt = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
hull = cv2.convexHull(cnt,returnPoints = False)
try:
defects = cv2.convexityDefects(cnt,hull)
mind=0
maxd=0
for i in range(defects.shape[0]):
s,e,f,d = defects[i,0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
dist = cv2.pointPolygonTest(cnt,centr,True)
cv2.line(img,start,end,[0,255,0],2)
cv2.circle(img,far,5,[0,0,255],-1)
print(i)
i=0
#Here i calculated the area of the different figures
time.sleep(1)
if distancia(depth):
print max_area
if max_area > 17400 and max_area < 18000:
video("1")
elif max_area > 18400 and max_area < 19000:
video("2")
elif max_area > 23100 and max_area < 236000:
video("3")
elif max_area > 20100 and max_area < 20500:
video("4")
elif max_area > 14000 and max_area < 14700:
video("5")
except Exception as e:
print 'Error', e
#raw_input()
cv2.imshow('output',drawing)
cv2.imshow('Original',img)
k = cv2.waitKey(10)
if k == 27:
break
main()
这是我得到的一个例子 https://drive.google.com/file/d/0B6AAm-ulsq0VTFNJSVRXMEZVTVk/view
这就是我想要的 https://drive.google.com/file/d/0B6AAm-ulsq0VWm43RDJLaFd1aFE/view?usp=sharing