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

边栏推荐
- dhcp、tftp基础
- Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)
- 13 -- remove invalid parentheses
- Question 4 - datepicker date selector, disabling two date selectors (start and end dates)
- WPS的JS宏实现图片正文在同一段落的分离方法
- "Adobe international certification" about Adobe Photoshop, creating and modifying brush tutorials?
- VsCode主题推荐
- 将mysql的数据库导出xxx.sql,将xxx.sql文件导入到服务器的mysql中。项目部署。
- 2022年制冷与空调设备运行操作上岗证题库及模拟考试
- SQL intra statement operation
猜你喜欢
随机推荐
How to use the virtual clock of FPGA?
About the iframe anchor, the anchor is offset up and down, and the anchor has page display problems Srcdoc problem of iframe
贷款五级分类
1279_ Vsock installation failure resolution when VMware player installs VMware Tools
LabVIEW finds prime numbers in an array of n elements
Getting started with crawler to giving up 06: crawler play Fund (with code)
普通token
Future trends in automated testing
2021-03-11 comp9021 class 8 notes
RCNN、Fast-RCNN、Faster-RCNN介绍
问题3 — messageBox弹框,修改默认背景色
[graduation season] Hello stranger, this is a pink letter
Blue Bridge Cup_ Queen n problem
C language_ Love and hate between string and pointer
VR is destined to reappear in the Jianghu?
Pagoda panel installation php7.2 installation phalcon3.3.2
Application of JDBC in performance test
You get in Anaconda
App Startup
C语言_字符串与指针的爱恨情仇









