OpenCV圖像的幾何變換處理方法詳解
一、圖像縮放
1.API
cv2.resize(src, dsize, fx=0,fy=0,interpolation = cv2.INTER_LINEAR)
參數(shù):
①src :輸入圖像
②dsize:絕對尺寸
③fx,fy:相對尺寸
④interpolation:插值方法
2.代碼演示
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
[rows, cols] = img.shape[:2]
res_1 = cv.resize(img, (2*cols, 2*rows), interpolation=cv.INTER_CUBIC)
cv.imshow('image', res_1)
cv.waitKey()
res_2 = cv.resize(img, None, fx=0.5, fy=0.5)
cv.imshow('image', res_1)
cv.waitKey()二、圖像平移
1.API
cv2.warpAffine(img, M, dsize)
參數(shù):
①img:輸入圖像
②M:2×3移動矩陣,為np.float32類型
③dsize:輸出圖像的大小
2.代碼演示
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
[rows, cols] = img.shape[:2]
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('image', dst)
cv.waitKey()
三、圖像旋轉(zhuǎn)
1.API
cv2.getRotationMatrix2D(center, angle, scale) cv.warpAffine()
參數(shù):
①center:旋轉(zhuǎn)中心
②angle:旋轉(zhuǎn)角度
③scale:縮放比例
返回值:
M:旋轉(zhuǎn)矩陣
2.代碼演示
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
[rows, cols] = img.shape[:2]
M = cv.getRotationMatrix2D((cols/2, rows/2), 120, 1)
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('image', dst)
cv.waitKey()

四、仿射變換
1.API
cv2.getAffineTransform() cv2.warpAffine()
2.代碼演示
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
[rows, cols] = img.shape[:2]
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[100, 100], [200, 50], [100, 250]])
M = cv.getAffineTransform(pts1, pts2)
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('image', dst)
cv.waitKey()

五、透射變換
1.API
cv2.getPerspectiveTransform() cv2.warpPerspective()
2.代碼演示
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
img = cv.resize(img, None, fx=0.5, fy=0.5)
[rows, cols] = img.shape[:2]
pts1 = np.float32([[56, 65], [368, 52], [28, 138], [389, 390]])
pts2 = np.float32([[100, 145], [300, 100], [80, 290], [310, 300]])
T = cv.getPerspectiveTransform(pts1, pts2)
dst = cv.warpPerspective(img, T, (cols, rows))
cv.imshow('image', dst)
cv.waitKey()

六、圖像金字塔
1.API
cv2.pyrUp(img) #對圖像進行上采樣 cv2.pyrDown(img) #對圖像進行下采樣
2.代碼演示
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
img = cv.pyrDown(img)
img = cv.pyrDown(img)
img = cv.pyrDown(img)
cv.imshow('image', img)
cv.waitKey()

總結(jié)
到此這篇關(guān)于OpenCV圖像的幾何變換處理的文章就介紹到這了,更多相關(guān)OpenCV圖像幾何變換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python for循環(huán)remove同一個list過程解析
這篇文章主要介紹了python for循環(huán)remove同一個list過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
僅用50行Python代碼實現(xiàn)一個簡單的代理服務(wù)器
這篇文章主要介紹了僅用50行Python代碼實現(xiàn)一個簡單的代理服務(wù)器,利用最簡單的client->proxy->forward原理在socket模塊下編寫,需要的朋友可以參考下2015-04-04
Python BentoML構(gòu)建部署和管理機器學習模型技巧掌握
BentoML是一個開源的Python框架,旨在簡化機器學習模型的打包、部署和管理,本文將深入介紹BentoML的功能和用法,提供詳細的示例代碼和解釋,幫助你更好地理解和應用這個強大的工具2024-01-01
Python異常對象Exception基礎(chǔ)類異常捕捉
這篇文章主要為大家介紹了Python異常對象異常捕捉及Exception基礎(chǔ)類,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Python?UserWarning:?Glyph?missing?from?font(s)?DejaVu?
這篇文章主要介紹了Python?UserWarning:?Glyph?missing?from?font(s)?DejaVu?Sans警告及解決方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2026-01-01

