文章目錄
1、opencv讀取圖片
- cv2.imread()方法封裝了4個步驟,分別爲:
- 1、文件的讀取 2、封裝格式解析 3、數據解碼 4、數據加載
import cv2
# 1、文件的讀取 2、封裝格式解析 3、數據解碼 4、數據加載
img = cv2.imread('image1.jpg', 1) #1表示彩色圖片
#圖片展示
cv2.imshow('image', img)
#釋放資源
cv2.waitKey(0)
cv2.destroyAllWindows()
2、opencv保存圖片
import cv2
# 1、文件的讀取 2、封裝格式解析 3、數據解碼 4、數據加載
img = cv2.imread('image1.jpg', 1) #1表示彩色圖片
#圖片保存
# 1、jpg,png 文件頭 2、文件數據
cv2.imwrite('image1.png', img)#返回結果爲True,表示寫入成功
3、圖像質量壓縮
3.1、有損壓縮——jpg格式
- 參數cv2.IMWRITE_JPEG_QUALITY的壓縮範圍是0~100,有損壓縮以犧牲圖片像素爲前提
import cv2
img = cv2.imread('image1.jpg', 1)
cv2.imwrite('imageTest.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 0])
3.2、無損壓縮——png格式
- cv2.IMWRITE_PNG_COMPRESSION 壓縮比0~9
import cv2
img = cv2.imread('image1.jpg', 1)
cv2.imwrite('imageTest.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 0])#壓縮比0~9
總結: jpg的0 壓縮比高 0~100; png的0 壓縮比低 0 ~ 9
4、像素操作
4.1、像素值的讀取
import cv2
img = cv2.imread('image1.jpg', 1)
#採集圖片座標爲(100,100)的像素點,返回方式爲元組
(b, g, r) = img[100, 100]
print (b, g, r)
232 240 240
4.2、像素值的寫入
import cv2
img = cv2.imread('image1.jpg', 1)
# 繪製一條(100,100) ————> (100,600)的堅狀紅線
for i in range(1,500):
img[100+i, 100] = (0, 0, 255)#和座標系方向相反,[100+i, 100]=[y, x]
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
5、圖片縮放
5.1、查看圖片寬高
import cv2
img = cv2.imread('image1.jpg', 1)
imgInfo = img.shape#返回值爲圖片的高,寬和顏色組成
print (imgInfo)
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
print ("圖片的高度=%s,寬度=%s,顏色組成爲:%s"%(height, width, mode))
(736, 1022, 3)
圖片的高度=736,寬度=1022,顏色組成爲:3
5.2、等比例縮放
- 寬高各縮放一半
5.2.1、直接定義比率
detHeight = int(height*0.5)
detWidth = int(width*0.5)
newSet = cv2.resize(img, (detWidth, detHeight))
cv2.imshow('image', newSet)
cv2.waitKey(0)
cv2.destroyAllWindows()
5.2.2、使用cv2.warpAffine()方法映射
import cv2
import numpy as np
img = cv2.imread('image1.jpg', 1)
cv2.imshow('src', img)
# 獲取圖片寬高
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
matScale = np.float32([[0.5,0,0],[0,0.5,0]])#2行3列矩陣;橫向移動100px,縱向移動200px
#cv2.warpAffine()實現原像素的映射
dst = cv2.warpAffine(img, matScale, (int(width//2), int(height//2)))# 1 data; 2 mat; 3 info
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
5.2、非等比例縮放
- 一共有4種:最近臨域插值、雙線性插值、像素關係重採樣、立方插值
- 默認是雙線性插值
5.2.1、最近臨域插值
- src=(10 Х 20) ——> dst=(5 Х 10)
- (1,2) <—— (2,4)
- newX = x*(src行 / dst行) ——> newX = 1 * (10/5) = 2
- newY = y*(src列 / dst列) ——> newY = 1 * (20/10) = 4
import cv2
import numpy as np
img = cv2.imread('image1.jpg', 1)
imgInfo = img.shape#返回值爲圖片的高,寬和顏色組成
print (imgInfo)
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
print ("圖片的高度=%s,寬度=%s,顏色組成爲:%s"%(height, width, mode))
detHeight = int(height/2)
detWidth = int(width/2)
dstImage = np.zeros((detHeight, detWidth, 3), np.uint8)#創建一個畫布
for i in range(0, detHeight):#行
for j in range(0, detWidth):#列
iNew = int(i*(height* 1.0/detHeight))
jNew = int(j*(width* 1.0/detWidth))
dstImage[i, j] = img[iNew, jNew]
cv2.imwrite('./newImage.jpg', dstImage)
cv2.imshow('dst',dstImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
5.2.2、雙線性插值
- A1 = 20% X 上 + 80% x 下; A2 = 20% X 上 + 80% x 下;
- B1 = 30% X 左 + 70% x 右; B2 = 20% X 左 + 80% x 右;
- 最終點1 = A1 x 30% + A2 x 70%
- 最終點2 = B1 x 20% + B2 x 80%
6、圖片剪切
import cv2
img = cv2.imread('image1.jpg', 1)
detImage = img[100:200, 100:300]#剪切像素點
cv2.imshow('CutImage', detImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
7、圖片位移
7.1、使用opencv方法位移圖片
import cv2
import numpy as np
img = cv2.imread('image1.jpg', 1)
cv2.imshow('src', img)
# 獲取圖片寬高
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
matShift = np.float32([[1,0,100],[0,1,200]])#2行3列矩陣;橫向移動100px,縱向移動200px
#cv2.warpAffine()實現原像素的映射
dst = cv2.warpAffine(img, matShift, (height, width))# 1 data; 2 mat; 3 info
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
7.2、使用數組方法位移圖片
import cv2
import numpy as np
img = cv2.imread('image1.jpg', 1)
cv2.imshow('src', img)
# 獲取圖片寬高
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros(img.shape, np.uint8)#定義一個空數組
for i in range(0, height):
for j in range(0, width-100):
dst[i, j+100] = img[i, j]
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
8、圖片鏡像
實現步驟:
- 創建一個足夠大的”畫板
- 將一副圖像分別從前向後、 從後向前繪製
- 繪製中心分割線
import cv2
import numpy as np
img = cv2.imread('image2.jpg', 1)
cv2.imshow('src', img)
# 獲取圖片寬高
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
#定義新圖片的信息
newImgInfo = (height*2, width, deep)
dst = np.zeros(newImgInfo, np.uint8)#定義一個空數組,作爲畫布大小
# 向畫布寫入像素點
for i in range(0, height):
for j in range(0, width):
dst[i, j] = img[i, j]#繪製上面一張圖片
#繪製下面張圖片,高正好是上面移動像素的一半,寬保持不變;y = 2*h-i-1
dst[height*2-i-1,j] = img[i,j]
for i in range(0, width):
dst[height, i] = (0,0,255)#在圖片中間畫一條紅線
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
9、仿射變換
- 仿射變換包括:位移、旋轉、縮放、變形
9.1、圖片旋轉
9.2、圖片變形
- 要領:操作圖片的三個角(左上角,左下角,右上角)變化
- cv2.getAffineTransform()完成組合
- cv2.warpAffine()完成映射
import cv2
import numpy as np
img = cv2.imread('image2.jpg', 1)
cv2.imshow('src', img)
# 獲取圖片寬高
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
# src 3個角 ——> dst 3個角 (左上角,左下角,右上角)
matSrc = np.float32([[0,0], [0,height-1], [width-1,0]])
matDst = np.float32([[50,50], [200,height-100], [width-100,50]])
# 仿射變換矩陣——>組合
matAffine = cv2.getAffineTransform(matSrc, matDst)# 1 src,2 dst
dst = cv2.warpAffine(img, matAffine,(width,height))
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()