1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > opencv实现数据增强(图片+标签)平移 翻转 缩放 旋转

opencv实现数据增强(图片+标签)平移 翻转 缩放 旋转

时间:2024-07-12 09:33:55

相关推荐

opencv实现数据增强(图片+标签)平移 翻转 缩放 旋转

面试问到了,让手撕数据增强,图片+标签。这里整理一下,直接上代码。

import mathimport cv2import numpy as npdef pan(img, anns, size=(50, 100)):''':param img: np.ndarray[h,w,c]:param anns: np.ndarray[n,4]:param size: list[shift_x, shift_y]'''shift_x, shift_y = sizeh, w, _ = img.shapeM = np.array([[1, 0, shift_x], [0, 1, shift_y]], dtype=np.float32) # 平移矩阵img_change = cv2.warpAffine(img, M, (w, h))anns_change = anns + np.array([shift_x, shift_y, shift_x, shift_y])return img_change, anns_changedef flip(img, anns, flip_code=0):# flip_code: 1:水平翻转, 0:垂直翻转, -1:水平垂直翻转h, w, _ = img.shapeimg_change = cv2.flip(img, flipCode=flip_code)anns_change = anns.copy()if flip_code == 1:anns_change[:, 0], anns_change[:, 2] = w - anns_change[:, 2], w - anns_change[:, 0]elif flip_code == 0:anns_change[:, 1], anns_change[:, 3] = h - anns_change[:, 3], h - anns_change[:, 1]else:anns_change[:, 0], anns_change[:, 2] = w - anns_change[:, 2], w - anns_change[:, 0]anns_change[:, 1], anns_change[:, 3] = h - anns_change[:, 3], h - anns_change[:, 1]anns_change = np.int32(anns_change)return img_change, anns_changedef resize(img, anns, scale=(2, 1)):h, w, _ = img.shapescale_x, scale_y = scaleanns_change = anns.copy()M = np.array([[scale_x, 0, 0], [0, scale_y, 0]], dtype=np.float32) # 缩放矩阵img_change = cv2.warpAffine(img, M, (int(w * scale_x), int(h * scale_y)))anns_change[:, 0], anns_change[:, 2] = anns_change[:, 0] * scale_x, anns_change[:, 2] * scale_xanns_change[:, 1], anns_change[:, 3] = anns_change[:, 1] * scale_y, anns_change[:, 3] * scale_yanns_change = np.int32(anns_change)return img_change, anns_changedef rotate(img, anns, center=(0, 0), angle=-45, scale=1):# scale为缩放比例,默认为1,也就是不缩放。 图像旋转+缩放,bboxes对不上,暂时不知道怎么解决。有人知道的话,请评论区告诉我,感谢。angle_pi = -angle * math.pi / 180.0 # 弧度h, w, _ = img.shapeM = cv2.getRotationMatrix2D(center, angle, scale)img_change = cv2.warpAffine(img, M, (w, h))anns_change = anns.copy() * scalex1, y1, x2, y2 = anns_change[:, 0], anns_change[:, 1], anns_change[:, 2], anns_change[:, 3]x3, y3, x4, y4 = x1, y2, x2, y1x1_ = (x1 - center[0]) * math.cos(angle_pi) - (y1 - center[1]) * math.sin(angle_pi) + center[0]y1_ = (x1 - center[0]) * math.sin(angle_pi) + (y1 - center[1]) * math.cos(angle_pi) + center[1]x2_ = (x2 - center[0]) * math.cos(angle_pi) - (y2 - center[1]) * math.sin(angle_pi) + center[0]y2_ = (x2 - center[0]) * math.sin(angle_pi) + (y2 - center[1]) * math.cos(angle_pi) + center[1]x3_ = (x3 - center[0]) * math.cos(angle_pi) - (y3 - center[1]) * math.sin(angle_pi) + center[0]y3_ = (x3 - center[0]) * math.sin(angle_pi) + (y3 - center[1]) * math.cos(angle_pi) + center[1]x4_ = (x4 - center[0]) * math.cos(angle_pi) - (y4 - center[1]) * math.sin(angle_pi) + center[0]y4_ = (x4 - center[0]) * math.sin(angle_pi) + (y4 - center[1]) * math.cos(angle_pi) + center[1]xs, ys = np.array([x1_, x2_, x3_, x4_]), np.array([y1_, y2_, y3_, y4_])xmin, xmax = np.amin(xs, axis=0), np.amax(xs, axis=0)ymin, ymax = np.amin(ys, axis=0), np.amax(ys, axis=0)anns_change = np.array(list(zip(xmin, ymin, xmax, ymax))) # 4个[2] ---》 [2, 4]anns_change = np.int32(anns_change)return img_change, anns_changeif __name__ == '__main__':img = cv2.imread("head.jpg")# 测试图片anns = np.array([[180, 100, 250, 150], [340, 100, 380, 150]])# 测试bboxfor i in anns:cv2.rectangle(img, (i[0], i[1]), (i[2], i[3]), (0, 0, 255), 2)cv2.imshow("origin", img)# 移动img1, anns1 = pan(img, anns)for i in anns1:cv2.rectangle(img1, (i[0], i[1]), (i[2], i[3]), (0, 0, 255), 2)cv2.imshow("pan", img1)# 翻转img2, anns2 = flip(img, anns)for i in anns2:cv2.rectangle(img2, (i[0], i[1]), (i[2], i[3]), (0, 0, 255), 2)cv2.imshow("flip", img2)# 缩放img3, anns3 = resize(img, anns)for i in anns3:cv2.rectangle(img3, (i[0], i[1]), (i[2], i[3]), (0, 0, 255), 2)cv2.imshow("resize", img3)# 旋转,anns4是下面可视化中蓝色框img4, anns4 = rotate(img, anns, center=(img.shape[1] // 2, img.shape[0] // 2)) # 中心旋转# img4, anns4 = rotate(img, anns) # 左上角旋转for i in anns4:cv2.rectangle(img4, (i[0], i[1]), (i[2], i[3]), (255, 0, 0), 2)cv2.imshow("rotate", img4)cv2.waitKey(0)

结果展示:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。