0x00. 圖片讀、寫和顯示操作
安裝好 OpenCV 之後,首先嚐試載入一張最簡單的圖片並顯示出來,程式碼示例:
第一種方式使用cv2.cv的LoadImage
、ShowImage
和SaveImage
函式
1 2 3 4 5 6 7 8 9 10 11 12 |
import cv2.cv as cv # 讀圖片 image=cv.LoadImage('img/image.png', cv.CV_LOAD_IMAGE_COLOR)#Load the image #Or just: image=cv.LoadImage('img/image.png') cv.NamedWindow('a_window', cv.CV_WINDOW_AUTOSIZE) #Facultative cv.ShowImage('a_window', image) #Show the image # 寫圖片 cv.SaveImage("thumb.png", thumb) cv.WaitKey(0) #Wait for user input and quit |
也可以直接使用cv2的imread
、imwrite
和imshow
函式
1 2 3 4 5 6 7 8 9 10 11 |
import numpy as np import cv2 img = cv2.imread('messi5.jpg',0) cv2.imshow('image',img) k = cv2.waitKey(0) if k == 27: # wait for ESC key to exit cv2.destroyAllWindows() elif k == ord('s'): # wait for 's' key to save and exit cv2.imwrite('messigray.png',img) cv2.destroyAllWindows() |
imread
函式還可以定義載入的mode,預設是以RGB模式處理圖片:
1 2 3 4 |
import cv2 grayImage = cv2.imread('MyPic.png', cv2.CV_LOAD_IMAGE_GRAYSCALE) # 可選引數CV_LOAD_IMAGE_COLOR (BGR), CV_LOAD_IMAGE_GRAYSCALE (grayscale), CV_LOAD_IMAGE_UNCHANGED(neither) cv2.imwrite('MyPicGray.png', grayImage) |
0x01. 獲取圖片屬性
1 2 3 4 5 6 7 8 9 |
import cv2 img = cv2.imread('img/image.png') print img.shape # (640, 640, 3) print img.size # 1228800 print img.dtype # uint8 # 在debug的時候,dtype很重要 |
0x02. 輸出文字
在處理圖片的時候,我們經常會需要把一些資訊直接以文字的形式輸出在圖片上,下面的程式碼將實現這個效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import cv2.cv as cv image=cv.LoadImage('img/lena.jpg', cv.CV_LOAD_IMAGE_COLOR) #Load the image font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8) #Creates a font y = image.height / 2 # y position of the text x = image.width / 4 # x position of the text cv.PutText(image,"Hello World !", (x,y),font, cv.RGB(255, 255, 255)) #Draw the text cv.ShowImage('Hello World', image) #Show the image cv.WaitKey(0) |
cv2:
1 |
cv2.putText(frame, 'Hello World', (300,100), 0, 0.5, (0,0,255),2) |
0x03. 縮放圖片
下面的例子將實現縮放圖片並儲存,這個在使用 OpenCV 做影象處理的時候都是很常用的操作:
1 2 3 4 5 6 7 8 9 10 |
import cv2.cv as cv im = cv.LoadImage("img/alkaline.jpg") #get the image thumb = cv.CreateImage((im.width / 2, im.height / 2), 8, 3) #Create an image that is twice smaller than the original cv.Resize(im, thumb) #resize the original image into thumb #cv.PyrDown(im, thumb) cv.SaveImage("thumb.png", thumb) # save the thumb image |
cv2:
1 2 3 4 5 6 7 8 9 |
import cv2 import numpy as np img = cv2.imread('messi5.jpg') res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC) #OR height, width = img.shape[:2] res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC) |
0x04. 影象平移
1 2 3 4 5 6 7 8 9 10 11 12 |
import cv2 import numpy as np img = cv2.imread('messi5.jpg',0) rows,cols = img.shape M = np.float32([[1,0,100],[0,1,50]]) dst = cv2.warpAffine(img,M,(cols,rows)) cv2.imshow('img',dst) cv2.waitKey(0) cv2.destroyAllWindows() |
0x05. 影象旋轉
1 2 3 4 5 |
img = cv2.imread('messi5.jpg',0) rows,cols = img.shape M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1) dst = cv2.warpAffine(img,M,(cols,rows)) |
0x06. 仿射變換
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import cv2 import numpy as np img = cv2.imread('mao.jpg') rows,cols,ch = img.shape pts1 = np.float32([[50,50],[200,50],[50,200]]) pts2 = np.float32([[10,100],[200,50],[100,250]]) M = cv2.getAffineTransform(pts1,pts2) dst = cv2.warpAffine(img,M,(cols,rows)) cv2.imshow('image',dst) cv2.waitKey(0) |
0x07. 影象顏色變換
實際使用過程中,我們經常也需要對一些圖片的顏色空間做一些改變之類的:
c2.cv:
1 2 3 4 5 6 7 8 9 |
import cv2.cv as cv im=cv.LoadImage('img/fruits.jpg',cv.CV_LOAD_IMAGE_COLOR) res = cv.CreateImage(cv.GetSize(im), cv.CV_8UC2, 3) #cv.CV_32F, cv.IPL_DEPTH_16S, ... cv.Convert(im, res) cv.ShowImage("Converted",res) res2 = cv.CreateImage(cv.GetSize(im), cv.CV_8UC2, 3) cv.CvtColor(im, res2, cv.CV_RGB2BGR) # HLS, HSV, YCrCb, .... cv.ShowImage("CvtColor", res2) cv.WaitKey(0) |
- cv.Convert():將圖片從一個顏色空間轉到另一個顏色空間
- cv.CvtColor(src, dst, code):
cv2:
cv2.cvtColor(input_image, flag)函式實現圖片顏色空間的轉換,flag 引數決定變換型別。如 BGR->Gray flag 就可以設定為 cv2.COLOR_BGR2GRAY 。
一個簡單的例子,下面的程式碼實現識別攝像視訊中藍色的部分:
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 |
import cv2 import numpy as np cap = cv2.VideoCapture(0) while(1): # 讀取視訊的每一幀 _, frame = cap.read() # 將圖片從 BGR 空間轉換到 HSV 空間 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 定義在HSV空間中藍色的範圍 lower_blue = np.array([110,50,50]) upper_blue = np.array([130,255,255]) # 根據以上定義的藍色的閾值得到藍色的部分 mask = cv2.inRange(hsv, lower_blue, upper_blue) res = cv2.bitwise_and(frame,frame, mask= mask) cv2.imshow('frame',frame) cv2.imshow('mask',mask) cv2.imshow('res',res) k = cv2.waitKey(5) |
以上的程式碼給出了視訊中獲取興趣物件的基本思想。
0x08. 通道的拆分/合併處理
對於一張圖片的 R、G、B 通道,我們可以很方便的使用 OpenCV 獲取並分離或者合併:
(這是將影象灰度化處理的一種方式)
cv2.cv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import cv2.cv as cv orig = cv.LoadImage('img/fruits.jpg') b = cv.CreateImage(cv.GetSize(orig), orig.depth, 1) g = cv.CloneImage(b) r = cv.CloneImage(b) cv.Split(orig, b, g, r, None) merged = cv.CreateImage(cv.GetSize(orig), 8, 3) cv.Merge(g, b, r, None, merged) cv.ShowImage("Image", orig) cv.ShowImage("Blue", b) cv.ShowImage("Green", g) cv.ShowImage("Red", r) cv.ShowImage("Merged", merged) cv.WaitKey(0) |
cv2
1 2 3 4 |
import cv2 img = cv2.imread('img/image.png') b,g,r = cv2.split(img) img = cv2.merge((b,g,r)) |
0x09. 圖片新增邊距
cv2.copyMakeBorder函式
1 2 3 4 5 6 7 8 9 10 11 12 |
import cv2 import numpy as np BLUE = [255,0,0] img1 = cv2.imread('opencv_logo.png') replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP) constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE) |