前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据增强之仿射变换

数据增强之仿射变换

原创
作者头像
用户4363240
修改2019-11-26 18:18:16
1.6K0
修改2019-11-26 18:18:16
举报
文章被收录于专栏:VisonVison

一般情况下,不同的图像任务和模型有不同的数据增强方法。然而比较通用的有图像的仿射变换,颜色抖动,水平/垂直翻转, 随机crop。

其中,仿射变换包括旋转,平移,错切(shear), 尺度变化(scale)。仿射变换特点:直线经过仿射变换仍然是直线。

在pytorch中,一般每个变换写一个类,然后多个变换compose在一起。

Class Compose(object):
	def __init__(self, transforms):
		self.transforms = transforms
	def __call__(self, *items):
		for t in self.transforms:
			item = t(*item)
		return item

# example
Class ColorJitter(torchvision.transforms.ColorJitter):
	def __call__(self, image, data):
		img = Image.fromarray(image)
		# data are annotations, like points, bboxes
		# do corresponding transforms to annotations if need, such as in Flip, Rotation, etc 
		return np.array(super().__call__(img)), data

Class Dataset(object):
	def gen_transforms(self):
		transforms = [
		you_define_transform1(),
		you_define_transform2()]
		self.transforms = Compose(transforms)

	def __getitem__(self, index):
		# ...
		image, data = self.transforms(image, data)
		# ...

————————————————

Shear:

图片来源:wiki
图片来源:wiki
theta = 30 # degree

# method1: directly calculate the affine matrix
shear = np.array([
                    [1, np.tan(theta), 0],
                    [0, 1, 0]], dtype=np.float32)

# method2: use 3 points to define a transform
# for example, choose red, blue, green points in above wiki image
point1 = np.float32([[0,0], [img.shape[1],0], [0,img.shape[0]]])
point2 = np.float32([[0,0], [img.shape[1],0], [img.shape[0]*np.tan(theta), img.shape[0]]])
M_shear = cv2.getAffineTransform(point1, point2)

# shear equals to M_shear

sheared_point = M_shear.dot(point)
# choose borderValue according to MEAN in your preprocess
img = cv2.warpAffine(img, M_shear, (int(sheared_point[0]), int(sheared_point[1])),borderMode=cv2.BORDER_CONSTANT, borderValue=(128, 128, 128))

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Shear:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档