標籤:
接著上篇,這次寫兩個主題:
- OpenCV中的色彩空間轉換
- OpenCV中的幾何變換
OpenCV中的色彩空間轉換
色彩空間有許多種,常用有RGB,CMY,HSV,HSI等. 我們平時說的三通道的圖就是指的是RGB的圖.
貼一段百度百科的介紹:
RGB(紅綠藍)是依據人眼識別的顏色定義出的空間,可表示大部分顏色。但在科學研究一般不採用RGB色彩空間,因為它的細節難以進行數字化的調整。它將色調,亮度,飽和度三個量放在一起表示,很難分開。它是最通用的面向硬體的彩色模型。該模型用於彩色監視器和一大類彩色視頻攝像
CMY是工業印刷採用的色彩空間。它與RGB對應。簡單的類比RGB來源於是物體發光,而CMY是依據反射光得到的。具體應用如印表機:一般採用四色墨盒,即CMY加黑色墨盒
HSV,HSI兩個色彩空間都是為了更好的數字化處理顏色而提出來的。有許多種HSX色彩空間,其中的X可能是V,也可能是I,依據具體使用而X含義不同。H是色調,S是飽和度,I是強度
對於影像處理上,常見的色彩空間轉換就是兩種:
- RGB—>Gray
- RGB—>HSV
轉換API:
cv2.cvtColor(input_image,flag),falg是轉換類型
代碼:
__author__ = ‘gavinzhou‘# -*- coding: utf-8 -*-import cv2# read the original imagesim = cv2.imread("./images/1.png")if len(im.shape) == 3: print "original image is an RGB image"else: print "original image is an gray image"# convert to grayif len(im.shape) == 3: im_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)# convert to HSVif len(im.shape) == 3: im_hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)# show the imagescv2.imshow("ori", im)cv2.imshow("gray", im_gray)cv2.imshow("hsv", im_hsv)cv2.waitKey(8000)cv2.destroyAllWindows()
結果:
這個圖的右下角是我的案頭哈,不是顯示的圖
OpenCV中的幾何變換
這個部分的化就比較難了,如果大家只是想實作類別似於映像翻轉之類的,大家可以不用看這個,直接使用PIL
,裡面有簡單的實現,直接調用就行了,不需要看這個
如果你還想實現點更強大點的功能,那就有必要看看接下來的了
空間變換對應矩陣的仿射變換。一個座標通過函數變換的新的座標位置:
就是原始的點座標(x,y)
,經過轉換以後,新的座標是(x‘,y‘)
所以,實質上幾何變換就是點的變換,對應到矩陣就是仿射變換
舉個例子:
把映像向右移動兩個像素,其實就是將原始的像素的位置,比如(x,y)
,變為(x+2,y+2)
,這樣就是映像整體向右移動了兩個像素
下面,以2x2
的矩陣來說明問題
平移變換
對原始的座標來說,(x,y)
變換為(x‘,y‘)
,所以變換矩陣及逆矩陣:
縮放變換
將映像橫座標放大(或縮小)sx倍,縱座標放大(或縮小)sy倍,變換矩陣及逆矩陣:
旋轉變換
映像繞原點逆時針旋轉a角,其變換矩陣及逆矩陣(順時針選擇)為:
代碼:
__author__ = ‘gavinzhou‘# -*- coding: utf-8 -*-import cv2import numpy as npfrom matplotlib import pyplot as plt# read the original imagesimg = cv2.imread("./images/1.png")rows, cols, channels = img.shaperes = cv2.resize(img, (cols/2, rows/2))#Translation:# 1.shiftM_shift = np.float32([[1, 0, 100], [0, 1, 50]])img_shift = cv2.warpAffine(img, M_shift, (cols, rows))# 2.rotateM_rotate = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 1)img_rotate = cv2.warpAffine(img, M_rotate, (cols, rows))# 3.affinepts1 = np.float32([[50, 50], [200, 50], [50, 200]])pts2 = np.float32([[10, 100], [200, 50], [100, 250]])M_affine = cv2.getAffineTransform(pts1,pts2)img_affine = cv2.warpAffine(img, M_affine, (cols, rows))# 4.perspectivepts3 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])pts4 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])M_perspective = cv2.getPerspectiveTransform(pts3, pts4)img_perspective = cv2.warpPerspective(img, M_perspective, (cols, rows))print ‘shift:\n‘, M_shiftprint ‘rotate:\n‘, M_rotateprint ‘affine:\n‘, M_affineprint ‘perspective:\n‘, M_perspectiveplt.subplot(231), plt.imshow(img), plt.title(‘src‘)plt.subplot(232), plt.imshow(res), plt.title(‘scale‘)plt.subplot(233), plt.imshow(img_shift), plt.title(‘shift‘)plt.subplot(234), plt.imshow(img_rotate), plt.title(‘rotate‘)plt.subplot(235), plt.imshow(img_affine), plt.title(‘affine‘)plt.subplot(236), plt.imshow(img_perspective), plt.title(‘perspective‘)plt.show()
結果:
console列印結果:
shift:[[ 1. 0. 100.] [ 0. 1. 50.]]rotate:[[ 6.12323400e-17 1.00000000e+00 7.70000000e+01] [ -1.00000000e+00 6.12323400e-17 4.73000000e+02]]affine:[[ 1.26666667 0.6 -83.33333333] [ -0.33333333 1. 66.66666667]]perspective:[[ 1.05587376e+00 9.18151097e-02 -6.50969128e+01] [ 4.69010049e-02 1.12562412e+00 -7.57920240e+01] [ 1.83251448e-04 5.13337001e-04 1.00000000e+00]]
Python-OpenCV(6)