標籤:int odi write red math sop ide python實現 des
利用opencv+python實現以下功能:
1)擷取即時視頻,分解幀頻;
2)將視頻做二值化處理;
3) 將視頻做濾波處理(去除噪點,擷取準確輪廓個數);
4)識別映像輪廓;
5)計算質心;
6)描繪質心動態變化曲線;
# -*- coding: utf-8 -*-"""Created on Thu Apr 24 12:10:23 2018@author: irene"""import numpy as npimport matplotlib.pyplot as 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,contours,hierarchy = cv2.findContours(thresh, 3, 1) img = cv2.medianBlur(image,5) #進行中值濾波 cnt = contours[1] #選取其中的第一個輪廓,這幅映像只有兩個輪廓 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.plot(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實現視頻即時質心讀取