利用随机剪裁可进行图像数据库的扩大,不同的剪裁位置能够提供更多的数据样本。
编程思路
- 确定需要的图片的大小
- 在左上角计算出剪裁后剩下的长度
- 在左上角区域中取得一点作为起始点从中截取所需要的面积
原始代码
import cv2
import random
img=cv2.imread("lena.jpg")
width,height,depth= img.shape
img_width_box=width*0.2
img_height_box=height*0.2
for i in range(9):
start_pointX=random.uniform(0,img_width_box)
start_pointY=random.uniform(0,img_height_box)
copyImg=img[start_pointX:200,start_pointY:200]
cv2.imshow("test",copyImg)
cv2.waitKey(0)
发现并不能运行,并报错:
...
width,height,depth= img.shape
AttributeError: 'NoneType' object has no attribute 'shape'
并发现img.shape
不存在,通过网上查阅资料得知:读入图片时需要添加路径,否则返回值为None,于是进一步修改代码有:
import cv2
import random
img