当前位置:网站首页>Opencv实现图像的基本变换
Opencv实现图像的基本变换
2022-06-24 06:58:00 【Keep_Trying_Go】
1.图像缩放
函数
resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None):
src:输入的图片 ;
dsize:缩放的目标尺寸大小;
dst:输入图片;
fx:x轴的缩放因子;
fy:y轴的缩放因子;
interpolation:插值算法;
插值算法:
(1)INTER_NEAREST:邻近插值算法,速度快,效果差;
(2)INTER_LINEAR:双线性插值(使用周围的四个像素作为参考);
(3)INTER_CURBIC:三次插值(使用周围的十六个像素作为参考);
(4)INTER_AREA:效果最好;
1.图像缩放
函数
resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None):
src:输入的图片 ;
dsize:缩放的目标尺寸大小;
dst:输入图片;
fx:x轴的缩放因子;
fy:y轴的缩放因子;
interpolation:插值算法;
插值算法:
(1)INTER_NEAREST:邻近插值算法,速度快,效果差;
(2)INTER_LINEAR:双线性插值(使用周围的四个像素作为参考);
(3)INTER_CURBIC:三次插值(使用周围的十六个像素作为参考);
(4)INTER_AREA:效果最好;
def Resize():
img=cv2.imread('images/lenna.png')
#resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None):
#src:输入的图片 dsize:缩放的目标尺寸大小 dst:输入图片 fx:x轴的缩放因子 fy:y轴的缩放因子 interpolation:插值算法
dst=cv2.resize(src=img,dsize=(500,500))
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

2.图像翻转
flip(src, flipCode, dst=None):
Src:输入的原始图像;
flipCode:翻转的方式;
Dst:输出图像;
flipCode==0表示上下翻转;
flipCode>0表示左右翻转;
flipCode<0表示上下+左右翻转;
def Rotate():
img=cv2.imread('images/lenna.png')
dst=cv2.flip(src=img,flipCode=0)
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

3.图像的旋转
rotate(src, rotateCode, dst=None):
Src:输入的原始图像;
rotateCode:旋转的方式;
Dst:输出图像;
旋转方式:
ROTATE_90_CLOCKWISE:顺时针旋转90度;
ROTATE_180:旋转180度;
ROTATE_90_COUNTERCLOCKWISE:逆时针旋转90度;
def Rotate():
img = cv2.imread('images/lenna.png')
dst = cv2.rotate(src=img,rotateCode=cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

4.仿射变换获取矩阵
(1)仿射变换:图像的旋转,缩放,平移的总称;
warpAffine(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None):
Src:输入的原始图像;
M:变换矩阵;
Dsize:输出的图像大小;
Flags:与resize中的插值算法一致;
borderMode:边界外推法标志;
borderValue:填充边界的值;
Dst:输出图像;
矩阵的平移:
(1)矩阵中的每个像素值都是由(x,y)组成;
(2)其变换矩阵为2x2的矩阵(单位阵);
(3)平移向量为2x1的向量;,所以平移矩阵为2x3的矩阵;
def WrapAffine():
img = cv2.imread('images/lenna.png')
#进行横向的平移,不进行竖向的平移
M=np.float32([[1,0,100],[0,1,0]])
dst = cv2.warpAffine(src=img,M=M,dsize=(450,450),flags=cv2.INTER_LINEAR)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
进行横向的平移,不进行竖向的平移
获取变换矩阵
getRotationMatrix2D(center, angle, scale):
Center:旋转中心点;
Angle:旋转角度(逆时针旋转);
Scale:缩放比例;
def GetRotationMatrix2D():
img = cv2.imread('images/lenna.png')
# 进行横向的平移,不进行竖向的平移
M=cv2.getRotationMatrix2D(center=(300,300),angle=45,scale=1.0)
dst = cv2.warpAffine(src=img, M=M, dsize=(450, 450), flags=cv2.INTER_LINEAR)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

获得变换矩阵
getAffineTransform(src, dst)
Src:三个点确定输入变换的位置;
Dst:三个点确定输出变换的位置;
def GetAffineTransform():
img = cv2.imread('images/lenna.png')
#输入图像的三个坐标点
src=np.float32([[100,100],[100,300],[100,300]])
#输出图像的三个坐标点
dst=np.float32([[200,200],[300,350],[350,450]])
M=cv2.getAffineTransform(src=src,dst=dst)
dst = cv2.warpAffine(src=img, M=M, dsize=(450, 450), flags=cv2.INTER_LINEAR)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

边栏推荐
- pyQt 常用系统的事件
- The article takes you to understand the security of Windows operating system and protect your computer from infringement
- More than observation | Alibaba cloud observable suite officially released
- Teach you how to use the reflect package to parse the structure of go - step 1: parameter type check
- 1279_VMWare Player安装VMWare Tools时VSock安装失败解决
- 【毕业季】你好陌生人,这是一封粉色信笺
- Analysis of abnormal problems in domain name resolution in kubernetes
- C language_ Love and hate between string and pointer
- Future trends in automated testing
- Dart development server, do I have a fever?
猜你喜欢

2022茶艺师(中级)上岗证题库及在线模拟考试

C language_ Love and hate between string and pointer

问题3 — messageBox弹框,修改默认背景色

Simple summary of lighting usage

OC extension detects whether an app is installed on the mobile phone (source code)

Synthesize video through ffmpeg according to m3u8 file of video on the network

根据网络上的视频的m3u8文件通过ffmpeg进行合成视频

Utilisation de la fermeture / bloc de base SWIFT (source)

Écouter le réseau d'extension SWIFT (source)

直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)
随机推荐
Pagoda panel installation php7.2 installation phalcon3.3.2
Optimization and practice of Tencent cloud EMR for cloud native containerization based on yarn
Vscode topic recommendation
05-ubuntu安装mysql8
longhorn安装与使用
Shader common functions
Question 1: the container that holds the most water
Industrial computer anti cracking
Solution of electric education system for intelligent supervision station
Getting started with crawler to giving up 06: crawler play Fund (with code)
Installation and use of selenium IDE
Analysis of abnormal problems in domain name resolution in kubernetes
Simple refraction effect
2022 mobile crane driver special operation certificate examination question bank and online simulation examination
Four models of iPhone 13 series have been exposed, and indeed, they are 13 fragrant!
12--合并两个有序链表
2021-03-11 comp9021 class 8 notes
51单片机_外部中断 与 定时/计数器中断
Swift extension chainlayout (UI chain layout) (source code)
搜索与推荐那些事儿