def rotate(image, angle, center=None, scale=1.0): assert image.dtype == np.uint8 """Copy paste of imutils method but with INTER_NEAREST and BORDER_CONSTANT flags""" # grab the dimensions of the image (h, w) = image.shape[:2] # if the center is None, initialize it as the center of # the image if center is None: center = (w // 2, h // 2) # perform the rotation M = cv.getRotationMatrix2D(center, angle, scale) rotated = cv.warpAffine( image, M, (w, h), flags=cv.INTER_NEAREST, borderMode=cv.BORDER_CONSTANT, borderValue=0, ) # return the rotated image return rotated
def rotate_90(self, image): #cv2.resize(image, (800, 800)) #cv2.imshow('dddimage', image) #cv2.waitKey(0) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.bitwise_not(gray) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] coords = np.column_stack(np.where(thresh > 0)) angle = cv2.minAreaRect(coords)[-1] if angle < -45: angle = -(90 + angle) else: angle = -angle angle = 90 (h, w) = image.shape[:2] #print(h, w) center = (w / 2, h / 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(image, M, (w, h)) #cv2.resize(rotated, (800, 1000)) #cv2.imshow('rotated', rotated) #cv2.waitKey(0) file_path_name = 'img/rotate_img.jpg' cv2.imwrite(file_path_name, rotated) return file_path_name
def deskew(img): skew_img = cv2.bitwise_not(img) # Invert image # grab the (x, y) coordinates of all pixel values that # are greater than zero, then use these coordinates to # compute a rotated bounding box that contains all # coordinates coords = np.column_stack(np.where(skew_img > 0)) angle = cv2.minAreaRect(coords)[-1] # the `cv2.minAreaRect` function returns values in the # range [-90, 0); as the rectangle rotates clockwise the # returned angle trends to 0 -- in this special case we # need to add 90 degrees to the angle if angle < -45: angle = -(90 + angle) # otherwise, just take the inverse of the angle to make # it positive else: angle = -angle # rotate the image to deskew it (h, w) = img.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) return angle, rotated
def correct_skew(image, delta=1, limit=5): def determine_score(arr, angle): data = inter.rotate(arr, angle, reshape=False, order=0) histogram = np.sum(data, axis=1) score = np.sum((histogram[1:] - histogram[:-1]) ** 2) return histogram, score gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] scores = [] angles = np.arange(-limit, limit + delta, delta) for angle in angles: histogram, score = determine_score(thresh, angle) scores.append(score) best_angle = angles[scores.index(max(scores))] (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, best_angle, 1.0) rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, \ borderMode=cv2.BORDER_REPLICATE) return best_angle, rotated
def random_image_rotate(img, rotation_center): angle_deg = _random_normal_crop(1, 10)[0] M = cv2.getRotationMatrix2D(rotation_center, angle_deg, 1.0) nimg = cv2.warpAffine(img, M, dsize=img.shape[0:2]) if len(nimg.shape) < 3: nimg = nimg[:, :, np.newaxis] return nimg #.reshape(img.shape)
def rotate_image(img, angle, crop): h, w = img.shape[:2] # 旋转角度的周期是360° angle %= 360 # 用OpenCV内置函数计算仿射矩阵 M_rotate = cv.getRotationMatrix2D((w / 2, h / 2), angle, 1) # 得到旋转后的图像 img_rotated = cv.warpAffine(img, M_rotate, (w, h)) # 如果需要裁剪去除黑边 if crop: angle_crop = angle % 180 # 对于裁剪角度的等效周期是180° if angle_crop > 90: # 并且关于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
def get_frame(self) -> np.array: # calculate the ROI points around self.pos x_0, x_1, y_0, y_1 = self.calculate_ROI() # add border to the ROI y_0 += int(self.crop_dimension / 2) x_0 += int(self.crop_dimension / 2) y_1 += int(self.crop_dimension * 3 / 2) x_1 += int(self.crop_dimension * 3 / 2) # crop out and copy of the area surrounding self.pos plus a border frame_to_rotate = self.borderd_picture[y_0:y_1, x_0:x_1].copy() # rotate the frame to the given viewangle and extract the resultframe shape = frame_to_rotate.shape rotation = cv2.getRotationMatrix2D((shape[0] / 2, shape[1] / 2), self.angle, 1) # avoid overflow self.avoid_overflow() # Perform actual rotation on cropped part of the image using the rotation matrix. # This will cut off parts of the image that don't fit a recangular bound (worst case: 45° rotation). # This is why we added a border to the cropped out area. result = cv2.warpAffine(frame_to_rotate, rotation, (self.cols, self.rows)) # crop out the actual area of self.crop_dimension, cutting away the border return result[int(self.crop_dimension / 2 + self.aspect_ratio_diff):int(self.crop_dimension * 3 / 2), int(self.crop_dimension / 2):int(self.crop_dimension * 3 / 2)]
def rotate_image(self, image, angle): """ Rotates the given image by the input angle in the counter-clockwise direction Parameters ---------- image : ndim np.array image to be rotated angle : float angle of rotation as degrees. Returns ------- rotated image as np.array """ # create an tuple that contains height/2, width/2 image_center = tuple(np.array(image.shape[1::-1]) / 2) # rot_mat 2x3 rotation mattrix rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0) # apply the affine transformation to the image # size of the output image image.shape[1::-1] result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR) return result
def alignment_matrix(self) -> np.array: """ Returns a transformation matrix which can be used to align images based on the position of the eyes. """ # Get the center of the eyes left_eye = Point.mean(self.left_eye) right_eye = Point.mean(self.right_eye) face_square = self.square() # Compute tilt delta_y = right_eye.y - left_eye.y delta_x = right_eye.x - left_eye.x angle = np.degrees(np.arctan2(delta_y, delta_x)) # Normalized eye positions out_left_eye_x, out_left_eye_y = 0.3, 0.2 out_right_eye_x, out_right_eye_y = 1.0 - out_left_eye_x, 1.0 - out_left_eye_y # Compute scale of output image dist = np.sqrt((delta_x**2) + (delta_y**2)) out_dist = (out_right_eye_x - out_left_eye_x) * face_square.width scale = out_dist / dist # Compute rotation center point eyes_center = Point.mean([left_eye, right_eye]) # Compute rotation matrix matrix = cv2.getRotationMatrix2D(eyes_center, angle, scale) # Update translation components matrix[0, 2] += (face_square.width * 0.5 - eyes_center.x) matrix[1, 2] += (face_square.height * out_left_eye_y - eyes_center.y) return matrix
def cropRois(image, rects, multHeight=0.73, multWidth=0.97, topHeightCrop=30): crops = [] data = {} # TODO cut off angle outliers here too angles = [] for r in rects: box = rect2Box(r) W = r[1][0] H = r[1][1] Xs = [i[0] for i in box] Ys = [i[1] for i in box] x1 = min(Xs) x2 = max(Xs) y1 = min(Ys) y2 = max(Ys) rotated = False angle = r[2] if angle < -45: angle += 90 rotated = True # calc the centroid center = (int((x1 + x2) / 2), int((y1 + y2) / 2)) size = (int((x2 - x1)), int((y2 - y1))) #cv2.circle(image, center, 2, 255, -1) M = cv2.getRotationMatrix2D((size[0] / 2, size[1] / 2), angle, 1.0) # prepare the crop cropped = cv2.getRectSubPix(image, size, center) cropped = cv2.warpAffine(cropped, M, size) croppedW = W if not rotated else H croppedH = H if not rotated else W ratio = float(croppedW) / (croppedH) area = float(croppedW) * croppedH # if in the ratio if (ratio > 2 and ratio < 16): #text = "{0:.2f}-{1:.2f} ".format(ratio, area) #cv2.putText(image, text, center, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) croppedRotated = cv2.getRectSubPix( cropped, (int(croppedW * multWidth), int(croppedH * multHeight if croppedH < topHeightCrop else croppedH * 0.9)), (size[0] / 2, size[1] / 2)) # save the angles to calc the avg/std angles.append(angle) # save the crops crops.append(croppedRotated) # will process from top to bottom, so save it to sort later data[y1] = [croppedRotated, area, ratio, angle] return data, np.mean(np.array(angles)), np.std(np.array(angles))
def rotate(xb, yb, angle): M_rotate = cv2.getRotationMatrix2D((size / 2, size / 2), angle, 1) xb = cv2.warpAffine(xb, M_rotate, (size, size)) yb = cv2.warpAffine(yb, M_rotate, (size, size)) return xb, yb
def rotate_image(image, angle): image_center = tuple(np.array(image.shape[1::-1]) / 2) rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0) result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR) return result
def rotate(img, angle, rotPoint=None): (height, width) = img.shape[:2] if rotPoint is None: rotPoint = (width//2, height//2) rotMat = cv.getRotationMatrix2D(rotPoint, angle, 1.0) dimensions = (width, height) return cv.warpAffine(img, rotMat, dimensions)
def crop_and_show_images(): show_images = True video_path = Path( "/Users/sebastian/PycharmProjects/forgerydetection/722_458") with open(video_path / "relative_bb.json", "r") as f: relative_bb = json.load(f) shape_predictor = dlib.shape_predictor( "/Users/sebastian/PycharmProjects/forgerydetection/shape_predictor_68_face_landmarks.dat" ) for image in tqdm(sorted(video_path.glob("*.png"))): img = cv2.imread(str(image)) total_width, total_height = len(img[0]), len(img) # show video_crop image if show_images: cv2.imshow(str(image.name), img) cv2.waitKey(0) cv2.destroyAllWindows() relative_bb_values = relative_bb[str(image.with_suffix("").name)] x, y, w, h = calculate_relative_bb(total_width, total_height, relative_bb_values) # show local bb_image new_image = img[y:y + w, x:x + h] if show_images: cv2.imshow(str(image.name), new_image) cv2.waitKey(0) cv2.destroyAllWindows() # show aligned global_image gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) box = dlib.rectangle(left=x, top=y, right=x + w, bottom=y + h) shape = shape_predictor(gray, box) shape = shape_to_np(shape) left_eye = np.mean(shape[36:42], axis=0) right_eye = np.mean(shape[42:48], axis=0) dx, dy = right_eye - left_eye # Calculate rotation angle with x & y component of the eyes vector angle = np.rad2deg(np.arctan2(dy, dx)) center = np.mean(shape, axis=0) R = cv2.getRotationMatrix2D(tuple(center), angle, 1.0) aligned_image = cv2.warpAffine(img, R, (total_width, total_height)) if show_images: cv2.imshow(str(image.name), aligned_image) cv2.waitKey(0) cv2.destroyAllWindows() aligned_image = aligned_image[y:y + w, x:x + h] if show_images: cv2.imshow(str(image.name), aligned_image) cv2.waitKey(0) cv2.destroyAllWindows()
def rotate(image, angle, center=None, scale=1.0): (h, w) = image.shape[:2] if center is None: center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, scale) rotated = cv2.warpAffine(image, M, (w, h)) return rotated
def rotation(image, angle): __img = cv.imread(image) __rows,__cols,__colors = __img.shape __M = cv.getRotationMatrix2D((__cols/2,__rows/2),angle,1) __img = cv.warpAffine(__img,__M,(__cols,__rows)) return __img
def rotate(self): angle = np.random.randint(-25, 25) rows, cols = self.input_image.shape m = getRotationMatrix2D(center=(cols / 2, rows / 2), angle=angle, scale=1) return warpAffine(self.input_image, m, (cols, rows)), warpAffine(self.label, m, (cols, rows))
def nDegreeRotation(self, image, degree): """ NDEGREEROTATION FUNCTION """ (rows, cols) = image.shape[:2] matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), degree, 1) rotation = cv2.warpAffine(image, matrix, (cols, rows), borderValue=(255, 255, 255)) return rotation
def motion_blur(gray, degree, angle): gray = np.array(gray) M = cv2.getRotationMatrix2D((round(degree / 2), round(degree / 2)), angle, 1) motion_blur_kernel = np.diag(np.ones(degree)) motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree)) PSF = motion_blur_kernel / degree blurred = cv2.filter2D(gray, -1, PSF) blurred = cv2.normalize(blurred,None, 0, 255, cv2.NORM_MINMAX) blurred = np.array(blurred, dtype=np.uint8) return blurred,PSF
def rotate(self, image, angle=0, center=None, scale=0.9): #rotate the image with "angle" rows, cols = image.shape[:2] while angle == 0: angle = random.randrange(-180, 180) if center is None: center = (cols / 2, rows / 2) #calculated the matrix Martrix_rot = cv2.getRotationMatrix2D(center, angle, scale) rotated = cv2.warpAffine(image, Martrix_rot, (cols, rows)) return rotated
def deskew(image): coords = np.column_stack(np.where(image > 0)) angle = cv2.minAreaRect(coords)[-1] if angle < -45: angle = -(90 + angle) else: angle = -angle (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) return rotated
def rot(image,image_width,image_height,angle): image_heightNew = int(image_width * fabs(sin(radians(angle))) + image_height * fabs(cos(radians(angle)))) image_widthNew = int(image_height * fabs(sin(radians(angle))) + image_width * fabs(cos(radians(angle)))) center = (image_width/2,image_height/2) M = cv2.getRotationMatrix2D(center, angle, 1) M[0,2] += (image_widthNew - image_width)/2 M[1,2] += (image_heightNew - image_height)/2 rotated = cv2.warpAffine(image,M,(image_widthNew, image_heightNew), borderValue = (255, 255, 255)) return rotated
def rotate(img, angle, scale=1.0): #旋转 height, width = img.shape[:2] #获取图像的高和宽 center = (width / 2, height / 2) #取图像的中点 M = cv.getRotationMatrix2D(center, angle, scale) #获得图像绕着某一点的旋转矩阵 rotated = cv.warpAffine(img, M, (height, width)) #cv.warpAffine()的第二个参数是变换矩阵,第三个参数是输出图像的大小 # cv.imshow("overturn-hv", rotated) # cv.waitKey(0) # cv.destroyAllWindows() return rotated
def image_transformation(file): # open image and apply filter img = cv2.imdecode(numpy.frombuffer(file, numpy.uint8), cv2.IMREAD_UNCHANGED) img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) kernel = numpy.ones((1, 1), numpy.uint8) img = cv2.dilate(img, kernel, iterations=1) img = cv2.erode(img, kernel, iterations=1) img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) # get coordinates coords = numpy.column_stack(numpy.where(img > 0)) angle = cv2.minAreaRect(coords)[-1] # the `cv2.minAreaRect` function returns values in the # range [-90, 0); as the rectangle rotates clockwise the # returned angle trends to 0 -- in this special case we # need to add 90 degrees to the angle if angle < -45: angle = -(90 + angle) # otherwise, just take the inverse of the angle to make # it positive else: angle = -angle (h, w) = img.shape[:2] center = (w // 2, h // 2) m = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(img, m, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) img = rotated ret, thresh_binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) value_thresh_binary = cv2.Laplacian(thresh_binary, cv2.CV_64F).var() blur = cv2.GaussianBlur(img, (5, 5), 0) # blur = gaussian_blur(img, 5) # img = otsu(blur) ret3, thresh_otsu = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) value_otsu = cv2.Laplacian(thresh_otsu, cv2.CV_64F).var() if value_thresh_binary > value_otsu: print(value_thresh_binary) img = thresh_binary else: print(value_otsu) img = thresh_otsu return img
def __make_batch(self, data): if data is not None: batch = [] resized_batch = [] for im_p in data: original_image = cv.imread(im_p) if self.__augmentation_settings: if random.random( ) >= self.__augmentation_settings.blur_chance: original_image = cv.GaussianBlur( original_image, (3, 3), self.__augmentation_settings.blur_amount) if random.random( ) >= self.__augmentation_settings.flip_chance: original_image = cv.flip(original_image, random.randint(-1, 1)) if random.random( ) >= self.__augmentation_settings.rotation_chance: rows, cols, c = original_image.shape M = cv.getRotationMatrix2D( (cols / 2, rows / 2), random.random() * self.__augmentation_settings.rotation_ammount, 1) original_image = cv.warpAffine(original_image, M, (cols, rows)) batch.append( cv.cvtColor(original_image, cv.COLOR_BGR2RGB) / 127.5 - 1.0) if self.__secondary_size: resized_batch.append( cv.cvtColor( cv.resize( original_image, dsize=(self.__secondary_size[0], self.__secondary_size[1]), interpolation=(cv.INTER_AREA if ( original_image.shape[0] > self. __secondary_size[1] and original_image. shape[1] > self.__secondary_size[0] ) else cv.INTER_CUBIC)), cv.COLOR_BGR2RGB) / 127.5 - 1.0) if batch: self.__batches.append(np.array(batch).astype(np.float32)) if resized_batch: self.__resized_batches.append( np.array(resized_batch).astype(np.float32))
def img_rotate(img, angle): ''' @param:原始图像 @param:旋转角度 return dstimg,M ''' h, w, = img.shape[0], img.shape[1] cx, cy = int(w / 2), int(h / 2) # 仿射变换矩阵 M = cv2.getRotationMatrix2D((cx, cy), angle, 1) rotated = cv2.warpAffine(img, M, (w, h)) # cv2.imshow('rotate',rotated) # cv2.waitKey() return rotated, M
def draw_image(src, dst, x, y, rotation): (h, w) = src.shape[:2] bounds = np.array([[-w / 2, -h / 2], [w / 2, -h / 2], [w / 2, h / 2], [-w / 2, h / 2]]) for i, v in enumerate(bounds): bounds[i] = rotate(v, rotation) + (x, y) x_min, x_max = min(np.concatenate(bounds[:, :1])), max( np.concatenate(bounds[:, :1])) y_min, y_max = min(np.concatenate(bounds[:, 1:])), max( np.concatenate(bounds[:, 1:])) M = cv2.getRotationMatrix2D((w / 2, h / 2), np.rad2deg(rotation), 1.0) src_rotated = cv2.warpAffine(src, M, (int(x_max - x_min), int(y_max - y_min))) (h, w) = src_rotated.shape[:2] blit_image(src_rotated, dst, int(x - w / 2), int(y - h / 2))
def __rotation(self, image, angle, center=None, scale=1.0): """ 旋转图片 :param image:待处理图像 :param angle:角度 :param center:中心点 :param scale: :return:the rotated image """ (height, width) = image.shape[:2] # 长宽 if center is None: # 默认中心为中点 center = (width // 2, height // 2) Matrix = getRotationMatrix2D(center, angle, scale) rotated = warpAffine(image, Matrix, (width, height)) return rotated
def dataset_augmentation(images_array): dataset = [] for image in images_array: horizontal_flipped_img = cv2.flip(image, 1) dataset.append(horizontal_flipped_img) vertical_flipped_img = cv2.flip(image, 0) dataset.append(vertical_flipped_img) angles = [10, 15, 20] for angle in angles: height, width = image.shape[:2] matrix = cv2.getRotationMatrix2D((int(width/2), int(height/2)), angle, 1) rotated_img = cv2.warpAffine(image, matrix, (width, height)) dataset.append(rotated_img) return np.array(dataset)
def imageRotate(image, angle, central): #图像旋转操作, 同时返回图像中心旋转后的坐标 row, col = image.shape[:2] # print(image.shape) #getRotationMatrix2D(),这个函数需要三个参数, ang = angle - 90 #旋转中心,旋转角度(逆 时针),旋转后图像的缩放比例,比如下例为1: M = cv2.getRotationMatrix2D((central[0], central[1]), ang, 1) #第一个参数为输入图像,第二个参数为仿射变换矩阵,第三个参数为变换后大小,第四个参数为边界外填充颜色 dst = cv2.warpAffine(image, M, (row, col), borderValue=(255, 255, 255)) central = np.array(central) central = np.r_[central, [1]] trans_c = np.matmul(np.array(M), central.T) ret = tuple(trans_c) ret1 = int(ret[0]) ret2 = int(ret[1]) return dst, (ret1, ret2)