目录
图片链接
跟着峰哥学计算机视觉所有需要用到的图片-深度学习文档类资源-CSDN下载
介绍
放射变换
镜像
将所有图像的x和y的横纵坐标平移。
镜像
旋转
透视变换
图像加法
图像减法
缩放
图像缩小
图像放大
图像腐蚀
图像膨胀
图像开运算
图像闭运算
形态学梯度
礼帽运算
黑帽运算
代码
图像翻转
"""
图像的翻转(镜像操作)
"""
import cv2
img = cv2.imread("../data/Linus.png")
cv2.imshow("img",img)
# 0-->垂直镜像
flip0 = cv2.flip(img,0)
cv2.imshow("flip0",flip0)
# 1-->水平镜像
flip1 = cv2.flip(img,1)
cv2.imshow("flip1",flip1)
cv2.waitKey()
cv2.destroyAllWindows()
垂直反转
水平镜像
仿射变换
"""
图像的仿射变换(平移与旋转)
"""
import numpy as np
import cv2
def translate(img, x, y):
"""
图像平移
:param img: 原始图像
:param x: 水平方向上平移的像素
:param y: 垂直方向平移的像素
:return: 平移之后的图形
"""
h, w = img.shape[:2] # 取出图像的形状属性
# 形状属性(高度,宽度,通道数) 通道数不切,取出1和2
# 定义平移矩阵
M = np.float32([[1, 0, x],
[0, 1, y]])
#调用warpAffine函数实现平移变换
shifted = cv2.warpAffine(img, # 原始图像
M, # 平移矩阵
(w, h)) # 输出图像的大小
return shifted
if __name__ == '__main__':
# 图像平移
img = cv2.imread("../data/Linus.png")
cv2.imshow("img", img)
#向下移动50个元素
shifted = translate(img,0,50)
cv2.imshow("shifted_1",shifted)
cv2.waitKey()
cv2.destroyAllWindows()
# 向左平移40个像素,向下平移40个像素
res2 = translate(img, -40, 40)
cv2.imshow("res2", res2)
import numpy as np
import cv2
def ratate(img, angle, center=None,
scale=1.0): # 缩放的大小
"""
图像旋转
:param img: 原始图像
:param angle: 旋转角度
:param center: 旋转中心
:param scale: 缩放比例
:return: 旋转后的图像
"""
h, w = img.shape[:2] # 获取图像的高度、宽度
if center is None: # 默认旋转中心为图像的中心
center = (w / 2, h / 2) # 宽度除以2,高度除以2
# 生成旋转矩阵
M = cv2.getRotationMatrix2D(center, # 中心
angle, # 角度
scale) # 缩放的比例
# 调用warpAffine函数实现旋转变换
res = cv2.warpAffine(img, M, (w, h))
return res
if __name__ == '__main__':
# 图像平移
img = cv2.imread("../data/Linus.png")
cv2.imshow("img", img)
# 图像旋转
# 逆时针旋转45度
res3 = ratate(img, 45)
cv2.imshow("res3", res3)
cv2.waitKey()
cv2.destroyAllWindows()
# 顺时针旋转90度
res4 = ratate(img, -90)
cv2.imshow("res4", res4)