其他
利用opencv实现的代码如下:
# 去除黑边的操作
crop_image = lambda img, x0, y0, w, h: img[y0:y0+h, x0:x0+w] # 定义裁切函数,后续裁切黑边使用
def rotate_image(img, angle, crop):
"""
angle: 旋转的角度
crop: 是否需要进行裁剪,布尔向量
"""
w, h = img.shape[:2]
# 旋转角度的周期是360°
angle %= 360
# 计算仿射变换矩阵
M_rotation = cv2.getRotationMatrix2D((w / 2, h / 2), angle, 1)
# 得到旋转后的图像
img_rotated = cv2.warpAffine(img, M_rotation, (w, h))
# 如果需要去除黑边
if crop:
# 裁剪角度的等效周期是180°
angle_crop = angle % 180
if angle > 90:
angle_crop = 180 - angle_crop
# 转化角度为弧度
theta = angle_crop * np.pi / 180
# 计算高宽比
hw_ratio = float(h) / float(w)
# 计算裁剪边长系数的分子项
tan_theta = np.tan(theta)
numerator = np.cos(theta) + np.sin(theta) * np.tan(theta)
# 计算分母中和高宽比相关的项
r = hw_ratio if h > w else 1 / hw_ratio
# 计算分母项
denominator = r * tan_theta + 1
# 最终的边长系数
crop_mult = numerator / denominator
# 得到裁剪区域
w_crop = int(crop_mult * w)
h_crop = int(crop_mult * h)
x0 = int((w - w_crop) / 2)
y0 = int((h - h_crop) / 2)
img_rotated = crop_image(img_rotated, x0, y0, w_crop, h_crop)
return img_rotated
#水平镜像
h_flip = cv2.flip(img,1)
#垂直镜像
v_flip = cv2.flip(img,0)
#水平垂直镜像
hv_flip = cv2.flip(img,-1)
#90度旋转
rows, cols, _ = img.shape
M = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1)
rotation_45 = cv2.warpAffine(img, M, (cols, rows))
#45度旋转
M = cv2.getRotationMatrix2D((cols/2, rows/2), 135, 2)
rotation_135 = cv2.warpAffine(img, M,(cols, rows))
#去黑边旋转45度
image_rotated = rotate_image(img, 45, True)
#显示
plt.figure(figsize=(15, 10))
plt.subplot(2,3,1), plt.imshow(img)
plt.axis('off'); plt.title('原图')
plt.subplot(2,3,2), plt.imshow(h_flip)
plt.axis('off'); plt.title('水平镜像')
plt.subplot(2,3,3), plt.imshow(v_flip)
plt.axis('off'); plt.title('垂直镜像')
plt.subplot(2,3,4), plt.imshow(hv_flip)
plt.axis('off'); plt.title('水平垂直镜像')
plt.subplot(2,3,5), plt.imshow(rotation_45)
plt.axis('off'); plt.title('旋转45度')
plt.subplot(2,3,6), plt.imshow(image_rotated)
plt.axis('off'); plt.title('去黑边旋转45度')
plt.show()
上述代码通过opencv自带的flip函数实现了翻转操作,该函数的第二个参数是控制翻转的方向。通过内切矩阵计算公式可得无黑边剪切结果。上述代码的结果图如图2-36所示。通过结果可以看出,旋转
img
3. 缩放
图像可以向外或向内缩放。向外缩放时,最终图像尺寸将大于原始图像尺寸,为了保持原始图像的大小,通常需要结合裁剪,从缩放后的图像中裁剪出和原始图像大小一样的图像。另一种方法是向内缩放,它会缩小图像大小,缩小到预设的大小。缩放也会带了一些问题,如缩放后的图像尺寸和原始图像尺寸的长宽比差异较大,会出现图像失帧的现象,如果在实验中对最终的结果有一定的影响,需要做等比例缩放,对不足的地方进行边缘填充。以下是缩放的代码和结果图像。
img_2 = cv2.resize(img, (int(h * 1.5), int(w * 1.5)))
img_2 = img_2[int((h - 512) / 2) : int((h + 512) / 2), int((w - 512) / 2) : int((w + 512) /2), :]
img_3 = cv2.resize(img, (512, 512))
## 显示
plt.figure(figsize=(15, 10))
plt.subplot(1,3,1), plt.imshow(img)
plt.axis('off'); plt.title('原图')
plt.subplot(1,3,2), plt.imshow(img_2)
plt.axis('off'); plt.title('向外缩放')
plt.subplot(1,3,3), plt.imshow(img_3)
plt.axis('off'); plt.title('向内缩放')
plt.show()
img
4.移位
移位只涉及沿X或Y方向(或两者)移动图像,如果图像的背景是单色背景或者是纯的黑色背景,使用该方法可以很有效的增强数据数量,可以通过cv2.warpAffine实现该代码
mat_shift = np.float32([[1,0,100], [0,1,200]])
img_1 = cv2.warpAffine(img, mat_shift, (h, w))
mat_shift = np.float32([[1, 0, -150], [0, 1, -150]])
img_2 = cv2.warpAffine(img, mat_shift, (h, w))
## 显示
plt.figure(figsize=(15, 10))
plt.subplot(1,3,1), plt.imshow(img)
plt.axis('off'); plt.title('原图')
plt.subplot(1,3,2), plt.imshow(img_1)
plt.axis('off'); plt.title('向右下移动')
plt.subplot(1,3,3), plt.imshow(img_2)
plt.axis('off'); plt.title('左上移动')
plt.show()
img
5. 高斯噪声
当神经网络试图学习可能无用的高频特征(即图像中大量出现的模式)时,通常会发生过度拟合。具有零均值的高斯噪声基本上在所有频率中具有数据点,从而有效地扭曲高频特征。这也意味着较低频率的组件也会失真,但你的神经网络可以学会超越它,添加适量的噪音可以增强学习能力。
基于噪声的数据增强就是在原来的图片的基础上,随机叠加一些噪声,最常见的做法就是高斯噪声。更复杂一点的就是在面积大小可选定、位置随机的矩形区域上丢弃像素产生黑色矩形块,从而产生一些彩色噪声,以Coarse Dropout方法为代表,甚至还可以对图片上随机选取一块区域并擦除图像信息。以下是代码和图像:
img_s1 = gasuss_noise(img, 0, 0.005)
img_s2 = gasuss_noise(img, 0, 0.05)
plt.figure(figsize=(15, 10))
plt.subplot(1,3,1), plt.imshow(img)
plt.axis('off'); plt.title('原图')
plt.subplot(1,3,2), plt.imshow(img_s1)
plt.axis('off'); plt.title('方差为0.005')
plt.subplot(1,3,3), plt.imshow(img_s2)
plt.axis('off'); plt.title('方差为0.05')
plt.show()
img
6.色彩抖动
上面提到的图像中有一个比较大的难点是背景干扰,在实际工程中为了消除图像在不同背景中存在的差异性,通常会做一些色彩抖动操作,扩充数据集合。色彩抖动主要是在图像的颜色方面做增强,主要调整的是图像的亮度,饱和度和对比度。工程中不是任何数据集都适用,通常如果不同背景的图像较多,加入色彩抖动操作会有很好的提升。
def randomColor(image, saturation=0, brightness=0, contrast=0, sharpness=0):
if random.random() < saturation:
random_factor = np.random.randint(0, 31) / 10. # 随机因子
image = ImageEnhance.Color(image).enhance(random_factor) # 调整图像的饱和度
if random.random() < brightness:
random_factor = np.random.randint(10, 21) / 10. # 随机因子
image = ImageEnhance.Brightness(image).enhance(random_factor) # 调整图像的亮度
if random.random() < contrast:
random_factor = np.random.randint(10, 21) / 10. # 随机因1子
image = ImageEnhance.Contrast(image).enhance(random_factor) # 调整图像对比度
if random.random() < sharpness:
random_factor = np.random.randint(0, 31) / 10. # 随机因子
ImageEnhance.Sharpness(image).enhance(random_factor) # 调整图像锐度
return image
cj_img = Image.fromarray(img)
**收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。**


**[如果你需要这些资料,可以戳这里获取](https://gitee.com/vip204888)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人**
**都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**