OpenCV圖像的幾何變換處理方法詳解
一、圖像縮放
1.API
cv2.resize(src, dsize, fx=0,fy=0,interpolation = cv2.INTER_LINEAR)
參數(shù):
①src :輸入圖像
②dsize:絕對(duì)尺寸
③fx,fy:相對(duì)尺寸
④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移動(dòng)矩陣,為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) #對(duì)圖像進(jìn)行上采樣 cv2.pyrDown(img) #對(duì)圖像進(jì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) 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PyCharm搭建Spark開發(fā)環(huán)境實(shí)現(xiàn)第一個(gè)pyspark程序
這篇文章主要介紹了PyCharm搭建Spark開發(fā)環(huán)境實(shí)現(xiàn)第一個(gè)pyspark程序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06python?playwright?庫(kù)上傳和下載操作(自動(dòng)化測(cè)試?playwright)
這篇文章主要介紹了python?playwright?庫(kù)上傳和下載操作(自動(dòng)化測(cè)試?playwright?),playwright中的上傳和下載比selenium的上傳和下載要簡(jiǎn)便些,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05python fuzzywuzzy模塊模糊字符串匹配詳細(xì)用法
這篇文章主要介紹了使用Python完成公司名稱和地址的模糊匹配的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08python異步的ASGI與Fast Api實(shí)現(xiàn)
本文主要介紹了python異步的ASGI與Fast Api實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07運(yùn)用Python快速的對(duì)MySQL數(shù)據(jù)庫(kù)進(jìn)行重命名
本文介紹了如何運(yùn)用Python快速的對(duì)現(xiàn)有的數(shù)據(jù)庫(kù)進(jìn)行重命名,有此需求的朋友可以參考下2021-06-06Jupyter notebook設(shè)置背景主題,字體大小及自動(dòng)補(bǔ)全代碼的操作
這篇文章主要介紹了Jupyter notebook設(shè)置背景主題,字體大小及自動(dòng)補(bǔ)全代碼的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04打包FlaskAdmin程序時(shí)關(guān)于static路徑問(wèn)題的解決
近期寫了個(gè)基于Flask-admin的數(shù)據(jù)庫(kù)管理程序,通過(guò)pyinstaller打包,給別人用,經(jīng)過(guò)幾次嘗試,打包的數(shù)據(jù)一直找不到static里面的樣式文件,查閱資料后,最總把問(wèn)題搞定了。寫下處理流程,供后來(lái)人參考2021-09-09Python OpenCV實(shí)現(xiàn)圖片上輸出中文
這篇文章主要為大家詳細(xì)介紹了Python OpenCV實(shí)現(xiàn)圖片上輸出中文,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01