The following features are implemented with Opencv+python:
1) Get real-time video, decompose frame frequency;
2) The video to do two value processing;
3) The Video filter processing (to remove noise, to obtain the exact number of profiles);
4) identify image contour;
5) Calculate the centroid;
6) depict the dynamic change curve of centroid;
#-*-Coding:utf-8-*-"" "Created on Thu Apr 12:10:23 2018@author:irene" "" Import NumPy as Npimport Matplotlib.pyplot a s pltfrom scipy.interpolate import spline import math as Mtimport Cv2cap = Cv2. Videocapture (' 1.avi ') #读入视频c =1plt.figure (figsize= (8,8), dpi=80) AA =[]bb =[]cc =[] #uing = Np.logspace ( -3,2,121) while ( Cap.isopened ()): ret, frame = Cap.read () #分解为一帧一帧图像 if ret = = True: #cv2. Imshow ("frame", image) Img=cv2.cvtcolor (Frame,cv2. Color_bgr2gray) #彩色转灰度 # Print (frame) ret,thresh= cv2.threshold (img,127,255,0) #二值化 Image,co Ntours,hierarchy = Cv2.findcontours (Thresh, 3, 1) img = Cv2.medianblur (image,5) #进行中值滤波 cnt = contours[1] #选取其中的第一个轮廓, this image has only two profiles M = cv2.moments (CNT) cx=int (m["M10"]/m["m00"]) #计算质心 cy=int (m["M01"]/m[" M00 "]) cv2.drawcontours (img,contours,-1, (0,255,0), 2) Cv2.circle (IMG, (Cx,cy), 7, (255,255,255),-1) Cv2.puttext (IMG, "", (cx-20,cy-20), Cv2. font_hershey_simplex,0.5, (255,255,255), 2) cv2.imshow ("IMG", IMG) cv2.imwrite (' img/' +str (c) + '. jpg '), frame) #存储为图像 # for U in Uing:aa.append (CX) bb.append (CY) Cc.append (c) # PLT.P Lot (c,cx, ' K ') #plt. Plot (c,cx,color= ' Red ', linewidth=2.5,linestyle= ': ') # Plt.plot (c,cx, ' k^ ') #plt. Plot (C,cy, ' Yo: ') c = c+1 Else:break # cv2.imshow (' frame ', gray) #显示标记后的图像q If Cv2.waitkey (1) & 0xFF = = Ord (' q '): Break Cap.release () cv2.destroyallwindows () C1=np.var (AA) C2=np.var (BB) c1_1=c1/720*2.3*mt.pi/180c1_2=c2/512*2.3*mt.pi/180print (c1_1) print (c1_2) plt.plot (CC,AA) plt.show () Plt.plot (CC,BB) plt.show ()
Opencv+python realization of video real-time centroid reading