利用opencv进行数据增强
包括平移、旋转、镜像、翻转
效果图
整体代码
#程序功能:python+opencv实现数据增强
#作者:mao
#时间:2019.5.25
import numpy as np
import cv2
img=cv2.imread("1.jpg")
cv2.imshow("original",img)
#水平镜像
h_flip=cv2.flip(img,1)
cv2.imshow("Flipped Horizontally",h_flip)
#垂直镜像
v_flip=cv2.flip(img,0)
cv2.imshow("Flipped Vertically",v_flip)
#水平垂直镜像
hv_flip=cv2.flip(img,-1)
cv2.imshow("Flipped Horizontally & Vertically",hv_flip)
#平移矩阵[[1,0,-100],[0,1,-12]]
M=np.array([[1,0,-100],[0,1,-12]],dtype=np.float32)
img_change=cv2.warpAffine(img,M,(300,300))
cv2.imshow("translation",img_change)
#90度旋转
rows,cols=img.shape[:2]
M=cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
dst=cv2.warpAffine(img,M,(cols,rows))
cv2.imshow("90",dst)
#45度旋转
rows,cols=img.shape[:2]
M=cv2.getRotationMatrix2D((cols/2,rows/2),45,1)
dst=cv2.warpAffine(img,M,(cols,rows))
cv2.imshow("45",dst)
#缩放
height,width=img.shape[:2]
res=cv2.resize(img,(2*width,2*height))
cv2.imshow("large",res)
# 仿射变换
#对图像进行变换(三点得到一个变换矩阵)
# 我们知道三点确定一个平面,我们也可以通过确定三个点的关系来得到转换矩阵
# 然后再通过warpAffine来进行变换
point1=np.float32([[50,50],[300,50],[50,200]])
point2=np.float32([[10,100],[300,50],[100,250]])
M=cv2.getAffineTransform(point1,point2)
dst1=cv2.warpAffine(img,M,(cols,rows),borderValue=(255,255,255))
cv2.imshow("affine transformation",dst1)
cv2.waitKey(0)
循环读取文件夹内图片并增强后保存为图片
import numpy
as np
import cv2
import os
save_path
='E:\\python_project\\yi_xue_ying_xiang\\tuxiangzengqiang\\after\\'
for info
in os
.listdir
(r
'E:\python_project\yi_xue_ying_xiang\tuxiangzengqiang\pic'):
domain
= os
.path
.abspath
(
r
'E:\python_project\yi_xue_ying_xiang\tuxiangzengqiang\pic')
info1
= os
.path
.join
(domain
, info
)
img
= cv2
.imread
(info1
)
cv2
.imshow
("original", img
)
cv2
.waitKey
(1000)
h_flip
= cv2
.flip
(img
, 1)
cv2
.imshow
("Flipped Horizontally", h_flip
)
cv2
.imwrite
(save_path
+info
+'_h_flip.jpg', h_flip
)
v_flip
= cv2
.flip
(img
, 0)
cv2
.imshow
("Flipped Vertically", v_flip
)
cv2
.imwrite
(save_path
+info
+'_v_flip.jpg', v_flip
)
hv_flip
= cv2
.flip
(img
, -1)
cv2
.imshow
("Flipped Horizontally & Vertically", hv_flip
)
cv2
.imwrite
(save_path
+info
+'hv_flip.jpg', hv_flip
)
rows
, cols
= img
.shape
[:2]
M
= cv2
.getRotationMatrix2D
((cols
/ 2, rows
/ 2), 90, 1)
dst_90
= cv2
.warpAffine
(img
, M
, (cols
, rows
))
cv2
.imshow
("dst_90", dst_90
)
cv2
.imwrite
(save_path
+info
+'dst_90.jpg', dst_90
)
rows
, cols
= img
.shape
[:2]
M
= cv2
.getRotationMatrix2D
((cols
/ 2, rows
/ 2), 70, 1)
dst_70
= cv2
.warpAffine
(img
, M
, (cols
, rows
))
cv2
.imshow
("dst_70", dst_70
)
cv2
.imwrite
(save_path
+info
+'dst_70.jpg', dst_70
)
rows
, cols
= img
.shape
[:2]
M
= cv2
.getRotationMatrix2D
((cols
/ 2, rows
/ 2), 60, 1)
dst_60
= cv2
.warpAffine
(img
, M
, (cols
, rows
))
cv2
.imshow
("dst_60", dst_60
)
cv2
.imwrite
(save_path
+info
+'dst_60.jpg', dst_60
)
rows
, cols
= img
.shape
[:2]
M
= cv2
.getRotationMatrix2D
((cols
/ 2, rows
/ 2), 50, 1)
dst_50
= cv2
.warpAffine
(img
, M
, (cols
, rows
))
cv2
.imshow
("dst_50", dst_50
)
cv2
.imwrite
(save_path
+info
+'dst_50.jpg', dst_50
)
rows
, cols
= img
.shape
[:2]
M
= cv2
.getRotationMatrix2D
((cols
/ 2, rows
/ 2), 45, 1)
dst_45
= cv2
.warpAffine
(img
, M
, (cols
, rows
))
cv2
.imshow
("dst_45", dst_45
)
cv2
.imwrite
(save_path
+info
+'dst_45.jpg', dst_45
)
rows
, cols
= img
.shape
[:2]
M
= cv2
.getRotationMatrix2D
((cols
/ 2, rows
/ 2), 40, 1)
dst_40
= cv2
.warpAffine
(img
, M
, (cols
, rows
))
cv2
.imshow
("dst_40", dst_40
)
cv2
.imwrite
(save_path
+info
+'dst_40.jpg', dst_40
)
rows
, cols
= img
.shape
[:2]
M
= cv2
.getRotationMatrix2D
((cols
/ 2, rows
/ 2), 30, 1)
dst_30
= cv2
.warpAffine
(img
, M
, (cols
, rows
))
cv2
.imshow
("dst_30", dst_30
)
cv2
.imwrite
(save_path
+info
+'dst_30.jpg', dst_30
)
rows
, cols
= img
.shape
[:2]
M
= cv2
.getRotationMatrix2D
((cols
/ 2, rows
/ 2), 20, 1)
dst_20
= cv2
.warpAffine
(img
, M
, (cols
, rows
))
cv2
.imshow
("dst_20", dst_20
)
cv2
.imwrite
(save_path
+info
+'dst_20.jpg', dst_20
)
point1
= np
.float32
([[50, 50], [300, 50], [50, 200]])
point2
= np
.float32
([[10, 100], [300, 50], [100, 250]])
M
= cv2
.getAffineTransform
(point1
, point2
)
dst1
= cv2
.warpAffine
(img
, M
, (cols
, rows
), borderValue
=(255, 255, 255))
cv2
.imshow
("affine transformation", dst1
)
cv2
.imwrite
(save_path
+info
+'dst1.jpg', dst1
)
cv2
.waitKey
(0)
翟羽嚄
认证博客专家
算法
机器学习
图像处理
我经常会分享一些智能驾驶、自动驾驶、图像识别、嵌入式linux、嵌入式音视频开发等知识,有兴趣的朋友可以关注我的头条号——翟羽镬,ID:1661013377414159,这里会分享一些整理好的且有一定价值的文章和源码。