命令列安裝方式 - mac
brew install python@3.9
pip3 install numpy matplotlib opencv_python
// numpy - 矩陣操作
// matplotlib - 顯示
// 測試是否安裝成功(import 無報錯資訊 表示安裝成功)
MacintoshdeMacBook-Pro:~ Jartin$ python3
Python 3.9.1 (default, Jan 8 2021, 17:15:36)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import matplotlib
>>> import cv2
import cv2;
# 建立視窗
cv2.namedWindow('new', cv2.WINDOW_NORMAL);
# WINDOW_AUTOSIZE 不可以resize
# WINDOW_NORMAL 可以resize
# 設定視窗大小
cv2.resizeWindow('new', 1920, 1080)
# 展示視窗
cv2.imshow('new', 0);
# 鍵盤和滑鼠的監聽 實現
key = cv2.waitKey(0)
if(key == 'q'):
exit()
# 銷燬所有視窗
cv2.destroyAllWindows();
import cv2;
# 圖片載入
cv2.namedWindow('img', cv2.WINDOW_NORMAL);
img = cv2.imread('./jobs.jpeg');
cv2.imshow('img', img)
key = cv2.waitKey(0)
if (key & 0xFF == ord('q')):
cv2.destroyAllWindows();
import cv2;
# 使用opencv 的 videocapture採集視訊資料
# 建立視窗
cv2.namedWindow('video', cv2.WINDOW_NORMAL);
cv2.resizeWindow('video', 640, 480);
# 獲取視訊裝置
cap = cv2.VideoCapture(0);
while True:
# 從攝像頭讀取視訊幀
ret, frame = cap.read();
# 將視訊幀在視窗中顯示
cv2.imshow('video', frame);
# 等待鍵盤事件,如果q,退出 waitKey(1)影格率越小 視訊越流暢
key = cv2.waitKey(1);
if (key & 0xFF == ord('q')):
break;
# 釋放VideoCapture
cap.release();
cv2.destroyAllWindows();
import cv2;
# 讀取視訊檔
# 建立視窗
cv2.namedWindow('video', cv2.WINDOW_NORMAL);
cv2.resizeWindow('video', 640, 480);
# 獲取視訊裝置 / 從視訊檔中讀取視訊幀
cap = cv2.VideoCapture(0);
cap = cv2.VideoCapture('/Users/Jartin/Desktop/opencv/zl.mp4');
while True:
# 從攝像頭讀取視訊幀
ret, frame = cap.read();
# 將視訊幀在視窗中顯示
cv2.imshow('video', frame);
# 等待鍵盤事件 如果q退出
key = cv2.waitKey(1);
if (key & 0xFF == ord('q')):
break;
# 釋放rediocap
cap.release();
cv2.destroyAllWindows();
import cv2;
# 視訊錄製
# 建立videowrite為寫多媒體檔案
fourcc = cv2.VideoWriter_fourcc(*'MJPG');
# 路徑、 forcc、影格率、解析度
vw = cv2.VideoWriter('./out.mp4', fourcc, 25, (1280, 720))
# 建立視窗
cv2.namedWindow('video', cv2.WINDOW_NORMAL);
cv2.resizeWindow('video', 640, 360);
# 獲取視訊裝置
cap = cv2.VideoCapture(0);
while True:
# 從攝像頭讀取資料
ret, frame = cap.read();
# 將視訊幀在視窗顯示
cv2.imshow('video', frame);
# 寫資料到多媒體檔案
vw.write(frame);
# 等待鍵盤事件 如果為q退出
key = cv2.waitKey(1);
if (key & 0xFF == ord('q')):
break;
# 釋放videocapture
cap.release();
# 釋放videowrite資源
vw.release();
cv2.destroyAllWindows();
import cv2;
import numpy as np;
# 控制滑鼠 滑鼠回撥函數
def mouse_callback(event, x, y, flags, userdata):
print(event, x, y, flags, userdata);
# 建立視窗
cv2.namedWindow('mouse', cv2.WINDOW_NORMAL);
cv2.resizeWindow('mouse', 640, 360);
# 設定滑鼠回撥
cv2.setMouseCallback('mouse', mouse_callback, '123');
# 顯示視窗和背景
# numpy圖片元件、np.zeros顯示圖片、(高, 寬, bgr3種陣列)、np.uint8畫素型別
img = np.zeros((360, 640, 3), np.uint8);
while True:
cv2.imshow('mouse', img);
key = cv2.waitKey(1);
if key & 0xFF == ord('q'):
break;
cv2.destroyAllWindows();
import cv2;
import numpy as np;
def callback():
pass;
# 建立視窗
cv2.namedWindow('trackbar', cv2.WINDOW_NORMAL);
# 建立trackbar 名字、視窗名字、預設當前值、最大值、回撥方法
cv2.createTrackbar('R', 'trackbar', 0, 255, callback);
cv2.createTrackbar('G', 'trackbar', 0, 255, callback);
cv2.createTrackbar('B', 'trackbar', 0, 255, callback);
# 建立圖片
img = np.zeros((480, 640, 3), np.uint8);
while True:
# 讀取值
r = cv2.getTrackbarPos('R', 'trackbar');
g = cv2.getTrackbarPos('G', 'trackbar');
b = cv2.getTrackbarPos('B', 'trackbar');
img[:] = [b, g, r];
cv2.imshow('trackbar', img);
key = cv2.waitKey(10);
if key & 0xFF == ord('q'):
break;
cv2.destroyAllWindows();
import cv2;
import numpy as np;
def callback():
pass;
# 建立視窗
cv2.namedWindow('trackbar', cv2.WINDOW_NORMAL);
# 建立trackbar 名字、視窗名字、預設當前值、最大值、回撥方法
cv2.createTrackbar('R', 'trackbar', 0, 255, callback);
cv2.createTrackbar('G', 'trackbar', 0, 255, callback);
cv2.createTrackbar('B', 'trackbar', 0, 255, callback);
# 建立圖片
img = np.zeros((480, 640, 3), np.uint8);
while True:
# 讀取值
r = cv2.getTrackbarPos('R', 'trackbar');
g = cv2.getTrackbarPos('G', 'trackbar');
b = cv2.getTrackbarPos('B', 'trackbar');
img[:] = [b, g, r];
cv2.imshow('trackbar', img);
key = cv2.waitKey(10);
if key & 0xFF == ord('q'):
break;
cv2.destroyAllWindows();
HSV Hue:色相、紅色、藍色、綠色
Saturation:飽和度,顏色的純度
Value:明度
YUV 視訊中常用
對畫素的描述 Y資料 UV彩色資訊
import cv2;
def callback():
pass;
cv2.namedWindow('color', cv2.WINDOW_NORMAL);
img = cv2.imread('./jobs.jpeg');
colorspaces = [cv2.COLOR_BGR2RGBA, cv2.COLOR_BGR2BGRA, cv2.COLOR_BGR2GRAY, cv2.COLOR_BGR2HSV, cv2.COLOR_BGR2YUV];
cv2.createTrackbar('curcolor', 'color', 0, 4, callback);
while True:
index = cv2.getTrackbarPos('curcolor', 'color');
# 顏色空間轉換API
cvt_img = cv2.cvtColor(img, colorspaces[index]);
cv2.imshow('color', img)
key = cv2.waitKey(10);
if key & 0xFF == ord('q'):
break;
cv2.destroyAllWindows();
import numpy as np
# np.zeros((行的個數, 列的個數, 通道數/層數), 矩陣中的資料型別);
c = np.zeros((480, 640, 3), np.uint8);
print(c)
矩陣的檢索與賦值
ROI [y1:y2,x1:x2]
Mat是什麼
是矩陣,可以理解為一張圖 有head 與 body組成
import cv2
import numpy as np
img = cv2.imread('./1.jpeg');
# 淺拷貝
img2 = img;
# 深拷貝
img3 = img.copy();
img[10:100, 10:100] = [0, 0, 255]
cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('img3', img3)
cv2.waitKey(0);
import cv2;
import numpy as numpy
img = cv2.imread('./1.jpeg')
# (1200, 1920, 3) 高度、長度、通道數
print(img.shape)
# 6912000 高度 * 長度 * 3
print(img.size)
# uint8 影象中每個元素的位深
print(img.dtype)
import cv2;
import numpy as np
# 通道的分離與合併
img = np.zeros((480, 640, 3), np.uint8)
b,g,r = cv2.split(img);
b[10:100, 10:100] = 255;
g[10:100, 10:100] = 255;
img2 = cv2.merge((b, g, r))
cv2.imshow('img', img);
cv2.imshow('b', b);
cv2.imshow('g', g);
cv2.imshow('img2', img2);
cv2.waitKey(0);
import cv2;
import numpy as np;
# 繪製直線
img = cv2.imread('./1.jpeg')
cv2.line(img, (10, 20), (10, 900), (0,0,255), 20, 4);
cv2.line(img, (100, 12), (400, 100), (0,0,255), 20, 14);
cv2.imshow('img', img);
key = cv2.waitKey(0);
import cv2;
import numpy as np;
img = cv2.imread('./1.jpeg');
# cv2.ellipse(img, (500, 500), )
# 畫圓
cv2.circle(img, (520, 440), 100, (0, 0, 255), 10)
cv2.circle(img, (520, 440), 5, (0, 0, 255), 10)
# 畫橢圓
# 度是按順時針計算的
cv2.ellipse(img, (520, 440), (100, 50), 15, 0, 360, (0, 0, 255), -1)
# 畫多邊形
pts = np.array(((320, 10), (150, 100), (450, 100)), np.int32)
cv2.polylines(img, [pts], True, (0, 0, 255))
# 填充
cv2.fillPoly(img, [pts], (255, 255, 0))
# 繪製文字
cv2.putText(img, 'Yulanlan', (800, 400), cv2.FONT_HERSHEY_PLAIN, 9, (255, 0, 0))
cv2.imshow('draw', img);
cv2.waitKey(0)
待續