Python cv2.warpAffine() Examples
The following are 30
code examples of cv2.warpAffine().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cv2
, or try the search function
.
Example #1
Source File: utils.py From tf2-yolo3 with Apache License 2.0 | 7 votes |
def cv2_letterbox_resize(img, expected_size): ih, iw = img.shape[0:2] ew, eh = expected_size scale = min(eh / ih, ew / iw) nh = int(ih * scale) nw = int(iw * scale) smat = np.array([[scale, 0, 0], [0, scale, 0], [0, 0, 1]], np.float32) top = (eh - nh) // 2 bottom = eh - nh - top left = (ew - nw) // 2 right = ew - nw - left tmat = np.array([[1, 0, left], [0, 1, top], [0, 0, 1]], np.float32) amat = np.dot(tmat, smat) amat_ = amat[:2, :] dst = cv2.warpAffine(img, amat_, expected_size) if dst.ndim == 2: dst = np.expand_dims(dst, axis=-1) return dst, amat
Example #2
Source File: convert2onnx.py From centerpose with MIT License | 6 votes |
def pre_process(image, cfg=None, scale=1, meta=None): height, width = image.shape[0:2] new_height = int(height * scale) new_width = int(width * scale) mean = np.array(cfg.DATASET.MEAN, dtype=np.float32).reshape(1, 1, 3) std = np.array(cfg.DATASET.STD, dtype=np.float32).reshape(1, 1, 3) inp_height, inp_width = cfg.MODEL.INPUT_H, cfg.MODEL.INPUT_W c = np.array([new_width / 2., new_height / 2.], dtype=np.float32) s = max(height, width) * 1.0 trans_input = get_affine_transform(c, s, 0, [inp_width, inp_height]) resized_image = cv2.resize(image, (new_width, new_height)) inp_image = cv2.warpAffine( resized_image, trans_input, (inp_width, inp_height), flags=cv2.INTER_LINEAR) inp_image = ((inp_image / 255. - mean) / std).astype(np.float32) images = inp_image.transpose(2, 0, 1).reshape(1, 3, inp_height, inp_width) images = torch.from_numpy(images) meta = {'c': c, 's': s, 'out_height': inp_height // cfg.MODEL.DOWN_RATIO, 'out_width': inp_width // cfg.MODEL.DOWN_RATIO} return images, meta
Example #3
Source File: dataset.py From PracticalPythonAndOpenCV_CaseStudies with GNU General Public License v3.0 | 6 votes |
def center_extent(image, size): # Grab the extent width and height (w, h) = size # When the width is greater than the height if image.shape[1] > image.shape[0]: image = imutils.resize(image, width=w) # When the height is greater than the width else: image = imutils.resize(image, height=h) # Save memory for the extent of the image and grab it extent = np.zeros((h, w), dtype="uint8") offset_x = (w - image.shape[1]) // 2 offset_y = (h - image.shape[0]) // 2 extent[offset_y:offset_y + image.shape[0], offset_x:offset_x + image.shape[1]] = image # Compute the center of mass of the image and then move the center of mass to the center of the image (c_y, c_x) = np.round(mahotas.center_of_mass(extent)).astype("int32") (d_x, d_y) = ((size[0] // 2) - c_x, (size[1] // 2) - c_y) matrix = np.float32([[1, 0, d_x], [0, 1, d_y]]) extent = cv2.warpAffine(extent, matrix, size) # Return the extent of the image return extent
Example #4
Source File: geometry.py From dataflow with Apache License 2.0 | 6 votes |
def get_transform(self, img): center = img.shape[1::-1] * self._rand_range( self.center_range[0], self.center_range[1], (2,)) deg = self._rand_range(-self.max_deg, self.max_deg) if self.step_deg: deg = deg // self.step_deg * self.step_deg """ The correct center is shape*0.5-0.5. This can be verified by: SHAPE = 7 arr = np.random.rand(SHAPE, SHAPE) orig = arr c = SHAPE * 0.5 - 0.5 c = (c, c) for k in range(4): mat = cv2.getRotationMatrix2D(c, 90, 1) arr = cv2.warpAffine(arr, mat, arr.shape) assert np.all(arr == orig) """ mat = cv2.getRotationMatrix2D(tuple(center - 0.5), deg, 1) return WarpAffineTransform( mat, img.shape[1::-1], interp=self.interp, borderMode=self.border, borderValue=self.border_value)
Example #5
Source File: morpher.py From yry with Apache License 2.0 | 6 votes |
def merge_img(src_img, dst_img, dst_matrix, dst_points, blur_detail_x=None, blur_detail_y=None, mat_multiple=None): face_mask = np.zeros(src_img.shape, dtype=src_img.dtype) for group in core.OVERLAY_POINTS: cv2.fillConvexPoly(face_mask, cv2.convexHull(dst_matrix[group]), (255, 255, 255)) r = cv2.boundingRect(np.float32([dst_points[:core.FACE_END]])) center = (r[0] + int(r[2] / 2), r[1] + int(r[3] / 2)) if mat_multiple: mat = cv2.getRotationMatrix2D(center, 0, mat_multiple) face_mask = cv2.warpAffine(face_mask, mat, (face_mask.shape[1], face_mask.shape[0])) if blur_detail_x and blur_detail_y: face_mask = cv2.blur(face_mask, (blur_detail_x, blur_detail_y), center) return cv2.seamlessClone(np.uint8(dst_img), src_img, face_mask, center, cv2.NORMAL_CLONE)
Example #6
Source File: chineselib.py From ctw-baseline with MIT License | 6 votes |
def cv_preprocess_image(img, output_height, output_width, is_training): assert output_height == output_width img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) img[:, :, 0] = np.uint8((np.int32(img[:, :, 0]) + (180 + random.randrange(-9, 10))) % 180) img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB) rows, cols, ch = img.shape output_size = output_width def r(): return (random.random() - 0.5) * 0.1 * output_size pts1 = np.float32([[0, 0], [cols, rows], [0, rows]]) pts2 = np.float32([[r(), r()], [output_size + r(), output_size + r()], [r(), output_size + r()]]) M = cv2.getAffineTransform(pts1, pts2) noize = np.random.normal(0, random.random() * (0.05 * 255), size=img.shape) img = np.array(img, dtype=np.float32) + noize img = cv2.warpAffine(img, M, (output_size, output_size), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101) return img
Example #7
Source File: face_align.py From Talking-Face-Generation-DAVS with MIT License | 6 votes |
def image_loader(self, path, points): if os.path.exists(path): img = cv2.imread(path) three_points = np.zeros((3, 2)) three_points[0] = np.array(points[:2]) # the location of the left eye three_points[1] = np.array(points[2:4]) # the location of the right eye three_points[2] = np.array([(points[6] + points[8]) / 2, (points[7] + points[9]) / 2]) # the location of the center of the mouth three_points.astype(np.float32) M = transformation_from_points(three_points, self.scale) align_img = cv2.warpAffine(img, M, self.ori_scale, borderValue=[127, 127, 127]) l = int(round(self.ori_scale[0] / 2 - self.crop_width / 2 + self.random_x)) r = int(round(self.ori_scale[0] / 2 + self.crop_width / 2 + self.random_x)) t = int(round(self.ori_scale[1] / 2 - self.crop_height / 2 + self.crop_center_y_offset + self.random_y)) d = int(round(self.ori_scale[1] / 2 + self.crop_height / 2 + self.crop_center_y_offset + self.random_y)) align_img2 = align_img[t:d, l:r, :] align_img2 = cv2.resize(align_img2, self.output_scale) return align_img2 else: raise ("image = 0")
Example #8
Source File: deconvolution.py From OpenCV-Python-Tutorial with MIT License | 5 votes |
def motion_kernel(angle, d, sz=65): kern = np.ones((1, d), np.float32) c, s = np.cos(angle), np.sin(angle) A = np.float32([[c, -s, 0], [s, c, 0]]) sz2 = sz // 2 A[:,2] = (sz2, sz2) - np.dot(A[:,:2], ((d-1)*0.5, 0)) kern = cv2.warpAffine(kern, A, (sz, sz), flags=cv2.INTER_CUBIC) return kern
Example #9
Source File: process256_224.py From Talking-Face-Generation-DAVS with MIT License | 5 votes |
def image_loader(self, img): self.find_three_points() self.M = self.transformation_from_points(self.three_points, self.scale) align_img = cv2.warpAffine(img, self.M, self.ori_scale, borderValue=[127, 127, 127]) self.l = int(round(self.ori_scale[0] / 2 - self.crop_width / 2)) self.r = int(round(self.ori_scale[0] / 2 + self.crop_width / 2)) self.t = int(round(self.ori_scale[1] / 2 - self.crop_height / 2 + self.crop_center_y_offset)) self.d = int(round(self.ori_scale[1] / 2 + self.crop_height / 2 + self.crop_center_y_offset)) align_img2 = align_img[self.t:self.d, self.l:self.r, :] # cv2.imwrite('/home/wyang/temp/align_img2.jpg', align_img2) align_img2 = cv2.resize(align_img2, self.output_scale) self.compute_transpoints() return align_img2
Example #10
Source File: transforms.py From vedaseg with Apache License 2.0 | 5 votes |
def __call__(self, image, mask): if random.random() < self.p: h, w, c = image.shape angle = random.uniform(*self.degrees) matrix = cv2.getRotationMatrix2D((w / 2, h / 2), angle, 1.0) image = cv2.warpAffine(image, M=matrix, dsize=(w, h), flags=self.mode, borderMode=self.border_mode, borderValue=self.image_value) mask = cv2.warpAffine(mask, M=matrix, dsize=(w, h), flags=cv2.INTER_NEAREST, borderMode=self.border_mode, borderValue=self.mask_value) return image, mask
Example #11
Source File: utils.py From deep-landmark with BSD 3-Clause "New" or "Revised" License | 5 votes |
def rotate(img, bbox, landmark, alpha): """ given a face with bbox and landmark, rotate with alpha and return rotated face with bbox, landmark (absolute position) """ center = ((bbox.left+bbox.right)/2, (bbox.top+bbox.bottom)/2) rot_mat = cv2.getRotationMatrix2D(center, alpha, 1) img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, img.shape) landmark_ = np.asarray([(rot_mat[0][0]*x+rot_mat[0][1]*y+rot_mat[0][2], rot_mat[1][0]*x+rot_mat[1][1]*y+rot_mat[1][2]) for (x, y) in landmark]) face = img_rotated_by_alpha[bbox.top:bbox.bottom+1,bbox.left:bbox.right+1] return (face, landmark_)
Example #12
Source File: rotate.py From chainer-compiler with MIT License | 5 votes |
def _rotate_cv2(img, angle, expand, fill, interpolation): if interpolation == PIL.Image.NEAREST: cv_interpolation = cv2.INTER_NEAREST elif interpolation == PIL.Image.BILINEAR: cv_interpolation = cv2.INTER_LINEAR elif interpolation == PIL.Image.BICUBIC: cv_interpolation = cv2.INTER_CUBIC _, H, W = img.shape affine_mat = cv2.getRotationMatrix2D((W / 2, H / 2), angle, 1) # Logic borrowed from Pillow if expand: # calculate output size yy = [] xx = [] for y, x in ((0, 0), (H, 0), (H, W), (0, W)): yy.append( affine_mat[1, 0] * x + affine_mat[1, 1] * y + affine_mat[1, 2]) xx.append( affine_mat[0, 0] * x + affine_mat[0, 1] * y + affine_mat[0, 2]) out_H = int(np.ceil(max(yy)) - np.floor(min(yy))) out_W = int(np.ceil(max(xx)) - np.floor(min(xx))) affine_mat[1][2] += out_H / 2 - H / 2 affine_mat[0][2] += out_W / 2 - W / 2 else: out_H = H out_W = W img = img.transpose((1, 2, 0)) img = cv2.warpAffine( img, affine_mat, (out_W, out_H), flags=cv_interpolation, borderValue=fill) if img.ndim == 2: img = img[:, :, None] img = img.transpose((2, 0, 1)) return img
Example #13
Source File: mosse.py From OpenCV-Python-Tutorial with MIT License | 5 votes |
def rnd_warp(a): h, w = a.shape[:2] T = np.zeros((2, 3)) coef = 0.2 ang = (np.random.rand()-0.5)*coef c, s = np.cos(ang), np.sin(ang) T[:2, :2] = [[c,-s], [s, c]] T[:2, :2] += (np.random.rand(2, 2) - 0.5)*coef c = (w/2, h/2) T[:,2] = c - np.dot(T[:2, :2], c) return cv2.warpAffine(a, T, (w, h), borderMode = cv2.BORDER_REFLECT)
Example #14
Source File: hogsvm.py From OpenCV-Python-Tutorial with MIT License | 5 votes |
def deskew(img): m = cv2.moments(img) if abs(m['mu02']) < 1e-2: return img.copy() skew = m['mu11']/m['mu02'] M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]]) img = cv2.warpAffine(img,M,(SZ, SZ),flags=affine_flags) return img ## [deskew] ## [hog]
Example #15
Source File: digits.py From OpenCV-Python-Tutorial with MIT License | 5 votes |
def deskew(img): m = cv2.moments(img) if abs(m['mu02']) < 1e-2: return img.copy() skew = m['mu11']/m['mu02'] M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]]) img = cv2.warpAffine(img, M, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR) return img
Example #16
Source File: asift.py From OpenCV-Python-Tutorial with MIT License | 5 votes |
def affine_skew(tilt, phi, img, mask=None): ''' affine_skew(tilt, phi, img, mask=None) -> skew_img, skew_mask, Ai Ai - is an affine transform matrix from skew_img to img ''' 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
Example #17
Source File: make_tinyimagenet_c.py From robustness with Apache License 2.0 | 5 votes |
def elastic_transform(image, severity=1): IMSIZE = 64 c = [(IMSIZE*0, IMSIZE*0, IMSIZE*0.08), (IMSIZE*0.05, IMSIZE*0.3, IMSIZE*0.06), (IMSIZE*0.1, IMSIZE*0.08, IMSIZE*0.06), (IMSIZE*0.1, IMSIZE*0.03, IMSIZE*0.03), (IMSIZE*0.16, IMSIZE*0.03, IMSIZE*0.02)][severity - 1] image = np.array(image, dtype=np.float32) / 255. shape = image.shape shape_size = shape[:2] # random affine 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 + np.random.uniform(-c[2], c[2], size=pts1.shape).astype(np.float32) M = cv2.getAffineTransform(pts1, pts2) image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101) dx = (gaussian(np.random.uniform(-1, 1, size=shape[:2]), c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32) dy = (gaussian(np.random.uniform(-1, 1, size=shape[:2]), c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32) dx, dy = dx[..., np.newaxis], dy[..., np.newaxis] x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2])) indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1)) return np.clip(map_coordinates(image, indices, order=1, mode='reflect').reshape(shape), 0, 1) * 255 # /////////////// End Distortions /////////////// # /////////////// Further Setup ///////////////
Example #18
Source File: make_imagenet_c_inception.py From robustness with Apache License 2.0 | 5 votes |
def elastic_transform(image, severity=1): c = [(360 * 2, 360 * 0.7, 360 * 0.1), (360 * 2, 360 * 0.08, 360 * 0.2), (360 * 0.05, 360 * 0.01, 360 * 0.02), (360 * 0.07, 360 * 0.01, 360 * 0.02), (360 * 0.12, 360 * 0.01, 360 * 0.02)][severity - 1] image = np.array(image, dtype=np.float32) / 255. shape = image.shape shape_size = shape[:2] # random affine 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 + np.random.uniform(-c[2], c[2], size=pts1.shape).astype(np.float32) M = cv2.getAffineTransform(pts1, pts2) image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101) dx = (gaussian(np.random.uniform(-1, 1, size=shape[:2]), c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32) dy = (gaussian(np.random.uniform(-1, 1, size=shape[:2]), c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32) dx, dy = dx[..., np.newaxis], dy[..., np.newaxis] x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2])) indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1)) return np.clip(map_coordinates(image, indices, order=1, mode='reflect').reshape(shape), 0, 1) * 255 # /////////////// End Distortions /////////////// # /////////////// Further Setup ///////////////
Example #19
Source File: imutils.py From PracticalPythonAndOpenCV_CaseStudies with GNU General Public License v3.0 | 5 votes |
def translate(image, x, y): # Define the translation matrix and perform the translation matrix = np.float32([[1, 0, x], [0, 1, y]]) shifted = cv2.warpAffine(image, matrix, (image.shape[1], image.shape[0])) # Return the translated image return shifted
Example #20
Source File: corruptions.py From robustness with Apache License 2.0 | 5 votes |
def elastic_transform(image, severity=1): c = [(244 * 2, 244 * 0.7, 244 * 0.1), # 244 should have been 224, but ultimately nothing is incorrect (244 * 2, 244 * 0.08, 244 * 0.2), (244 * 0.05, 244 * 0.01, 244 * 0.02), (244 * 0.07, 244 * 0.01, 244 * 0.02), (244 * 0.12, 244 * 0.01, 244 * 0.02)][severity - 1] image = np.array(image, dtype=np.float32) / 255. shape = image.shape shape_size = shape[:2] # random affine 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 + np.random.uniform(-c[2], c[2], size=pts1.shape).astype(np.float32) M = cv2.getAffineTransform(pts1, pts2) image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101) dx = (gaussian(np.random.uniform(-1, 1, size=shape[:2]), c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32) dy = (gaussian(np.random.uniform(-1, 1, size=shape[:2]), c[1], mode='reflect', truncate=3) * c[0]).astype(np.float32) dx, dy = dx[..., np.newaxis], dy[..., np.newaxis] x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2])) indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1)) return np.clip(map_coordinates(image, indices, order=1, mode='reflect').reshape(shape), 0, 1) * 255 # /////////////// End Corruptions ///////////////
Example #21
Source File: cv_detection_right_hand.py From AI-Robot-Challenge-Lab with MIT License | 5 votes |
def __rotate_image_size_corrected(image, angle): # Calculate max size for the rotated template and image offset image_size_height, image_size_width = image.shape image_center_x = image_size_width // 2 image_center_y = image_size_height // 2 # Create rotation matrix rotation_matrix = cv2.getRotationMatrix2D((image_center_x, image_center_y), -angle, 1) # Apply offset new_image_size = int(math.ceil(cv2.norm((image_size_height, image_size_width), normType=cv2.NORM_L2))) rotation_matrix[0, 2] += (new_image_size - image_size_width) / 2 rotation_matrix[1, 2] += (new_image_size - image_size_height) / 2 # Apply rotation to the template image_rotated = cv2.warpAffine(image, rotation_matrix, (new_image_size, new_image_size)) return image_rotated
Example #22
Source File: opencv_functional.py From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License | 5 votes |
def affine(img, angle, translate, scale, shear, interpolation=cv2.INTER_LINEAR, mode=cv2.BORDER_CONSTANT, fillcolor=0): """Apply affine transformation on the image keeping image center invariant Args: img (numpy ndarray): numpy ndarray to be transformed. angle (float or int): rotation angle in degrees between -180 and 180, clockwise direction. translate (list or tuple of integers): horizontal and vertical translations (post-rotation translation) scale (float): overall scale shear (float): shear angle value in degrees between -180 to 180, clockwise direction. interpolation (``cv2.INTER_NEAREST` or ``cv2.INTER_LINEAR`` or ``cv2.INTER_AREA``, ``cv2.INTER_CUBIC``): An optional resampling filter. See `filters`_ for more information. If omitted, it is set to ``cv2.INTER_LINEAR``, for bilinear interpolation. mode (``cv2.BORDER_CONSTANT`` or ``cv2.BORDER_REPLICATE`` or ``cv2.BORDER_REFLECT`` or ``cv2.BORDER_REFLECT_101``) Method for filling in border regions. Defaults to cv2.BORDER_CONSTANT, meaning areas outside the image are filled with a value (val, default 0) val (int): Optional fill color for the area outside the transform in the output image. Default: 0 """ if not _is_numpy_image(img): raise TypeError('img should be numpy Image. Got {}'.format(type(img))) assert isinstance(translate, (tuple, list)) and len(translate) == 2, \ "Argument translate should be a list or tuple of length 2" assert scale > 0.0, "Argument scale should be positive" output_size = img.shape[0:2] center = (img.shape[1] * 0.5 + 0.5, img.shape[0] * 0.5 + 0.5) matrix = _get_affine_matrix(center, angle, translate, scale, shear) if img.shape[2]==1: return cv2.warpAffine(img, matrix, output_size[::-1],interpolation, borderMode=mode, borderValue=fillcolor)[:,:,np.newaxis] else: return cv2.warpAffine(img, matrix, output_size[::-1],interpolation, borderMode=mode, borderValue=fillcolor)
Example #23
Source File: align_images_video.py From df with Mozilla Public License 2.0 | 5 votes |
def transform( image, mat, size, padding=0 ): mat = mat * size mat[:,2] += padding new_size = int( size + padding * 2 ) return cv2.warpAffine( image, mat, ( new_size, new_size ) )
Example #24
Source File: image_augmentation.py From df with Mozilla Public License 2.0 | 5 votes |
def random_warp( in_image ): assert in_image.shape[:2] == (256,256) image = in_image.copy() scale = 5 range_ = numpy.linspace( 128-120, 128+120, scale ) mapx = numpy.broadcast_to( range_, (scale,scale) ) mapy = mapx.T mapx = mapx + numpy.random.normal( size=(scale,scale), scale= 6 ) mapy = mapy + numpy.random.normal( size=(scale,scale), scale= 6 ) interp_mapx = cv2.resize( mapx, (80,80) )[8:72,8:72].astype('float32') interp_mapy = cv2.resize( mapy, (80,80) )[8:72,8:72].astype('float32') warped_image = cv2.remap( image[:,:,:3], interp_mapx, interp_mapy, cv2.INTER_CUBIC ) src_points = numpy.stack( [ mapx.ravel(), mapy.ravel() ], axis=-1 ) dst_points = numpy.mgrid[0:65:16,0:65:16].T.reshape(-1,2) mat = umeyama( src_points, dst_points, True )[0:2] target_image = cv2.warpAffine( image, mat, (64,64) ) target_mask = target_image[:,:,3].reshape((64,64,1)) target_image = target_image[:,:,:3] if len(target_image.shape)>2: return ( warped_image, target_image, target_mask ) else: return ( warped_image, target_image )
Example #25
Source File: align_images_masked.py From df with Mozilla Public License 2.0 | 5 votes |
def transform( image, mat, size, padding=0 ): mat = mat * size mat[:,2] += padding new_size = int( size + padding * 2 ) return cv2.warpAffine( image, mat, ( new_size, new_size ) )
Example #26
Source File: faceMorph.py From face-morphing with MIT License | 5 votes |
def applyAffineTransform(src, srcTri, dstTri, size) : # Given a pair of triangles, find the affine transform. warpMat = cv2.getAffineTransform( np.float32(srcTri), np.float32(dstTri) ) # Apply the Affine Transform just found to the src image dst = cv2.warpAffine( src, warpMat, (size[0], size[1]), None, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101 ) return dst # Warps and alpha blends triangular regions from img1 and img2 to img
Example #27
Source File: map_utils.py From DOTA_models with Apache License 2.0 | 5 votes |
def get_map_to_predict(src_locs, src_x_axiss, src_y_axiss, map, map_size, interpolation=cv2.INTER_LINEAR): fss = [] valids = [] center = (map_size-1.0)/2.0 dst_theta = np.pi/2.0 dst_loc = np.array([center, center]) dst_x_axis = np.array([np.cos(dst_theta), np.sin(dst_theta)]) dst_y_axis = np.array([np.cos(dst_theta+np.pi/2), np.sin(dst_theta+np.pi/2)]) def compute_points(center, x_axis, y_axis): points = np.zeros((3,2),dtype=np.float32) points[0,:] = center points[1,:] = center + x_axis points[2,:] = center + y_axis return points dst_points = compute_points(dst_loc, dst_x_axis, dst_y_axis) for i in range(src_locs.shape[0]): src_loc = src_locs[i,:] src_x_axis = src_x_axiss[i,:] src_y_axis = src_y_axiss[i,:] src_points = compute_points(src_loc, src_x_axis, src_y_axis) M = cv2.getAffineTransform(src_points, dst_points) fs = cv2.warpAffine(map, M, (map_size, map_size), None, flags=interpolation, borderValue=np.NaN) valid = np.invert(np.isnan(fs)) valids.append(valid) fss.append(fs) return fss, valids
Example #28
Source File: imutils.py From PracticalPythonAndOpenCV_CaseStudies with GNU General Public License v3.0 | 5 votes |
def translate(image, x, y): # Define the translation matrix and perform the translation matrix = np.float32([[1, 0, x], [0, 1, y]]) shifted = cv2.warpAffine(image, matrix, (image.shape[1], image.shape[0])) # Return the translated image return shifted
Example #29
Source File: dataset.py From PracticalPythonAndOpenCV_CaseStudies with GNU General Public License v3.0 | 5 votes |
def de_skew(image, width): # Grab the width and height of the image and compute moments for the image (h, w) = image.shape[:2] moments = cv2.moments(image) # De-skew the image by applying an affine transformation skew = moments["mu11"] / moments["mu02"] matrix = np.float32([[1, skew, -0.5 * w * skew], [0, 1, 0]]) image = cv2.warpAffine(image, matrix, (w, h), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR) # Resize the image to have a constant width image = imutils.resize(image, width=width) # Return the de-skewed image return image
Example #30
Source File: imutils.py From PracticalPythonAndOpenCV_CaseStudies with GNU General Public License v3.0 | 5 votes |
def translate(image, x, y): # Define the translation matrix and perform the translation matrix = np.float32([[1, 0, x], [0, 1, y]]) shifted = cv2.warpAffine(image, matrix, (image.shape[1], image.shape[0])) # Return the translated image return shifted