def _random_roate(self, images, labels, degree):
if(images.shape[0] != labels.shape[0]):
raise Exception("Batch size Error.")
degree = degree * math.pi / 180
rand_degree = np.random.uniform(-degree, degree, images.shape[0])
o_images = np.zeros_like(images)
o_labels = np.zeros_like(labels)
for idx in xrange(images.shape[0]):
theta = rand_degree[idx]
# labels
for ii in xrange(self.points_num):
o_labels[idx, 2*ii: 2*ii+2] = self._rotate(labels[idx, ii*2: 2*ii+2], theta)
# image
M = cv2.getRotationMatrix2D((self.img_width/2,self.img_height/2),-theta*180/math.pi,1)
o_images[idx] = np.expand_dims(cv2.warpAffine(images[idx],M,(self.img_width,self.img_height)), axis=2)
return o_images, o_labels
python类warpAffine()的实例源码
read_data.py 文件源码
项目:human-pose-estimation-by-deep-learning
作者: HYPJUDY
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def affine_skew(self, tilt, phi, img, mask=None):
h, w = img.shape[:2]
if mask is None:
mask = np.zeros((h, w), np.uint8)
mask[:] = 255
A = np.float32([[1, 0, 0], [0, 1, 0]])
if phi != 0.0:
phi = np.deg2rad(phi)
s, c = np.sin(phi), np.cos(phi)
A = np.float32([[c, -s], [s, c]])
corners = [[0, 0], [w, 0], [w, h], [0, h]]
tcorners = np.int32(np.dot(corners, A.T))
x, y, w, h = cv2.boundingRect(tcorners.reshape(1, -1, 2))
A = np.hstack([A, [[-x], [-y]]])
img = cv2.warpAffine(img, A, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE)
if tilt != 1.0:
s = 0.8*np.sqrt(tilt * tilt - 1)
img = cv2.GaussianBlur(img, (0, 0), sigmaX=s, sigmaY=0.01)
img = cv2.resize(img, (0, 0), fx=1.0 / tilt, fy=1.0, interpolation=cv2.INTER_NEAREST)
A[0] /= tilt
if phi != 0.0 or tilt != 1.0:
h, w = img.shape[:2]
mask = cv2.warpAffine(mask, A, (w, h), flags=cv2.INTER_NEAREST)
Ai = cv2.invertAffineTransform(A)
return img, mask, Ai
def rotate_image(mat, angle):
height, width = mat.shape[:2]
image_center = (width / 2, height / 2)
rotation_mat = cv2.getRotationMatrix2D(image_center, angle, 1)
radians = math.radians(angle)
sin = math.sin(radians)
cos = math.cos(radians)
bound_w = int((height * abs(sin)) + (width * abs(cos)))
bound_h = int((height * abs(cos)) + (width * abs(sin)))
rotation_mat[0, 2] += ((bound_w / 2) - image_center[0])
rotation_mat[1, 2] += ((bound_h / 2) - image_center[1])
rotated_mat = cv2.warpAffine(mat, rotation_mat, (bound_w, bound_h))
return rotated_mat
read_data.py 文件源码
项目:human-pose-estimation-by-deep-learning
作者: HYPJUDY
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def _batch_random_roate(self, images, labels, degree):
if(images.shape[0] != labels.shape[0]):
raise Exception("Batch size Error.")
degree = degree * math.pi / 180
rand_degree = np.random.uniform(-degree, degree)
o_images = np.zeros_like(images)
o_labels = np.zeros_like(labels)
for idx in xrange(images.shape[0]):
theta = rand_degree
# labels
for ii in xrange(self.points_num):
o_labels[idx, 2*ii: 2*ii+2] = self._rotate(labels[idx, ii*2: 2*ii+2], theta)
# image
M = cv2.getRotationMatrix2D((self.img_width/2,self.img_height/2),-theta*180/math.pi,1)
o_images[idx] = np.expand_dims(cv2.warpAffine(images[idx],M,(self.img_width,self.img_height)), axis=2)
return o_images, o_labels
spine_layers_nii.py 文件源码
项目:SemiSupervised_itterativeCNN
作者: styloInt
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def data_augmentation(im, label):
rotatation_angle = [-20, -10, 0, 10, 20]
translate_x = [-15, -10, 0, 10, 15]
translate_y = [-15, -10, 0, 10, 15]
angle = random.choice(rotatation_angle)
tx = random.choice(translate_x)
ty = random.choice(translate_y)
rows, cols = im.shape
M_rotate = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1)
M_translate = np.float32([[1,0,tx],[0,1,ty]])
im = cv2.warpAffine(im, M_translate,(cols,rows))
label = cv2.warpAffine(label,M_translate,(cols,rows))
im = cv2.warpAffine(im,M_rotate,(cols,rows))
label = cv2.warpAffine(label, M_rotate,(cols,rows))
return im, label
def _aligned(im_ref, im, im_to_align=None, key=None):
w, h = im.shape[:2]
im_ref = cv2.resize(im_ref, (h, w), interpolation=cv2.INTER_CUBIC)
im_ref = _preprocess_for_alignment(im_ref)
if im_to_align is None:
im_to_align = im
im_to_align = _preprocess_for_alignment(im_to_align)
assert im_ref.shape[:2] == im_to_align.shape[:2]
try:
cc, warp_matrix = _get_alignment(im_ref, im_to_align, key)
except cv2.error as e:
logger.info('Error getting alignment: {}'.format(e))
return im, False
else:
im = cv2.warpAffine(im, warp_matrix, (h, w),
flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
im[im == 0] = np.mean(im)
return im, True
fx_transform_and_crop_face.py 文件源码
项目:prepare-faces-zyf
作者: walkoncross
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def transform_and_crop_face(src_img, facial_pts, normalized_pts=dft_normalized_5points, crop_size=dft_crop_size):
pts_dst = np.float32(normalized_pts)
if pts_dst.shape[0]==2:
pts_dst = pts_dst.transpose()
pts_src = np.float32(facial_pts)
if pts_src.shape[0]==2:
pts_src = pts_src.transpose()
# tfm = cv2.getAffineTransform(pts_src[0:3], pts_dst[0:3])
# print('cv2.getAffineTransform returns tfm=\n' + str(tfm))
# print('type(tfm):' + str(type(tfm)))
# print('tfm.dtype:' + str(tfm.dtype))
tfm = _get_transform_matrix(pts_src, pts_dst)
# print('_get_transform_matrix returns tfm=\n' + str(tfm))
# print('type(tfm):' + str(type(tfm)))
# print('tfm.dtype:' + str(tfm.dtype))
dst_img = cv2.warpAffine(src_img, tfm, (crop_size[0], crop_size[1]))
return dst_img
~fx_transform_and_crop_face.py 文件源码
项目:prepare-faces-zyf
作者: walkoncross
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def transform_and_crop_face(src_img, facial_pts, normalized_pts=dft_normalized_5points, crop_size=dft_crop_size):
pts_dst = np.float32(normalized_pts)
if pts_dst.shape[0]==2:
pts_dst = pts_dst.transpose()
pts_src = np.float32(facial_pts)
if pts_src.shape[0]==2:
pts_src = pts_src.transpose()
# tfm = cv2.getAffineTransform(pts_src[0:3], pts_dst[0:3])
# print('cv2.getAffineTransform returns tfm=\n' + str(tfm))
# print('type(tfm):' + str(type(tfm)))
# print('tfm.dtype:' + str(tfm.dtype))
tfm = _get_transform_matrix(pts_src, pts_dst)
print('_get_transform_matrix returns tfm=\n' + str(tfm))
print('type(tfm):' + str(type(tfm)))
print('tfm.dtype:' + str(tfm.dtype))
dst_img = cv2.warpAffine(src_img, tfm, (crop_size[0], crop_size[1]))
return dst_img
def warp_and_crop_face(src_img, facial_pts, normalized_pts=dft_normalized_5points, crop_size=dft_crop_size):
pts_dst = np.float32(normalized_pts)
if pts_dst.shape[0]==2:
pts_dst = pts_dst.transpose()
pts_src = np.float32(facial_pts)
if pts_src.shape[0]==2:
pts_src = pts_src.transpose()
# tfm = cv2.getAffineTransform(pts_src[0:3], pts_dst[0:3])
# print('cv2.getAffineTransform returns tfm=\n' + str(tfm))
# print('type(tfm):' + str(type(tfm)))
# print('tfm.dtype:' + str(tfm.dtype))
tfm = _get_transform_matrix(pts_src, pts_dst)
# print('_get_transform_matrix returns tfm=\n' + str(tfm))
# print('type(tfm):' + str(type(tfm)))
# print('tfm.dtype:' + str(tfm.dtype))
dst_img = cv2.warpAffine(src_img, tfm, (crop_size[0], crop_size[1]))
return dst_img
def affineTransform(self):
folder=self.sort_files()
P=self.get_points()
self.height,self.width=cv2.imread("Frames/1.jpg").shape[:2]
# Process frames
for i in folder:
pic="Frames/"+str(i)+".jpg"
img = cv2.imread(pic)
pts1 = np.float32([[P[0][0],P[0][1]],[P[1][0],P[1][1]],[P[2][0],P[2][1]]])
pts2 = np.float32([[P[0][2],P[0][3]],[P[1][2],P[1][3]],[P[2][2],P[2][3]]])
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img,M,(self.width,self.height))
cv2.imwrite("Frames/%d.jpg" % i, dst)
# Method for Perspective transformation: OpenCV Module
def segment_ch4(self, segment_fn, segment_transform):
segs = np.zeros_like(self.ch4_images, dtype=np.float32)
ims = np.copy(self.ch4_images).reshape(-1, 1, self.ch4_images.shape[1],
self.ch4_images.shape[2])
ims = segment_transform(ims)
for i in xrange(self.ch4_images.shape[0]):
segs[i:i+1] = segment_fn(ims[i:i+1])
_,sb = cv2.threshold(np.copy(segs[i])*255, 127, 255, cv2.THRESH_BINARY)
patches = get_patches(sb)
sb = np.zeros_like(sb, dtype=np.uint8)
if len(patches) > 0:
patch = next(p for p in patches if p.shape[0] == max(p1.shape[0]
for p1 in patches))
for x,y in patch:
sb[x,y]=255
pca = decomposition.PCA(n_components=2)
pca.fit(patch)
mean, major = pca.mean_, pca.components_[0]
middle = sb.shape[0]/2
sb = cv2.warpAffine(sb, np.float32([[1,0,middle-mean[1]],
[0,1,middle-mean[0]]]), sb.shape)
sb = scipy.misc.imrotate(sb, np.arctan2(*major)*180/np.pi)
segs[i:i+1]=sb
self.ch4seg = segs
self.ch4counts = np.array([np.count_nonzero(s) for s in self.ch4seg]).reshape(1,-1)
gen_no_plate_shape_version.py 文件源码
项目:tensorflow_lstm_ctc_ocr
作者: linfan
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def generate_im(char_ims, num_bg_images):
bg = generate_bg(num_bg_images)
plate, plate_mask, code = generate_plate(FONT_HEIGHT, char_ims)
M, out_of_bounds = make_affine_transform(
from_shape=plate.shape,
to_shape=bg.shape,
min_scale=0.8,
max_scale=0.9,
rotation_variation=0,
scale_variation=1.0,
translation_variation=1.0)
plate = cv2.warpAffine(plate, M, (bg.shape[1], bg.shape[0]))
plate_mask = cv2.warpAffine(plate_mask, M, (bg.shape[1], bg.shape[0]))
# plate_mask = cv2.warpAffine(plate_mask, M, (bg.shape[1], bg.shape[0]))
# out = plate * plate_mask + bg * (1 - plate_mask)
out = plate + bg
# out = plate
out = cv2.resize(out, (OUTPUT_SHAPE[1], OUTPUT_SHAPE[0]))
# out += numpy.random.normal(scale=0.05, size=out.shape)
out = numpy.clip(out, 0., 1.)
return out, code, not out_of_bounds
def generate_im(char_ims, num_bg_images):
bg = generate_bg(num_bg_images)
plate, plate_mask, code = generate_plate(FONT_HEIGHT, char_ims)
M, out_of_bounds = make_affine_transform(
from_shape=plate.shape,
to_shape=bg.shape,
min_scale=0.8,
max_scale=0.9,
rotation_variation=0.3,
scale_variation=1.0,
translation_variation=1.0)
plate = cv2.warpAffine(plate, M, (bg.shape[1], bg.shape[0]))
plate_mask = cv2.warpAffine(plate_mask, M, (bg.shape[1], bg.shape[0]))
out = plate * plate_mask + bg * (1 - plate_mask)
out = cv2.resize(out, (OUTPUT_SHAPE[1], OUTPUT_SHAPE[0]))
out += numpy.random.normal(scale=0.05, size=out.shape)
out = numpy.clip(out, 0., 1.)
return out, code, not out_of_bounds
def get_speed(frame):
"""
:param frame: Captured image
:return: Speed
"""
# Disable speed detection
return 0
speed = frame[settings.IMAGE_SPEED_Y[0]:settings.IMAGE_SPEED_Y[1], settings.IMAGE_SPEED_X[0]:settings.IMAGE_SPEED_X[1]]
speed_gray = cv2.cvtColor(speed, cv2.COLOR_BGR2GRAY)
# Zoom
rows, cols = speed_gray.shape[:2]
M = np.float32([[2, 0, 0], [0, 2, 0]])
speed_zoom = cv2.warpAffine(speed_gray, M, (cols * 2, rows * 2))
_, speed_threshold = cv2.threshold(speed_zoom, 210, 255, cv2.THRESH_BINARY)
to_detect = speed_threshold[:, 26:]
#cv2.imshow('speed', to_detect)
to_detect = cv2.resize(to_detect, (20, 20))
to_detect = to_detect.reshape((1, 400))
to_detect = np.float32(to_detect)
_, results, _, _ = model.findNearest(to_detect, k=1)
return int((results[0][0]))
def augmentate(self):
angles = [45, 90, 135, 180, 225, 270, 315]
scale = 1.0
for img in self.images:
print "image shape : ", img.shape
w = img.shape[1]
h = img.shape[0]
img_vmirror = cv2.flip(img,1)
skimage.io.imsave("testv"+".jpg", img_vmirror )
for angle in angles:
#rangle = np.deg2rad(angle)
# nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale
# nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale
rot_mat = cv2.getRotationMatrix2D((w*0.5, h*0.5), angle, scale)
# rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0]))
# rot_mat[0,2] += rot_move[0]
# rot_mat[1,2] += rot_move[1]
new_img = cv2.warpAffine(img, rot_mat, (int(math.ceil(w)), int(math.ceil(h))), flags=cv2.INTER_LANCZOS4)
skimage.io.imsave("test"+str(angle)+".jpg", new_img)
new_img_vmirror = cv2.flip(new_img, 1)
skimage.io.imsave("testv"+str(angle)+".jpg", new_img_vmirror)
# img_rmirror = cv2.flip(new_img, 0)
# skimage.io.imsave("testh"+str(angle)+".jpg", img_rmirror)
def load_and_augmentate(self, root):
angles = [45, 90, 135, 180, 225, 270, 315]
scale = 1.0
for img_dir in os.listdir(root):
img_dir_path = os.path.join(root, img_dir)
for img in os.listdir(img_dir_path):
img_path = os.path.join(img_dir_path, img)
image = caffe.io.load_image(img_path,color=True)
w = image.shape[1]
h = image.shape[0]
img_name = img.split(".")[0]
img_type = img.split(".")[-1]
img_vmirror = cv2.flip(image,1)
img_vmirror_path = os.path.join(img_dir_path,img_name+"_v."+img_type)
skimage.io.imsave(img_vmirror_path, img_vmirror )
for angle in angles:
rot_mat = cv2.getRotationMatrix2D((w*0.5, h*0.5), angle, scale)
new_img = cv2.warpAffine(image, rot_mat, (int(math.ceil(w)), int(math.ceil(h))), flags=cv2.INTER_LANCZOS4)
new_img_path = os.path.join(img_dir_path,img_name+"_"+str(angle)+"."+img_type)
skimage.io.imsave(new_img_path, new_img)
new_img_vmirror = cv2.flip(new_img, 1)
new_img_vmirror_path = os.path.join(img_dir_path, img_name+"_"+str(angle)+"_v."+img_type)
skimage.io.imsave(new_img_vmirror_path, new_img_vmirror)
image_augmentation.py 文件源码
项目:dlcv_for_beginners
作者: frombeijingwithlove
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def rotate_image(img, angle, crop):
h, w = img.shape[:2]
angle %= 360
M_rotate = cv2.getRotationMatrix2D((w/2, h/2), angle, 1)
img_rotated = cv2.warpAffine(img, M_rotate, (w, h))
if crop:
angle_crop = angle % 180
if angle_crop > 90:
angle_crop = 180 - angle_crop
theta = angle_crop * np.pi / 180.0
hw_ratio = float(h) / float(w)
tan_theta = np.tan(theta)
numerator = np.cos(theta) + np.sin(theta) * tan_theta
r = hw_ratio if h > w else 1 / hw_ratio
denominator = r * tan_theta + 1
crop_mult = numerator / denominator
w_crop = int(round(crop_mult*w))
h_crop = int(round(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
sculpture_gen.py 文件源码
项目:Simple-User-Input-Sculpture-Generation
作者: ClaireKincaid
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def create_image_matrix(self, degrees=180):
"""
This creates a 3d matrix of an image with rotations acting in the xy plane
This code is not yet integrated into the menu, but it works. It needs
to be able to take user text input to create transformation matrices that
can act on any volume data.
"""
width = self.matrix_size
rows,cols = self.img_cp.shape #Image cp is the compressed image.
v = np.zeros((width, width, width))
for z in range(width):
M = cv2.getRotationMatrix2D((cols/2,rows/2),z*degrees/width,1) #This finds the rotation matirx
dyn_img = cv2.resize(image, (int(np.cos(z/width)*width+10), width-z+10)) #Resizes the image throughout the z axis based on a mathematical function.
dst = cv2.warpAffine(dyn_img, M,(cols/2,rows/2)) #This applies the rotation matrix to the image.
v[:][z][:] += cv2.warpAffine(dyn_img,M,(cols,rows))
v = np.lib.pad(v, ((1,1),(1,1),(1,1)), 'constant') #This padds the z axis with zero's arrays so that a closed shape is produced by create_iso_surface.
return v
def _rotate_image(self, mat, angle, width, height):
big = max(width, height)
small = min(width, height)
center = (big / 2.0) - (small / 2.0)
trans = numpy.float32([[1, 0, 0], [0, 1, 0]])
trans2 = numpy.float32([[1, 0, 0], [0, 1, 0]])
if small == width:
trans[0, 2] = center
trans2[1, 2] = -center - 1
else:
trans[1, 2] = center
trans2[0, 2] = -center - 1
# first enlarge the image to a square, translating the pixels to the new center
mat = cv2.warpAffine(mat, trans, (big, big))
# then rotate on the new center
rot = cv2.getRotationMatrix2D((big / 2, big / 2), angle, 1)
mat = cv2.warpAffine(mat, rot, (big, big))
# finally translate back to the start and resize to the new size
return cv2.warpAffine(mat, trans2, (height, width))
def align_face_to_template(img, facial_landmarks, output_dim, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP):
"""
Aligns image by warping it to fit the landmarks on
the image (src) to the landmarks on the template (dst)
Args:
img: src image to be aligned
facial_landmarks: list of 68 landmarks (obtained from dlib)
output_dim: image output dimension
"""
np_landmarks = np.float32(facial_landmarks)
np_landmarks_idx = np.array(landmarkIndices)
H = cv2.getAffineTransform(np_landmarks[np_landmarks_idx],
output_dim * SCALED_LANDMARKS[np_landmarks_idx])
warped = cv2.warpAffine(img, H, (output_dim, output_dim))
return warped
def translate_image(image, translationMatrix):
""" Translates the image given a translation matrix."""
# which image shape? (ConvNet (3, w, h) vs. Normal (w, h, 3)
reshape = False
prevShape = image.shape
if image.shape[0] == 3 or image.shape[0] == 1:
reshape = True
if image.shape[0] == image.shape[1] or (image.shape[0] == 1 and image.shape[1] == 3): # grayscale 1L, 1L, h, w OR color 1L, 3L, h, w
reshapeVector = (image.shape[2], image.shape[3], image.shape[1])
else:
reshapeVector = (image.shape[1], image.shape[2], image.shape[0]) # single row color or grayscale 1L/3L, h, w
image = image.reshape(reshapeVector)
h, w = image.shape[0], image.shape[1]
image = cv.warpAffine(image, translationMatrix, (w, h))
if reshape:
image = image.reshape(prevShape)
return image
def transition(self, src, level):
size = tuple(np.array([src.shape[1], src.shape[0]]))
if random.randint(0, 1) == 0:
move_x = level
else:
move_x = level * -1
if random.randint(0, 1) == 0:
move_y = level
else:
move_y = level * -1
matrix = [
[1, 0, move_x],
[0, 1, move_y]
]
affine_matrix = np.float32(matrix)
img_afn = cv2.warpAffine(src, affine_matrix,
size, flags=cv2.INTER_LINEAR)
return img_afn
def rotate_image(img_src, angle,scale ,crop=True):
img_src,size_dest= pad_image(img_src,scale)
size = tuple(np.array([img_src.shape[1], img_src.shape[0]]))
org_h=size[1]
org_w=size[0]
src_r = np.sqrt((size[0]/2.0)**2+(size[1]/2.0)**2)
org_angle =np.arctan(float(org_h)/org_w)
dest_h = size_dest[0]
dest_w = size_dest[1]
center = tuple(np.array([img_src.shape[1] * 0.5, img_src.shape[0] * 0.5]))
dsize= (dest_w,dest_h)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
img_rot = cv2.warpAffine(img_src, rotation_matrix, size, flags=cv2.INTER_CUBIC)
if crop:
x,y,w,h = cv2.boundingRect(img_rot[:,:,3])
return img_rot[y:y+h, x:x+w,:]
else:
return img_rot
def rotate_image(img_src, angle,scale ):
img_src,size_dest= pad_image(img_src,scale)
size = tuple(np.array([img_src.shape[1], img_src.shape[0]]))
org_h=size[1]
org_w=size[0]
src_r = np.sqrt((size[0]/2.0)**2+(size[1]/2.0)**2)
org_angle =np.arctan(float(org_h)/org_w)
dest_h = size_dest[0]
dest_w = size_dest[1]
center = tuple(np.array([img_src.shape[1] * 0.5, img_src.shape[0] * 0.5]))
dsize= (dest_w,dest_h)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
img_rot = cv2.warpAffine(img_src, rotation_matrix, size, flags=cv2.INTER_CUBIC)
x,y,w,h = cv2.boundingRect(img_rot[:,:,3])
return img_rot[y:y+h, x:x+w,:]
def plot_samples(Ia, Ib, M, mean, prefix=''):
assert Ia.shape == Ib.shape, 'shapes must match'
for i, _ in enumerate(Ia):
crop = (Ia[i].transpose(1, 2, 0) + mean).astype(np.uint8)
warp = (Ib[i].transpose(1, 2, 0) + mean).astype(np.uint8)
theta = M[i].reshape((2, 3))
trns = cv2.warpAffine(warp, theta, crop.shape[0:2],
flags=cv2.INTER_LINEAR | cv2.WARP_INVERSE_MAP)
out = np.hstack((crop, warp, trns))
cv2.imwrite('%s_%d.png' % (prefix, i), out)
# This is slightly different from https://arxiv.org/abs/1703.05593,
# where the dataset is generated in advance and kept fixed. Here,
# we generate a new transformation every time an image is sampled.
def crop_transform(img, params):
# take the center crop of the original image as I_{A}
crop = center_crop(img, 227)
M, = generate_transformations(
1, (img.shape[0], img.shape[1]), **params
)
# apply T_{\theta_{GT}} to I_{A} to get I_{B}
warp = cv2.warpAffine(
crop.astype(np.float32), M[:2], (crop.shape[1], crop.shape[0]),
flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101
)
return crop, warp, M
def calculatePicture(file):
"""gettings infos of the image and applie the matrixes"""
img = cv2.imread(file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces, eyes = detect(img, gray)
# print("faces: " + str(faces) + " # eyes:" + str(eyes))
height, width, channels = img.shape
if faces is None or eyes is None:
return None
face = faces[0]
eye = [eyes[0], eyes[1]]
moveMatrix, rotMatrix = matrixPicture(face, eye, height, width)
dst = cv2.warpAffine(img, moveMatrix, (width, height))
dst = cv2.warpAffine(dst, rotMatrix, (width, height))
return dst
def _translate(image, horizontal=(0,40), vertical=(0,10)):
'''
Randomly translate the input image horizontally and vertically.
Arguments:
image (array-like): The image to be translated.
horizontal (int tuple, optinal): A 2-tuple `(min, max)` with the minimum
and maximum horizontal translation. A random translation value will
be picked from a uniform distribution over [min, max].
vertical (int tuple, optional): Analog to `horizontal`.
Returns:
The translated image and the horzontal and vertical shift values.
'''
rows,cols,ch = image.shape
x = np.random.randint(horizontal[0], horizontal[1]+1)
y = np.random.randint(vertical[0], vertical[1]+1)
x_shift = random.choice([-x, x])
y_shift = random.choice([-y, y])
M = np.float32([[1,0,x_shift],[0,1,y_shift]])
return cv2.warpAffine(image, M, (cols, rows)), x_shift, y_shift
def _scale(image, min=0.9, max=1.1):
'''
Scale the input image by a random factor picked from a uniform distribution
over [min, max].
Returns:
The scaled image, the associated warp matrix, and the scaling value.
'''
rows,cols,ch = image.shape
#Randomly select a scaling factor from the range passed.
scale = np.random.uniform(min, max)
M = cv2.getRotationMatrix2D((cols/2,rows/2), 0, scale)
return cv2.warpAffine(image, M, (cols, rows)), M, scale
image_processing_common.py 文件源码
项目:tensorflow-litterbox
作者: rwightman
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def distort_affine_cv2(image, alpha_affine=10, random_state=None):
if random_state is None:
random_state = np.random.RandomState(None)
shape = image.shape
shape_size = shape[:2]
center_square = np.float32(shape_size) // 2
square_size = min(shape_size) // 3
pts1 = np.float32([
center_square + square_size,
[center_square[0] + square_size, center_square[1] - square_size],
center_square - square_size])
pts2 = pts1 + random_state.uniform(-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)
M = cv2.getAffineTransform(pts1, pts2)
distorted_image = cv2.warpAffine(
image, M, shape_size[::-1], borderMode=cv2.BORDER_REPLICATE) #cv2.BORDER_REFLECT_101)
return distorted_image