opencv4 python 版本_Opencv4 with Python3.6
最近在做一個手指追蹤,涉及到opencv。一開始選了個基于nodejs的opencv項目,不過環(huán)境配置項目的examples死活無法運行,可能和我macOS自己升級了catalina測試版有關(guān)。不過倒是了解了不少關(guān)于npm的用法(不過感覺npm還是沒有pip方便)。
那就還是回歸熟悉的Python吧。
關(guān)于Python版本的opencv,有個很棒的tutorial。地址
Opencv4在Python3.6上的安裝
這部分網(wǎng)上很多博客教程用Homebrew裝,之后又是一堆復雜的操作,創(chuàng)建軟鏈接什么的。其實如果只想要Python支持opencv十分簡單。
安裝基礎功能模塊:
1pip install opencv-python
安裝contrib modules:
1pip install opencv-contrib-python
具體可以參考
之后嘗試import cv2,如果成功就代表安裝成功。
cv2的Video相關(guān)
VideoCapture()
1.cap = cv2.VideoCapture(0)
VideoCapture()中參數(shù)是0,表示打開筆記本的內(nèi)置攝像頭,參數(shù)是視頻文件路徑則打開視頻,如cap = cv2.VideoCapture("../test.avi")
2.ret, frame = capture.read()
1
2
3
4
5
6
7
8
9
10
11# 打開攝像頭并灰度化顯示
import cv2
capture = cv2.VideoCapture(0)
while(True):
# 獲取一幀
ret, frame = capture.read()
# 將這幀轉(zhuǎn)換為灰度圖
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) == ord('q'):
break`
3.cap.get(propId) 獲取視頻屬性
4.cap.set(propId,value)設置視頻屬性。
至于這邊的propId是視頻屬性的id,分為18個不同的屬性,詳情參考上面的官方文檔
VideoWriter()
ffmpeg視頻編碼里面適用于macOS的有對應mp4的'm', 'p', '4', 'v'和對應avi的'M','J','P','G'
同時設置的時候frame_width和frame_height用get方法獲取,自己定義具體數(shù)值,可能錄下的文件無法正常播放。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43import cv2
import numpy as np
# Create a VideoCapture object
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Unable to read camera feed")
# Default resolutions of the frame are obtained.The default resolutions are system dependent.
# We convert the resolutions from float to integer.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
while(True):
ret, frame = cap.read()
if ret == True:
# Write the frame into the file 'output.avi'
out.write(frame)
# Display the resulting frame
cv2.imshow('frame',frame)
# Press Q on keyboard to stop recording
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture and video write objects
cap.release()
out.release()
# Closes all the frames
cv2.destroyAllWindows()
總結(jié)
以上是生活随笔為你收集整理的opencv4 python 版本_Opencv4 with Python3.6的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据模型与决策_数据模型与决策复习资料拿
- 下一篇: websocket python爬虫_p