当前位置: 首页 > article >正文

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()

3.5 重映射 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() 函数非常重要。


http://www.kler.cn/a/454895.html

相关文章:

  • 探索基金聚合平台的背景与发展:Finanzen.net、Franklin Templeton、Finect
  • STM32完全学习——FATFS0.15移植SD卡
  • AI客服机器人如何帮助企业应对高峰期客户咨询压力?
  • LeetCode--239.滑动窗口最大值(使用双端队列解决)
  • docker拉取rabbitmq镜像时报Client.Timeout exceeded while awaiting header错误处理办法
  • Linux-Ubuntu之串口通信
  • ChatGPT助力数据可视化与数据分析效率的提升(二)
  • lua debug相关方法详解
  • leetcode82:删除链表中的重复元素II
  • 【蓝桥杯】走迷宫
  • 题海拾贝:蓝桥杯 2020 省AB 乘法表
  • 免费资源网站
  • ANSYS EMC Plus:谐振腔中的天线
  • Markdown语法字体字号讲解
  • git revert
  • 【C#】WPF设置Separator为垂直方向
  • (icml2024)SLAattention,基于原文时序模型进行改进
  • 【AIGC篇】AIGC 引擎:点燃创作自动化的未来之火
  • 项目报 OutOfMemoryError 、GC overhead limit exceeded 问题排查以及解决思路实战
  • LeetCode 热题 100_二叉树的中序遍历(36_94_简单_C++)(二叉树;递归(中序遍历);迭代)