close
項目 說明
定義 幾何變換是對圖像進行空間變換,改變像素的位置。
類型 - 平移<br>- 縮放<br>- 旋轉<br>- 扭曲<br>- 仿射變換<br>- 射影(透視)變換
優點 - 能夠調整圖像的視角、大小、方向。<br>- 用於圖像矯正、增強和特定效果。
缺點 - 可能會引入插值誤差。<br>- 不恰當的變換可能會導致圖像質量降低。

 

平移

import cv2
import numpy as np

# 讀取圖像
image = cv2.imread('path_to_image.jpg')

# 定義平移矩陣
M = np.float32([[1, 0, 50], [0, 1, 50]])
shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

cv2.imshow('Original', image)
cv2.imshow('Shifted', shifted)
cv2.waitKey(0)
cv2.destroyAllWindows()
 

旋轉

# 定義旋轉中心、角度和縮放比例
center = (image.shape[1] // 2, image.shape[0] // 2)
angle = 45
scale = 1.0

# 獲取旋轉矩陣
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

cv2.imshow('Original', image)
cv2.imshow('Rotated', rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
 

仿射變換

# 定義原始點和目標點
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])

# 獲取仿射矩陣
M = cv2.getAffineTransform(pts1, pts2)
affine_transformed = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

cv2.imshow('Original', image)
cv2.imshow('Affine Transformation', affine_transformed)
cv2.waitKey(0)
cv2.destroyAllWindows()
 

arrow
arrow
    文章標籤
    圖像前處理
    全站熱搜

    Rocky 發表在 痞客邦 留言(0) 人氣()