圖像處理(Getting Started with Images)
讀取圖像
顯示圖像
保存圖像
使用Matplotlib
視頻處理(Getting Started with Videos)
讀取攝像頭播放視頻
播放視頻文件
保存視頻文件
OpenCV畫圖函數(shù)
畫線
畫矩形
畫圓
畫橢圓
畫多邊形
添加文字
以上步驟最終圖像
鼠標作為畫圖刷
簡單示例
高級示例
移動條調(diào)色板
學習使用cv2.imread(), cv2.imshow() , cv2.imwrite()
這三個函數(shù),以及Matplotlib
庫顯示圖像。
cv2.imread(filename, flags=None)
import cv2img = cv2.imread(r'/home/luke/圖片/2018-08-09 11-58-52.jpg',cv2.IMREAD_COLOR)# filename參數(shù)u用于定位圖片,可以是當前路徑或絕對路徑,注意windows平臺,字符串前加 r# 第二項參數(shù)的默認值是:cv2.IMREAD_COLOR,讀取色彩圖像,還可以是# cv2.IMREAD_GRAYSCALE : 讀取灰度圖像# cv2.IMREAD_UNCHANGED : 讀取圖像包含 alpha channel
1
2
3
4
5
6
1
2
3
4
5
6
cv2.imshow()
函數(shù)將圖像顯示到窗口,窗口大小自動適應(yīng)圖像大小。
cv2.imshow('image',img)#參數(shù)1:窗口標題名稱#參數(shù)2:已讀取的顯示圖像cv2.waitKey(0)# cv2.waitKey()函數(shù)等待任意按鍵, # 參數(shù)是等待超時時間,時間單位為ms;參數(shù)為0時,等待直到任意鍵按下,# 返回值是按下的鍵值cv2.destroyAllWindows()# 銷毀所有打開的窗口123456789123456789
img = cv2.imread('/home/luke/圖片/2018-07-19 19-47-33屏幕截圖.png',0)cv2.imshow('image',img)k = cv2.waitKey(0)#k = cv2.waitKey(0) & 0xFFif k == 27: # wait for ESC key to exit cv2.destroyAllWindows()elif k == ord('s'): # wait for 's' key to save and exit cv2.imwrite('/home/luke/圖片/test.png',img) cv2.destroyAllWindows()
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
使用Matplotlib庫顯示圖像、縮放圖像,保存圖像,關(guān)于這個庫的說明,需要另寫文章。以下是簡單示例
import numpy as npimport cv2from matplotlib import pyplot as pltimg = cv2.imread('messi5.jpg',0)plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axisplt.show()# 注意:OpenCV圖像存儲格式為BGR模式,但是 Matplotlib 使用RGB模式;因此要想正確使用 Matplotlib庫必須先進行轉(zhuǎn)換。# convert BGR image to RGB image# cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# img2 = img[:,:,::-1]123456789101112123456789101112
主要學習函數(shù):
cv2.VideoCapture(),
cv2.VideoWriter()
要播放攝像頭視頻,需要先捕捉攝像頭視頻流,創(chuàng)造一個VideoCapture對象。
import numpy as npimport cv2cap = cv2.VideoCapture(0)#cap = cv2.VideoCapture(/dev/video0)#cap.read() returns a bool (True/False). If frame is read correctly, #it will be True. So you can check end of the video by checking this return valuewhile(True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break# When everything done, release the capturecap.release()cv2.destroyAllWindows()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cv2.VideoCapture()
用于創(chuàng)建VideoCapture對象,參數(shù)可以使video設(shè)備序號,從0開始,也可以是設(shè)備名稱/dev/video0
cap 對象代表的攝像頭節(jié)點可能還未被初始化,直接讀取可能會報錯可以使用cap.isOpened()
函數(shù)判斷,返回值為True,表示已打開;否則用cap.open()
函數(shù)打開。
cap.get(propId)函數(shù)可以獲取視頻相關(guān)參數(shù)
cap.set(propId, vaule),設(shè)置視頻相關(guān)參數(shù)
propId取值范圍是0-21
CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
CV_CAP_PROP_FPS Frame rate.
CV_CAP_PROP_FOURCC 4-character code of codec.
CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
CV_CAP_PROP_HUE Hue of the image (only for cameras).
CV_CAP_PROP_GAIN Gain of the image (only for cameras).
CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently)
CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently)
import numpy as npimport cv2cap = cv2.VideoCapture('vtest.avi')while(cap.isOpened()): ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): breakcap.release()cv2.destroyAllWindows()123456789101112123456789101112
注意:Make sure proper versions of ffmpeg or gstreamer is installed. Sometimes, it is a headache to work with Video Capture mostly due to wrong installation of ffmpeg/gstreamer.
以下代碼一遍播放一遍保存視頻文件
import numpy as npimport cv2cap = cv2.VideoCapture(0)# Define the codec and create VideoWriter objectfourcc = cv2.VideoWriter_fourcc(*'XVID')out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))while(cap.isOpened()): ret, frame = cap.read() if ret==True: frame = cv2.flip(frame,0) # write the flipped frame,垂直方向翻轉(zhuǎn) out.write(frame) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break# Release everything if job is finishedcap.release()out.release()cv2.destroyAllWindows()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cv2.VideoWriter()參數(shù)說明:
參數(shù)1:存儲的視頻文件名稱
參數(shù)2:視頻文件格式FourCC, 4-byte 的編碼格式,In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
In Windows: DIVX (More to be tested and added)
參數(shù)3:視頻文件的幀率大小,每秒多少幀
參數(shù)4:元組格式(640, 320),表示視頻文件畫面的大小
基本函數(shù)
cv2.line(),
cv2.circle() ,
cv2.rectangle(),
cv2.ellipse(),
cv2.putText()
基本參數(shù)
img : 被作圖的圖像載體
color : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
thickness : Thickness of the line or circle etc. If -1 is passed for closed figures like circles, it will fill the shape. default thickness = 1
lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv2.LINE_AA gives anti-aliased line which looks great for curves.
首先創(chuàng)建全黑圖像,然后給出起點與終點坐標,線的顏色、厚度
以下函數(shù)畫一條從左上角到右下角的藍色線
import numpy as npimport cv2# Create a black imageimg = np.zeros((512,512,3), np.uint8)# Draw a diagonal blue line with thickness of 5 pximg = cv2.line(img,(0,0),(511,511),(255,0,0),5)1234567812345678
聲明矩形的左上角坐標、右下角坐標
img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
1
1
聲明圓心點坐標、半徑、顏色
img = cv2.circle(img,(447,63), 63, (0,0,255), -1)11
橢圓參數(shù):
圓心坐標 (x,y)
a、b長短軸長度 (major axis length, minor axis length)
作圖方向,逆時針,起點角度與終點角度 0 到 360 g畫出完整橢圓
img = cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
1
1
多邊形參數(shù):
多邊形定點坐標
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)pts = pts.reshape((-1,1,2))img = cv2.polylines(img,[pts],True,(0,255,255))123123
注意:If third argument is False, you will get a polylines joining all the points, not a closed shape.
注意:cv2.polylines() can be used to draw multiple lines. Just create a list of all the lines you want to draw and pass it to the function. All lines will be drawn individually. It is more better and faster way to draw a group of lines than calling cv2.line() for each line.
向圖像添加文字需要聲明以下參數(shù)
文字內(nèi)容
文字起點坐標,起始位置為左下角
字體類型
字體大小
其它參數(shù):color, thickness, lineType, lineType = cv2.LINE_AA 推薦使用.
font = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)
1
2
1
2
學習函數(shù):cv2.setMouseCallback()
1、首先創(chuàng)建鼠標事件回調(diào)函數(shù),每一個鼠標事件給出一個坐標(x,y),根據(jù)此坐標與對應(yīng)的鼠標EVENT,灰回調(diào)函數(shù)可以實現(xiàn)任何事情
2、鼠標事件EVENT:['EVENT_FLAG_ALTKEY', 'EVENT_FLAG_CTRLKEY', 'EVENT_FLAG_LBUTTON', 'EVENT_FLAG_MBUTTON', 'EVENT_FLAG_RBUTTON', 'EVENT_FLAG_SHIFTKEY', 'EVENT_LBUTTONDBLCLK', 'EVENT_LBUTTONDOWN', 'EVENT_LBUTTONUP', 'EVENT_MBUTTONDBLCLK', 'EVENT_MBUTTONDOWN', 'EVENT_MBUTTONUP', 'EVENT_MOUSEHWHEEL', 'EVENT_MOUSEMOVE', 'EVENT_MOUSEWHEEL', 'EVENT_RBUTTONDBLCLK', 'EVENT_RBUTTONDOWN', 'EVENT_RBUTTONUP']
3、以下代碼,設(shè)置鼠標雙擊左鍵事件,以此事件的坐標點換一個圓
import cv2import numpy as np# mouse callback functiondef draw_circle(event,x,y,flags,param): if event == cv2.EVENT_LBUTTONDBLCLK: cv2.circle(img,(x,y),100,(255,0,0),-1)# Create a black image, a window and bind the function to windowimg = np.zeros((512,512,3), np.uint8)cv2.namedWindow('image')cv2.setMouseCallback('image',draw_circle)while(1): cv2.imshow('image',img) if cv2.waitKey(20) & 0xFF == 27: breakcv2.destroyAllWindows()123456789101112131415161718123456789101112131415161718
鼠標回調(diào)函數(shù)綁定多個事件,既可以畫圓也可以畫矩形
import cv2import numpy as npdrawing = False # true if mouse is pressedmode = True # if True, draw rectangle. Press 'm' to toggle to curveix,iy = -1,-1# mouse callback functiondef draw_circle(event,x,y,flags,param): global ix,iy,drawing,mode if event == cv2.EVENT_LBUTTONDOWN: drawing = True ix,iy = x,y elif event == cv2.EVENT_MOUSEMOVE: if drawing == True: if mode == True: cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1) else: cv2.circle(img,(x,y),5,(0,0,255),-1) elif event == cv2.EVENT_LBUTTONUP: drawing = False if mode == True: cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1) else: cv2.circle(img,(x,y),5,(0,0,255),-1)
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
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
主循環(huán)檢測“m”鍵,是否按下,用以切換畫圓還是畫矩形
鼠標左鍵按下使能畫圖,并拖動開始畫圖
img = np.zeros((512,512,3), np.uint8)cv2.namedWindow('image')cv2.setMouseCallback('image',draw_circle)while(1): cv2.imshow('image',img) k = cv2.waitKey(1) & 0xFF if k == ord('m'): mode = not mode elif k == 27: breakcv2.destroyAllWindows()1234567891011121312345678910111213
主要學習函數(shù)
cv2.createTrackbar()
cv2.getTrackbarPos(),參數(shù)1,trackbar名稱,參數(shù)2,附著窗口名稱,參數(shù)3,默認值,參數(shù)4,最大值,參數(shù)5,回調(diào)函數(shù),移動條value變化時出發(fā)執(zhí)行,此示例中,只需trackbar的value值,因此回調(diào)函數(shù)什么也不做
此示例有4個移動條,分別用來選擇R、G、B;以及On/Off選擇
trackbar的value取值只有0與1時,可以作為switch開關(guān),此示例中用來決定是否顯色調(diào)色的后的顏色,value為0 時,調(diào)色板為黑色
import cv2import numpy as npdef nothing(x): pass# Create a black image, a windowimg = np.zeros((300,512,3), np.uint8)cv2.namedWindow('image')# create trackbars for color changecv2.createTrackbar('R','image',0,255,nothing)cv2.createTrackbar('G','image',0,255,nothing)cv2.createTrackbar('B','image',0,255,nothing)# create switch for ON/OFF functionalityswitch = '0 : OFF \n1 : ON'cv2.createTrackbar(switch, 'image',0,1,nothing)while(1): cv2.imshow('image',img) k = cv2.waitKey(1) & 0xFF if k == 27: break # get current positions of four trackbars r = cv2.getTrackbarPos('R','image') g = cv2.getTrackbarPos('G','image') b = cv2.getTrackbarPos('B','image') s = cv2.getTrackbarPos(switch,'image') if s == 0: img[:] = 0 else: img[:] = [b,g,r]cv2.destroyAllWindows()
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
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
好了,本片就這么多,繼續(xù)學習!