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

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 cv2lena = 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 cv2lena = 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 nplena = 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 nplena = 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 nplena = 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 nplena = 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 nplena = 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] = cmapy[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] = cmapy[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-cmapy[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-cmapy[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 nplena = 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*cmapy[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.lryc.cn/news/512724.html

相关文章:

  • 存储进阶笔记(二):Linux 存储栈:从 Device Mapper、LVM 到文件系统(2024)
  • Linux(Centos 7.6)基础命令/常用命令说明
  • 超详细!一文搞定PID!嵌入式STM32-PID位置环和速度环
  • 【Goland】怎么执行 go mod download
  • 服务器主机测试网络
  • 【JMeter详解】
  • Maven Wrapper 报错“未找到有效的 Maven 安装”
  • 如何通过 360 驱动大师检查自己电脑上的显卡信息
  • C++并发:线程管控
  • C++ 设计模式:策略模式(Strategy Pattern)
  • SpringBoot(Ⅱ-2)——,SpringBoot版本控制,自动装配原理补充(源码),自动导包原理补充(源码),run方法
  • 爬虫的工作原理
  • 你了解DNS吗?
  • 利用JavaScript实现顺序九宫格抽奖
  • 音视频入门知识(四):封装篇
  • 在基于IMX6ULL的Linux嵌入式编程中,与内存相关的堆(Heap)和栈(Stack)有什么区别?Linux 系统中堆和栈的内存布局是怎么样的?
  • Sealos Devbox 基础教程:使用 Cursor 从零开发一个 One API 替代品
  • pthread.h互斥锁与原子操作
  • 网络基础入门到深入(3):网络协议-HTTP/S
  • Git的.gitignore文件详解与常见用法
  • UniApp 组件的深度运用
  • k8s部署nginx+sshd实现文件上传下载
  • Spring-Mybatis 2.0
  • Linux 的历史与发展:从诞生到未来
  • SQL Server实现将分组的其他字段数据拼接成一条数据
  • 学习笔记 --C#基础其他知识点(同步和异步)
  • 一维、线性卡尔曼滤波的例程(MATLAB)
  • 极品飞车6的游戏手柄设置
  • FreeRTOS Lwip Socket APi TCP Server 1对多
  • 逆袭之路(11)——python网络爬虫:原理、应用、风险与应对策略