opencv+python實現視頻即時質心讀取

來源:互聯網
上載者:User

標籤: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實現視頻即時質心讀取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.