OpenCV+TensorFlow 人工智能圖像處理 (2)

1. 圖片縮放

# 1. 加載圖片
# 2. 獲取圖片信息(寬度,高度)
# 3. 調用OpenCV的resize方法,進行圖片的縮放
# 4. 檢查最終的結果
import cv2
img = cv2.imread('images/image0.jpg', 1)
imgInfo = img.shape
print (imgInfo)
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2] # 顏色的儲存方式,由3種顏色(bgr)組成
# 放大, 縮小, 等比縮放 非等比縮放
dstHeight = int(height*0.5) # 目標高度
dstWidth = int(width*0.5)
# 四種resize: 最近鄰域插值 雙線性插值 像素關係重採樣 立方插值
dst = cv2.resize(img, (dstWidth, dstHeight))
cv2.imshow("image", dst)
cv2.waitKey(0)
(547, 730, 3)

-1

2. 最近鄰域插值、雙線性插值原理

  • 最近鄰域插值 原圖像:10*20 目標圖像: 5*10 目標圖像的像素來源於原圖像 舉例: 目標圖像(1, 2)來源於原圖像(2, 4) 如何計算: newX = 原圖x*(原圖像的行/目標圖像的行) newY = 原圖y*(原圖像的列/目標圖像的列) 比如目標圖像的第一列的第一個點,來源於原圖像的第一列的二個點(1* (10/5) = 2) 目標圖像(2, 3)點,來源於(4, 6)
  • 雙線性插值 A1 = 20%上 + 80%下 A2 B1 = 30%上 + 70%下 B2
# 最近鄰域插值
# 1. 獲取圖片信息
# 2. 創建一個空白模板,與預期目標大小一樣
# 3. 計算對應的像素
import cv2
import numpy as np
img = cv2.imread('images/image0.jpg', 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dstHeight = int(height/2)
dstWidth = int(width/2)
dstImage = np.zeros((dstHeight, dstWidth, 3), np.uint8) # 0-255
for i in range(0, dstHeight): # 行
    for j in range(0, dstWidth): # 列
        iNew = int(i*(height*1.0/dstHeight))
        jNew = int(j*(width*1.0/dstWidth))
        dstImage[i, j] = img[iNew, jNew]

cv2.imshow('dstImage', dstImage)
cv2.waitKey(0)
1114089

2. 圖片剪切

# 剪切(100, 100) -> (200, 300)
import cv2
img = cv2.imread('images/image0.jpg', 1)
dst = img[100:200, 100:300]
# 將藍紅通道設爲0
dst[:, :, [0, 2]] = 0
cv2.imshow("image", dst)
cv2.waitKey(0)
-1

3. 圖片移位

import cv2
import numpy as np
img = cv2.imread('images/image0.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
height, width = imgInfo[0], imgInfo[1]
# 進行矩陣的移位
matShift = np.float32([[1, 0, 100], [0, 1, 200]]) # 2*3
dst = cv2.warpAffine(img, matShift, (height, width)) # 矩陣映射,原圖, 移位矩陣, 圖片的高度和寬度
cv2.imshow('dst', dst)
cv2.waitKey(0)
1048603

[[1, 0, 100], [0, 1, 200]] 轉變爲2個矩陣: [[1, 0], [0, 1]] 和 [[100], [200]] 分別對應A和B矩陣,原圖像爲C[x, y] A * C + B = [[1x+0y], [0x+1y]] + [[100], [200]]

# 算法原理實現圖片移位
import cv2
import numpy as np
img = cv2.imread('images/image0.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
dst = np.zeros(img.shape, np.uint8)
height, width = imgInfo[0], imgInfo[1]
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)
-1

4. 圖片鏡像

# 1. 創建一個足夠大的畫板
# 2. 將一副圖像從前向後,從後向前繪製
# 3. 繪製中心分割線
import cv2
import numpy as np
img = cv2.imread('images/image0.jpg', 1)
imgInfo = img.shape
height, width, deep = imgInfo[0], imgInfo[1], imgInfo[2]
newImgInfo = (height*2, width, deep)
dst = np.zeros(newImgInfo, np.uint8)
for i in range(height):
    for j in range(width):
        dst[i, j] = img[i, j]
        # 繪製鏡像部分, x軸不變, y = 2*h - y - 1
        dst[height*2-i-1, j] = img[i, j]
for i in range(width):
    dst[height, i] = (0, 0, 255)
cv2.imshow('image', dst)
cv2.waitKey(0)
-1

5. 圖片縮放

# 縮放1倍
import cv2
import numpy as np
img = cv2.imread('images/image0.jpg', 1)
imgInfo = img.shape
height, width = imgInfo[0], imgInfo[1]
matScale = np.float32([[0.5, 0, 0], [0, 0.5, 0]])
dst = cv2.warpAffine(img, matScale, (int(width/2), int(height/2)))
cv2.imshow('dst', dst)
cv2.waitKey(0)
-1

6. 仿射變換

import cv2
import numpy as np
img = cv2.imread('images/image0.jpg', 1)
imgInfo = img.shape
height, width = imgInfo[0], imgInfo[1]
# 3點確定一個平面,分別是左上角, 左下角, 右上角
matSrc = np.float32([[0, 0], [0, height], [width, 0]])
matDst = np.float32([[50, 50], [300, height-200], [width-300, 100]])
# 組合
matAffine = cv2.getAffineTransform(matSrc, matDst) # 獲取矩陣組合, 1: 描述原矩陣的三點, 2: 目標矩陣的三點
dst = cv2.warpAffine(img, matAffine, (width, height))
cv2.imshow('image', dst)
cv2.waitKey(0)

7. 旋轉圖片

import cv2
import numpy as np
img = cv2.imread('images/image0.jpg', 1)
imgInfo = img.shape
height, width = imgInfo[0], imgInfo[1]
# 定義旋轉矩陣
matRotate = cv2.getRotationMatrix2D((height*0.5, width*0.5), 45, 1) # 得到旋轉矩陣, 1 旋轉中心店, 2 旋轉角度, 3 縮放係數
dst = cv2.warpAffine(img, matRotate, (width, height))
cv2.imshow('image', dst)
cv2.waitKey(0)
-1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章