Python3+OpenCV2實(shí)現(xiàn)圖像的幾何變換(平移、鏡像、縮放、旋轉(zhuǎn)、仿射)
前言
總結(jié)一下最近看的關(guān)于opencv圖像幾何變換的一些筆記.
這是原圖:

1.平移
import cv2
import numpy as np
img = cv2.imread("image0.jpg", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
dst = np.zeros(imgInfo, np.uint8)
for i in range( height ):
for j in range( width - 100 ):
dst[i, j + 100] = img[i, j]
cv2.imshow('image', dst)
cv2.waitKey(0)
demo很簡(jiǎn)單,就是將圖像向右平移了100個(gè)像素.如圖:

2.鏡像
import cv2
import numpy as np
img = cv2.imread('image0.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
height= imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
dst = np.zeros([height*2, width, deep], np.uint8)
for i in range( height ):
for j in range( width ):
dst[i,j] = img[i,j]
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)
demo生成一個(gè)如下效果:

3.縮放
import cv2
img = cv2.imread("image0.jpg", 1)
imgInfo = img.shape
print( imgInfo )
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
# 1 放大 縮小 2 等比例 非等比例
dstHeight = int(height * 0.5)
dstWeight = int(width * 0.5)
# 最近鄰域插值 雙線性插值 像素關(guān)系重采樣 立方插值
dst = cv2.resize(img, (dstWeight,dstHeight))
print(dst.shape)
cv2.imshow('image', dst)
cv2.waitKey(0)
使用resize直接進(jìn)行縮放操作,同時(shí)還可以使用鄰域插值法進(jìn)行縮放,代碼如下:
# 1 info 2 空白模板 3 重新計(jì)算x, y
import cv2
import numpy as np
img = cv2.imread('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)
for i in range( dstHeight ):
for j in range(dstWidth):
iNew = i * ( height * 1.0 / dstHeight )
jNew = j * ( width * 1.0 / dstWidth )
dstImage[i,j] = img[int(iNew),int(jNew)]
cv2.imshow('image', dstImage)
cv2.waitKey(0)
4.旋轉(zhuǎn)
import cv2
img = cv2.imread('image0.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
height= imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
# 定義一個(gè)旋轉(zhuǎn)矩陣
matRotate = cv2.getRotationMatrix2D((height*0.5, width*0.5), 45, 0.7) # mat rotate 1 center 2 angle 3 縮放系數(shù)
dst = cv2.warpAffine(img, matRotate, (height, width))
cv2.imshow('image',dst)
cv2.waitKey(0)
旋轉(zhuǎn)需要先定義一個(gè)旋轉(zhuǎn)矩陣,cv2.getRotationMatrix2D(),參數(shù)1:需要旋轉(zhuǎn)的中心點(diǎn).參數(shù)2:需要旋轉(zhuǎn)的角度.參數(shù)三:需要縮放的比例.效果如下圖:

5.仿射
import cv2
import numpy as np
img = cv2.imread('image0.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
height= imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
# src 3 -> dst 3 (左上角, 左下角,右上角)
matSrc = np.float32([[0,0],[0,height-1],[width-1, 0]]) # 需要注意的是 行列 和 坐標(biāo) 是不一致的
matDst = np.float32([[50,50],[100, height-50],[width-200,100]])
matAffine = cv2.getAffineTransform(matSrc,matDst) #mat 1 src 2 dst 形成組合矩陣
dst = cv2.warpAffine(img, matAffine,(height, width))
cv2.imshow('image',dst)
cv2.waitKey(0)
需要確定圖像矩陣的三個(gè)點(diǎn)坐標(biāo),及(左上角, 左下角,右上角).定義兩個(gè)矩陣,matSrc 為原圖的三個(gè)點(diǎn)坐標(biāo),matDst為進(jìn)行仿射的三個(gè)點(diǎn)坐標(biāo),通過(guò)cv2.getAffineTransform()形成組合矩陣.效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn)
本文主要介紹了Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
基于Python實(shí)現(xiàn)簡(jiǎn)單的漢字拼音轉(zhuǎn)換工具
將漢字轉(zhuǎn)為拼音,可以用于批量漢字注音、文字排序、拼音檢索文字等常見(jiàn)場(chǎng)景?,F(xiàn)在互聯(lián)網(wǎng)上有許多拼音轉(zhuǎn)換工具,基于Python的開(kāi)源模塊也不少,本文將利用pypinyin模塊制作簡(jiǎn)單的漢字拼音轉(zhuǎn)換工具,感興趣的可以了解一下2022-09-09
python字典中items()函數(shù)用法實(shí)例
Python字典items()函數(shù)作用以列表返回可遍歷的(鍵, 值)元組數(shù)組,下面這篇文章主要給大家介紹了關(guān)于python字典中items()函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2022-11-11
python對(duì)list中的每個(gè)元素進(jìn)行某種操作的方法
今天小編就為大家分享一篇python對(duì)list中的每個(gè)元素進(jìn)行某種操作的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
PyTorch實(shí)現(xiàn)FedProx聯(lián)邦學(xué)習(xí)算法
這篇文章主要為大家介紹了PyTorch實(shí)現(xiàn)FedProx的聯(lián)邦學(xué)習(xí)算法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

