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

OpenCV 图像的几何变换

一、图像缩放

1.API

cv2.resize(src, dsize, fx=0,fy=0,interpolation = cv2.INTER_LINEAR)

参数:

src :输入图像

dsize:绝对尺寸

fx,fy:相对尺寸

interpolation:插值方法

2.代码演示 

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
[rows, cols] = img.shape[:2]
res_1 = cv.resize(img, (2*cols, 2*rows), interpolation=cv.INTER_CUBIC)
cv.imshow('image', res_1)
cv.waitKey()
res_2 = cv.resize(img, None, fx=0.5, fy=0.5)
cv.imshow('image', res_1)
cv.waitKey()

二、图像平移

1.API

cv2.warpAffine(img, M, dsize)

参数:

img:输入图像

M:2×3移动矩阵,为np.float32类型

dsize:输出图像的大小

 2.代码演示 

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
[rows, cols] = img.shape[:2]
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('image', dst)
cv.waitKey()

三、图像旋转

1.API

cv2.getRotationMatrix2D(center, angle, scale)
cv.warpAffine()

参数:

center:旋转中心

angle:旋转角度

scale:缩放比例

返回值:

M:旋转矩阵 

2.代码演示 

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
[rows, cols] = img.shape[:2]
M = cv.getRotationMatrix2D((cols/2, rows/2), 120, 1)
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('image', dst)
cv.waitKey()

 四、仿射变换

1.API

cv2.getAffineTransform()
cv2.warpAffine()

2.代码演示

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
[rows, cols] = img.shape[:2]
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[100, 100], [200, 50], [100, 250]])
M = cv.getAffineTransform(pts1, pts2)
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('image', dst)
cv.waitKey()

五、透射变换

1.API

cv2.getPerspectiveTransform()
cv2.warpPerspective()

 2.代码演示

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
img = cv.resize(img, None, fx=0.5, fy=0.5)
[rows, cols] = img.shape[:2]
pts1 = np.float32([[56, 65], [368, 52], [28, 138], [389, 390]])
pts2 = np.float32([[100, 145], [300, 100], [80, 290], [310, 300]])
T = cv.getPerspectiveTransform(pts1, pts2)
dst = cv.warpPerspective(img, T, (cols, rows))
cv.imshow('image', dst)
cv.waitKey()

六、图像金字塔

1.API

cv2.pyrUp(img) #对图像进行上采样
cv2.pyrDown(img) #对图像进行下采样

2.代码演示

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('Genshin.jpeg', -1)
cv.imshow('image', img)
img = cv.pyrDown(img)
img = cv.pyrDown(img)
img = cv.pyrDown(img)
cv.imshow('image', img)
cv.waitKey()

 

相关文章:

  • redis中setnx命令的底层原理是什么
  • 三个表联合查询的场景分析-场景1:a表关联了b表和c表
  • 【Java基础】IO流(二)字符集知识
  • vue3路由跳转时,页面如何滚动到顶部
  • 如何更改ldap用户在local node上的默认shell
  • 从前端到前端框架
  • 专题二 - 滑动窗口 - leetcode 76. 最小覆盖子串 | 困难难度
  • Node.js入门基础—day01
  • Notepad++从文件夹查找文本内容
  • Vue2 + node.js项目
  • Apache Paimon 的 CDC Ingestion 概述
  • linux 安装gradle7.4.2环境
  • apache commons-dbcp Apache Commons DBCP 软件实现数据库连接池 commons-dbcp2
  • chatGPT的耳朵!OpenAI的开源语音识别AI:Whisper !
  • spring 面试题
  • 第三章 OpenGL ES 基础-基础-GLSL渲染纹理
  • 安卓通过termux部署ChatGLM
  • github(不是git啊)操作记录(踩坑)
  • Linux 自动检测进程是否存活,如果挂掉自动拉起
  • Nodejs 第五十四章(net)
  • 飙升至熔断,巴基斯坦股市两大股指收盘涨逾9%
  • 苹果或将于2027年推出由玻璃制成的曲面iPhone
  • 国家统计局今年将在全国开展两次人口固定样本跟访调查
  • “海豚音”依旧,玛丽亚·凯莉本周来沪开唱
  • 母亲节书单|关于生育自由的未来
  • 梅花奖在上海|朱洁静:穿越了人生暴风雨,舞台是最好良药