OpenCV-Python实战(8)——图像变换

一、缩放 cv2.resize()

img = cv2.resize(src=*,dsize=*,fx=*,fy=*,interpolation=*)

img:目标图像。

src:原始图像。

dsize:(width,height)图像大小。

fx、fy:可选参数,水平/垂直方向缩放比例。

interpolation:可选参数,进行缩放操作使用哪种方法对图像进行删减或增补,常用方法如下:

方法解释
INTER_NEAREST0最近插值法
INTER_LINEAR1双线性插值法
INTER_CUBIC2双三次插值法
INTER_AREA3
INTER_LENCZOS44Lencz的插值方法
import cv2

lena = cv2.imread('Lena.png')

img1 = cv2.resize(src=lena,dsize=(int(lena.shape[0]/2),int(lena.shape[1]/2)))
img2 = cv2.resize(src=lena,dsize=None,fx=.5,fy=0.5)
cv2.imshow('img1',img1)
cv2.imshow('img2',img2)

cv2.waitKey(0)
cv2.destroyAllWindows()

二、翻转 cv2.flip()

img = cv2.flip(src=*,flipCode=*)

img:目标图像。

src:原始图像。

flipCode:翻转方式:

flipCode解释
0垂直翻转
1水平翻转
-1垂直与水平同时翻转
import cv2

lena = cv2.imread('Lena.png')
lena = lena[::2,::2,:]

img1 = cv2.flip(src=lena,flipCode=0)
img2 = cv2.flip(src=lena,flipCode=1)
img3 = cv2.flip(src=lena,flipCode=-1)

cv2.imshow('lena',lena)
cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('img3',img3)

cv2.waitKey(0)
cv2.destroyAllWindows()

三、仿射 

M=\begin{bmatrix} M11 &M12 &M13 \\ M21 &M22 &M23 \end{bmatrix}

img(x,y)=src(M_{11}x + M_{12}y + M_{13} , M_{21}x + M_{22}y + M_{23})

img = cv2.warpAffine(src=*,M=*,dsize=*,flags=*,borderMode=*,borderValue=*)

img:目标图像。

src:原始图像。

M:3*2 变换矩阵,不同变换矩阵的仿射效果不同。

dsize:(width,height)新图像大小。

flags:进行仿射操作的插值方法。

borderMode:边界像素,默认为:BORDER_CONSTANT。

borderValue:边界填充值,默认为0。

3.1 平移

img(x,y)=src(1*x + 0*y + M_{13} , 0*x + 1*y + M_{23})

M=\begin{bmatrix} 1 &0 &M13 \\ 0 &1 &M23 \end{bmatrix}

表示图像向X轴方向平移 _{}M_{13},向Y轴方向平移 _{}M_{23}

import numpy as np

lena = cv2.imread('Lena.png')
lena = lena[::2,::2,:]

x = 25
y = 25
M = np.float32([[1,0,x],[0,1,y]])
img = cv2.warpAffine(src=lena,M=M,dsize=lena.shape[:2])

cv2.imshow('lena',lena)
cv2.imshow('img',img)

cv2.waitKey(0)
cv2.destroyAllWindows()

3.2 旋转 cv2.getRotationMatix2D()

M = cv2.getRotationMatrix2D(center=*,angle=*,scale=*)

center:旋转的中心点坐标(width,height)。

angle:旋转角度,正值(逆时针);负值(顺时针)。

scale:缩放比。

import cv2
import numpy as np

lena = cv2.imread('Lena.png')
lena = lena[::2,::2,:]

w,h = lena.shape[:2]
M = cv2.getRotationMatrix2D(center=(w/2,h/2),angle=30,scale=1)
img = cv2.warpAffine(src=lena,M=M,dsize=lena.shape[:2])

cv2.imshow('lena',lena)
cv2.imshow('img',img)

cv2.waitKey(0)
cv2.destroyAllWindows()

3.3 倾斜 cv2.getAffineTransform()

M = cv2.getAffineTransform(src=*,dst=*)

src:原始图像的三个定位点坐标。(可以是图像的任意三个角坐标)

dst:倾斜图像对应的三个定位点坐标。

import cv2
import numpy as np

lena = cv2.imread('Lena.png')
lena = lena[::2,::2,:]

w,h = lena.shape[:2]
src = np.float32([[0,0],[w-1,0],[w-1,h-1]])
dst = np.float32([[30,0],[w+29,0],[w-1,h-1]])
M = cv2.getAffineTransform(src=src,dst=dst)
dsize = (w+50,h)
img = cv2.warpAffine(src=lena,M=M,dsize=dsize)

cv2.imshow('lena',lena)
cv2.imshow('img',img)

cv2.waitKey(0)
cv2.destroyAllWindows()

 3.4 透视

透视相比于倾斜,定义了四个基准点,可以进行非平行变换。

M = cv2.getPerspectiveTransform(src=*,dst=*)
img = cv2.warpPerspective(src=*,M=*,dsize=*,flags=*,borderMode=*,borderValue=*)

src:原始图像的四个定位点坐标。(可以是图像的任意四个角坐标)

dst:倾斜图像对应的四个定位点坐标。

cv2.warpPerspective:的参数基本与 cv2.warpAffine 相同,只不过这里的 M:4*2 变换矩阵

import cv2
import numpy as np

lena = cv2.imread('Lena.png')
lena = lena[::2,::2,:]

w,h = lena.shape[:2]
src = np.float32([[0,0],[w-1,0],[w-1,h-1],[0,h-1]])
dst = np.float32([[10,0],[w-11,0],[w-20,h-1],[20,h-1]])
M = cv2.getPerspectiveTransform(src=src,dst=dst)
img = cv2.warpPerspective(src=lena,M=M,dsize=lena.shape[:2])

cv2.imshow('lena',lena)
cv2.imshow('img',img)

cv2.waitKey(0)
cv2.destroyAllWindows()

四、重映射 cv2.remap()

按照自定义方法执行映射,可以实现图像的翻转、扭曲、变形、或特定区域图片内容的改变。 

img = cv2.remap(src=*,map1=*,map2=*,interpolation=*,borderMode=*,borderValue=*)

img:目标图像。

src:原始图像。

map1、map2:用于存放 src 原始图像的 X 坐标、Y 坐标。

interpolation:标注插值方式,默认为:INRTER_LINEAR。

borderMode:边界像素,默认为:BORDER_CONSTANT。

borderValue:边界填充值,默认为0。

import cv2
import numpy as np

lena = cv2.imread('Lena.png')[::2,::2,:]

w,h = lena.shape[:2]
mapx = np.zeros(lena.shape[:2],np.float32)
mapy = np.zeros(lena.shape[:2],np.float32)

# 复制
for r in range(h):
    for c in range(w):
        mapx[r,c] = c
        mapy[r,c] = r
img1 = cv2.remap(src=lena,map1=mapx,map2=mapy,interpolation=cv2.INTER_LINEAR)

# 垂直翻转
for r in range(h):
    for c in range(w):
        mapx[r,c] = c
        mapy[r,c] = h-1-r
img2 = cv2.remap(src=lena,map1=mapx,map2=mapy,interpolation=cv2.INTER_LINEAR)

# 水平翻转
for r in range(h):
    for c in range(w):
        mapx[r,c] = w-1-c
        mapy[r,c] = r
img3 = cv2.remap(src=lena,map1=mapx,map2=mapy,interpolation=cv2.INTER_LINEAR)

# 垂直和水平翻转
for r in range(h):
    for c in range(w):
        mapx[r,c] = w-1-c
        mapy[r,c] = h-1-r
img4 = cv2.remap(src=lena,map1=mapx,map2=mapy,interpolation=cv2.INTER_LINEAR)

cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('img3',img3)
cv2.imshow('img4',img4)
cv2.waitKey(0)
cv2.destroyAllWindows()

import cv2
import numpy as np

lena = cv2.imread('Lena.png')[::2,::2,:]

w,h = lena.shape[:2]
mapx = np.zeros(lena.shape[:2],np.float32)
mapy = np.zeros(lena.shape[:2],np.float32)

# 缩小
for r in range(h):
    for c in range(w):
        mapx[r,c] = 2*c
        mapy[r,c] = 2*r
img1 = cv2.remap(src=lena,map1=mapx,map2=mapy,interpolation=cv2.INTER_LINEAR)[:int(w/2),:int(h/2),:]

cv2.imshow('lena',lena)
cv2.imshow('img1',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

理解(mapx,mapy)这两个二维矩阵重叠所构成的坐标 (width,height),对掌握 cv2.remap() 函数非常重要。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小码贾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值