#需要部署模块: import cv [as别名]
#或: from cv import CvtColor [as别名]
defrun(self ) :
while True:
t1=time.time (
# blurthesourceimagetoreducecolornoise
cv.smooth(img,img,cv.CV_BLUR,3 );
#转换主题同步(hue,Saturation,Value ) so its
# easiertodeterminethecolortotrack (hue ) )。
HSV _ img=cv.create image (cv.getsize (img ),8,3 ) ) ) ) ) ) ) )。
cv.cvtcolor(img,hsv_img,cv.CV_BGR2HSV )。
# limitallpixelsthatdon ' tmatchourcriteria,in this case we are
# lookingforpurplebutifyouwantyoucanadjustthefirstvaluein
# bothturpleswhichisthehuerange (120,140 ).OpenCV uses 0-180 as
#a hue range for the HSV color model
threshold ed _ img=cv.create image (cv.getsize (HSV _ img ),8,1 ) ) ) ) ) ) ) )。
#白色
敏感性=10
cv .适当的心情(hsv_img,) 0,0,255-sensitivity,) 255,sensitivity,255 ),thresholded_img ) ) )。
# Red
#cv .适当的心情(hsv_img,(0,150,0 ),() 5,255,thresholded_img ) ) ) ) ) ) ) ) )。
#蓝色
#cv .适当的心情(hsv_img,) 100,50,50 ),) 140,255,255 ),thresholded_img () ) ) ) ) ) ) )。
#绿色
#cv .适当的心情(hsv_img,) 40,50,50 ),) 80,255,255 ),thresholded_img ) ) ) ) ) )。
# determinetheobjectsmomentsandcheckthattheareaislarge
#enough to be our object
moments=cv.moments(mat,0 ) )。
area=cv.HP DSB (运动,0,0 ) ) ) ) ) ) ) )。
# therecanbenoiseinthevideosoignoreobjectswithsmallareas
if(area10000 ) :
# determinethexandycoordinatesofthecenteroftheobject
# wearetrackingbydividingthe 1,0 and 0,1 momentsbythearea
x=cv .令人担心的栗子(moments,1,0 )/area
x=int(round ) x )
# createanoverlaytomarkthecenterofthetrackedobject
(cv.circle(Overlay,) x,y ),2,),255,255 ),20 ) ) ) ) ) ) )
cv.add(img,overlay,img ) )。
# addthethresholdedimagebacktotheimgsowecanseewhatwas
#left after it was applied
t2=time.time (
cv.merge(Thresholded_img,None,None,None,img ) )。
打印'检测时间=% GS x=% d,y=% d ' % (运行(T2-t1,3 ),x,y ) ) ) ) ) ) )
#display the image
cv.show image (color _ tracker _ window,img ) )。
ifcv.waitkey(10 )==27:
布雷克