当前位置:网站首页>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()

边栏推荐
- JS scroll div scroll bar to bottom
- Getting started with crawler to giving up 06: crawler play Fund (with code)
- Tool functions – get all files in the project folder
- 独立站运营中如何提升客户留存率?客户细分很重要!
- VR is destined to reappear in the Jianghu?
- os.path.join()使用过程中遇到的坑
- June 27, 2021: given a positive array arr, it represents the weight of several people
- 2022 tea artist (intermediate) work license question bank and online simulation examination
- The first exposure of Alibaba cloud's native security panorama behind the only highest level in the whole domain
- 新技术实战,一步步用Activity Results API封装权限申请库
猜你喜欢

Application of JDBC in performance test

Shader common functions

Swift Extension NetworkUtil(網絡監聽)(源碼)

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

Understanding of the concept of "quality"

一文带你了解Windows操作系统安全,保护自己的电脑不受侵害

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

Pat 1157: school anniversary

2021-03-04 COMP9021第六节课笔记

Blue Bridge Cup_ Queen n problem
随机推荐
js滚动div滚动条到底部
Tool functions – get all files in the project folder
将mysql的数据库导出xxx.sql,将xxx.sql文件导入到服务器的mysql中。项目部署。
Industrial computer anti cracking
[graduation season] Hello stranger, this is a pink letter
jwt(json web token)
Model effect optimization, try a variety of cross validation methods (system operation)
小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
LabVIEW查找n个元素数组中的质数
Swift Extension NetworkUtil(網絡監聽)(源碼)
Sql语句内运算问题
Transformers pretrainedtokenizer class
Getting started with crawler to giving up 06: crawler play Fund (with code)
2022茶艺师(中级)上岗证题库及在线模拟考试
MAYA重新拓布
2021-03-16 COMP9021第九节课笔记
C language_ Love and hate between string and pointer
Promise的使用场景
os. path. Pits encountered during the use of join()
2021-03-04 comp9021 class 6 notes