def testset_generator(im):
"""
Cast a variation of transform onto `im`, includes
1. scale
2. rotate
3. affine
4. gaussian blur
5. reflect
@yield: a tri-tuple
(<name_of_the_transform>, <image_array>, <validation_function>)
where <validation_function> is a function,
receive a position (row, col), and return the new position after transform
"""
def inner(m):
return lambda r, c: np.dot(m, [c, r, 1])[::-1]
rows, cols = im.shape
# resize
yield "scale1", cv2.resize(im, None, fx=0.5, fy=0.5), lambda r, c: (r/2, c/2)
yield "scale2", cv2.resize(im, None, fx=2, fy=2), lambda r, c: (r*2, c*2)
# rotate
r_m1 = cv2.getRotationMatrix2D((cols/2, rows/2), -30, 1)
yield "rotate_30", cv2.warpAffine(im, r_m1, (cols, rows)), inner(r_m1)
r_m2 = cv2.getRotationMatrix2D((cols/2, rows/2), -45, 1)
yield "rotate_45", cv2.warpAffine(im, r_m2, (cols, rows)), inner(r_m2)
r_m3 = cv2.getRotationMatrix2D((cols/2, rows/2), -90, 1)
yield "rotate_90", cv2.warpAffine(im, r_m3, (cols, rows)), inner(r_m3)
# r_m4 = cv2.getRotationMatrix2D((cols/2, rows/2), -30, 0.7)
# yield "rotate_30_scale_0.7", cv2.warpAffine(im, r_m4, (cols, rows)), inner(r_m4)
# r_m5 = cv2.getRotationMatrix2D((cols/2, rows/2), -30, 0.5)
# yield "rotate_30_scale_0.5", cv2.warpAffine(im, r_m5, (cols, rows)), inner(r_m5)
# affine
pts1 = np.array([[50, 50], [200, 50], [50, 200]], dtype=np.float32)
pts2 = np.array([[10, 100], [200, 50], [100, 250]], dtype=np.float32)
a_m = cv2.getAffineTransform(pts1, pts2)
yield "affine", cv2.warpAffine(im, a_m, (cols, rows)), inner(a_m)
# blur
# yield "blur_11", cv2.GaussianBlur(im, (11, 11), 0), lambda r,c: (r,c)
# yield "blur_31", cv2.GaussianBlur(im, (31, 31), 0), lambda r,c: (r,c)
# reflect
yield "reflect", im[:,::-1], lambda r,c : (r, cols-c)
评论列表
文章目录