Python cv2 模块,warpAffine() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.warpAffine()

项目:human-pose-estimation-by-deep-learning    作者:HYPJUDY    | 项目源码 | 文件源码
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
项目:DoNotSnap    作者:AVGInnovationLabs    | 项目源码 | 文件源码
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
项目:Farmbot_GeneralAP    作者:SpongeYao    | 项目源码 | 文件源码
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
项目:human-pose-estimation-by-deep-learning    作者:HYPJUDY    | 项目源码 | 文件源码
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
项目:SemiSupervised_itterativeCNN    作者:styloInt    | 项目源码 | 文件源码
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
项目:kaggle-dstl    作者:lopuhin    | 项目源码 | 文件源码
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
项目:prepare-faces-zyf    作者:walkoncross    | 项目源码 | 文件源码
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
项目:prepare-faces-zyf    作者:walkoncross    | 项目源码 | 文件源码
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
项目:prepare-faces-zyf    作者:walkoncross    | 项目源码 | 文件源码
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
项目:Computer-Vision    作者:PratikRamdasi    | 项目源码 | 文件源码
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
项目:diagnose-heart    作者:woshialex    | 项目源码 | 文件源码
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)
项目:tensorflow_lstm_ctc_ocr    作者:linfan    | 项目源码 | 文件源码
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
项目:tensorflow_lstm_ctc_ocr    作者:linfan    | 项目源码 | 文件源码
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
项目:ETS2Autopilot    作者:BrunoTh    | 项目源码 | 文件源码
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]))
项目:CVtools    作者:Tyler-D    | 项目源码 | 文件源码
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)
项目:CVtools    作者:Tyler-D    | 项目源码 | 文件源码
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)
项目:dlcv_for_beginners    作者:frombeijingwithlove    | 项目源码 | 文件源码
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
项目:Simple-User-Input-Sculpture-Generation    作者:ClaireKincaid    | 项目源码 | 文件源码
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
项目:3dstools    作者:ObsidianX    | 项目源码 | 文件源码
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))
项目:iffse    作者:kendricktan    | 项目源码 | 文件源码
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
项目:Defect-Prediction    作者:Jorba123    | 项目源码 | 文件源码
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
项目:HIRAGANA_NN    作者:natureCode    | 项目源码 | 文件源码
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
项目:ppap_detect    作者:ashitani    | 项目源码 | 文件源码
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
项目:ppap_detect    作者:ashitani    | 项目源码 | 文件源码
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,:]
项目:convnet-for-geometric-matching    作者:hjweide    | 项目源码 | 文件源码
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.
项目:convnet-for-geometric-matching    作者:hjweide    | 项目源码 | 文件源码
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
项目:face2movie    作者:Stunkymonkey    | 项目源码 | 文件源码
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
项目:ssd_keras    作者:pierluigiferrari    | 项目源码 | 文件源码
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
项目:ssd_keras    作者:pierluigiferrari    | 项目源码 | 文件源码
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
项目:tensorflow-litterbox    作者:rwightman    | 项目源码 | 文件源码
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
项目:deepstacks    作者:guoxuesong    | 项目源码 | 文件源码
def random_rotate(w,h,angle,scale,*all_inputs):
    if type(angle)==float:
        angle=(-angle,angle)
    if type(scale)==float:
        scale=(1-scale,1+scale)
    cx=(np.random.rand(len(all_inputs[0])).astype(floatX))*w
    cy=(np.random.rand(len(all_inputs[0])).astype(floatX))*h
    actions=(np.random.rand(len(all_inputs[0]),4,1,1)).astype(floatX)
    actions2=np.zeros_like(actions)
    actions2[:,0]=(actions[:,0]*(angle[1]-angle[0])+angle[0]).astype(floatX)
    actions2[:,1]=(actions[:,1]*(scale[1]-scale[0])+scale[0]).astype(floatX)
    actions2[:,2,0,0]=cx
    actions2[:,3,0,0]=cy
    all_outputs=[]
    for inputs in all_inputs:
        outputs=np.zeros(inputs.shape,dtype=floatX)
        for i in range(len(inputs)):
            mat = cv2.getRotationMatrix2D((cx[i],cy[i]),actions2[i,0,0,0],actions2[i,1,0,0])
            tmp = cv2.warpAffine(inputs[i].transpose(1,2,0),mat,inputs[i].shape[1:]).transpose(2,0,1)
            #tmp=np.pad(inputs[i:i+1],((0,0),(0,0),(n,n),(n,n)),mode='constant',constant_values=0)
            #tmp=np.roll(tmp,actions2[i,0,0,0],2)
            #tmp=np.roll(tmp,actions2[i,1,0,0],3)
            outputs[i]=tmp
        all_outputs+=[outputs]
    return all_outputs+[actions2.reshape(len(inputs),4)]
项目:sceneReco    作者:yijiuzai    | 项目源码 | 文件源码
def dumpRotateImage(img,degree,pt1,pt2,pt3,pt4):
    height,width=img.shape[:2]
    heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
    widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))
    matRotation=cv2.getRotationMatrix2D((width/2,height/2),degree,1)
    matRotation[0, 2] += (widthNew - width) / 2
    matRotation[1, 2] += (heightNew - height) / 2
    imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))
    pt1 = list(pt1)
    pt3 = list(pt3)


    [[pt1[0]], [pt1[1]]] = np.dot(matRotation, np.array([[pt1[0]], [pt1[1]], [1]]))
    [[pt3[0]], [pt3[1]]] = np.dot(matRotation, np.array([[pt3[0]], [pt3[1]], [1]]))
    imgOut=imgRotation[int(pt1[1]):int(pt3[1]),int(pt1[0]):int(pt3[0])]
    height,width=imgOut.shape[:2]
    return imgOut
项目:kaggle_ndsb2017    作者:juliandewit    | 项目源码 | 文件源码
def random_translate_img(img, xy_range, border_mode="constant"):
    if random.random() > xy_range.chance:
        return img
    import cv2
    if not isinstance(img, list):
        img = [img]

    org_height, org_width = img[0].shape[:2]
    translate_x = random.randint(xy_range.x_min, xy_range.x_max)
    translate_y = random.randint(xy_range.y_min, xy_range.y_max)
    trans_matrix = numpy.float32([[1, 0, translate_x], [0, 1, translate_y]])

    border_const = cv2.BORDER_CONSTANT
    if border_mode == "reflect":
        border_const = cv2.BORDER_REFLECT

    res = []
    for img_inst in img:
        img_inst = cv2.warpAffine(img_inst, trans_matrix, (org_width, org_height), borderMode=border_const)
        res.append(img_inst)
    if len(res) == 1:
        res = res[0]
    xy_range.last_x = translate_x
    xy_range.last_y = translate_y
    return res
项目:kaggle_ndsb2017    作者:juliandewit    | 项目源码 | 文件源码
def random_rotate_img(img, chance, min_angle, max_angle):
    import cv2
    if random.random() > chance:
        return img
    if not isinstance(img, list):
        img = [img]

    angle = random.randint(min_angle, max_angle)
    center = (img[0].shape[0] / 2, img[0].shape[1] / 2)
    rot_matrix = cv2.getRotationMatrix2D(center, angle, scale=1.0)

    res = []
    for img_inst in img:
        img_inst = cv2.warpAffine(img_inst, rot_matrix, dsize=img_inst.shape[:2], borderMode=cv2.BORDER_CONSTANT)
        res.append(img_inst)
    if len(res) == 0:
        res = res[0]
    return res
项目:Robo-Plot    作者:JackBuck    | 项目源码 | 文件源码
def create_rotated_sub_image(image, centre, search_width, angle_rad):
    # Rotation transform requires x then y.
    M = cv2.getRotationMatrix2D((centre[1], centre[0]), np.rad2deg(angle_rad), 1.0)

    w = image.shape[1]
    h = centre[0] + int((image.shape[0] - centre[0]) * abs(math.sin(angle_rad)))
    rotated = cv2.warpAffine(image, M, (w, h))

    # Centre the last white centroid into the centre of the image.
    half_sub_image_width = int(min(min(search_width, centre[1]),
                                   min(rotated.shape[1] - centre[1], search_width)))

    sub_image = rotated[centre[0]:,
                centre[1] - half_sub_image_width: centre[1] + half_sub_image_width]

    return sub_image
项目:phocnet    作者:ssudholt    | 项目源码 | 文件源码
def create_affine_transform_augmentation(img, random_limits=(0.8, 1.1)):
        '''
        Creates an augmentation by computing a homography from three
        points in the image to three randomly generated points
        '''
        y, x = img.shape[:2]
        fx = float(x)
        fy = float(y)
        src_point = np.float32([[fx/2, fy/3,],
                                [2*fx/3, 2*fy/3],
                                [fx/3, 2*fy/3]])
        random_shift = (np.random.rand(3,2) - 0.5) * 2 * (random_limits[1]-random_limits[0])/2 + np.mean(random_limits)
        dst_point = src_point * random_shift.astype(np.float32)
        transform = cv2.getAffineTransform(src_point, dst_point)
        borderValue = 0
        if img.ndim == 3:
            borderValue = np.median(np.reshape(img, (img.shape[0]*img.shape[1],-1)), axis=0)
        else:
            borderValue=np.median(img)
        warped_img = cv2.warpAffine(img, transform, dsize=(x,y), borderValue=borderValue)
        return warped_img
项目:Notes2ppt    作者:gsengupta2810    | 项目源码 | 文件源码
def rotate_bound(image, angle):
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)

    # grab the rotation matrix (applying the negative of the
    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])

    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))

    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY

    # perform the actual rotation and return the image
    return cv2.warpAffine(image, M, (nW, nH))
项目:pose_estimation    作者:JakeRenn    | 项目源码 | 文件源码
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
项目:pose_estimation    作者:JakeRenn    | 项目源码 | 文件源码
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
项目:QRCodeReader    作者:Griffintaur    | 项目源码 | 文件源码
def Transform(self, PointTop, PointRight, PointBottom):
        Point1 = [PointTop[0], PointTop[1]]
        Point2 = [PointRight[0], PointRight[1]]
        Point3 = [PointBottom[0], PointBottom[1]]
        src = np.float32([Point1, Point2, Point3])
        dest_pointTop = [40, 40]
        dest_pointRight = [140, 40]
        dest_pointBottom = [40, 140]
        destination = np.float32(
            [dest_pointTop, dest_pointRight, dest_pointBottom])
        affineTrans = cv.getAffineTransform(src, destination)
        self.TransformImage = cv.warpAffine(
            self.OriginalImage, affineTrans, self.OriginalImage.shape[:2])
        self.TransformImage = self.TransformImage[0:200, 0:200]
        #cv.imshow("TransformImage",self.TransformImage)            #uncomment to debug
        #cv.waitKey(0)
        #cv.destroyAllWindows()
        return self.TransformImage
项目:deep-anpr    作者:matthewearl    | 项目源码 | 文件源码
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.6,
                            max_scale=0.875,
                            rotation_variation=1.0,
                            scale_variation=1.5,
                            translation_variation=1.2)
    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.0 - 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
项目:FaceSwap    作者:Aravind-Suresh    | 项目源码 | 文件源码
def warp_image(img, tM, shape):
    out = np.zeros(shape, dtype=img.dtype)
    # cv2.warpAffine(img,
    #                tM[:2],
    #                (shape[1], shape[0]),
    #                dst=out,
    #                borderMode=cv2.BORDER_TRANSPARENT,
    #                flags=cv2.WARP_INVERSE_MAP)
    cv2.warpPerspective(img, tM, (shape[1], shape[0]), dst=out,
                        borderMode=cv2.BORDER_TRANSPARENT,
                        flags=cv2.WARP_INVERSE_MAP)
    return out

# TODO: Modify this method to get a better face contour mask
项目:facerecognition    作者:guoxiaolu    | 项目源码 | 文件源码
def align(self, imgDim, rgbImg, bb,
              landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP, scale=1.0):
        r"""align(imgDim, rgbImg, bb=None, landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP)

        Transform and align a face in an image.

        :param imgDim: The edge length in pixels of the square the image is resized to.
        :type imgDim: int
        :param rgbImg: RGB image to process. Shape: (height, width, 3)
        :type rgbImg: numpy.ndarray
        :param bb: Bounding box around the face to align. \
                   Defaults to the largest face.
        :type bb: dlib.rectangle
        :param landmarks: Detected landmark locations. \
                          Landmarks found on `bb` if not provided.
        :type landmarks: list of (x,y) tuples
        :param landmarkIndices: The indices to transform to.
        :type landmarkIndices: list of ints
        :param scale: Scale image before cropping to the size given by imgDim.
        :type scale: float
        :return: The aligned RGB image. Shape: (imgDim, imgDim, 3)
        :rtype: numpy.ndarray
        """
        assert imgDim is not None
        assert rgbImg is not None
        assert landmarkIndices is not None
        assert bb is not None

        bb_dlib = dlib.rectangle(left=bb[0], top=bb[1], right=bb[2], bottom=bb[3])
        if landmarks is None:
            landmarks = self.findLandmarks(rgbImg, bb_dlib)

        npLandmarks = np.float32(landmarks)
        npLandmarkIndices = np.array(landmarkIndices)

        #pylint: disable=maybe-no-member
        H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices],
                                   imgDim * MINMAX_TEMPLATE[npLandmarkIndices]*scale + imgDim*(1-scale)/2)
        thumbnail = cv2.warpAffine(rgbImg, H, (imgDim, imgDim))

        return thumbnail
项目:facerecognition    作者:guoxiaolu    | 项目源码 | 文件源码
def align(self, imgDim, rgbImg, bb,
              landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP, scale=1.0):
        r"""align(imgDim, rgbImg, bb=None, landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP)

        Transform and align a face in an image.

        :param imgDim: The edge length in pixels of the square the image is resized to.
        :type imgDim: int
        :param rgbImg: RGB image to process. Shape: (height, width, 3)
        :type rgbImg: numpy.ndarray
        :param bb: Bounding box around the face to align. \
                   Defaults to the largest face.
        :type bb: dlib.rectangle
        :param landmarks: Detected landmark locations. \
                          Landmarks found on `bb` if not provided.
        :type landmarks: list of (x,y) tuples
        :param landmarkIndices: The indices to transform to.
        :type landmarkIndices: list of ints
        :param scale: Scale image before cropping to the size given by imgDim.
        :type scale: float
        :return: The aligned RGB image. Shape: (imgDim, imgDim, 3)
        :rtype: numpy.ndarray
        """
        assert imgDim is not None
        assert rgbImg is not None
        assert landmarkIndices is not None
        assert bb is not None

        bb_dlib = dlib.rectangle(left=bb[0], top=bb[1], right=bb[2], bottom=bb[3])
        if landmarks is None:
            landmarks = self.findLandmarks(rgbImg, bb_dlib)

        npLandmarks = np.float32(landmarks)
        npLandmarkIndices = np.array(landmarkIndices)

        #pylint: disable=maybe-no-member
        H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices],
                                   imgDim * MINMAX_TEMPLATE[npLandmarkIndices]*scale + imgDim*(1-scale)/2)
        thumbnail = cv2.warpAffine(rgbImg, H, (imgDim, imgDim))

        return thumbnail
项目:lung-cancer-detector    作者:YichenGong    | 项目源码 | 文件源码
def apply_affine(img, mat):
    return cv.warpAffine(img, mat, img.shape, flags=cv.INTER_LINEAR)
项目:Millennium-Eye    作者:Elysium1937    | 项目源码 | 文件源码
def findCorners(contour):
    """blank_image = np.zeros((img.shape[0],img.shape[1],3), np.uint8)
    cv2.drawContours(blank_image, contour, -1, (255, 255, 255))
    rows,cols = img.shape[0], img.shape[1]
    M = cv2.getRotationMatrix2D((cols/2,rows/2),-45,0.5)
    dst = cv2.warpAffine(blank_image,M,(cols,rows))
    cv2.imshow("rotatio", dst)
    cv2.waitKey()"""
    rect = cv2.minAreaRect(contour)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    height_px_1 = box[0][1] - box[3][1]
    height_px_2 = box[1][1] - box[2][1]
    print height_px_1, height_px_2
    if height_px_1 < height_px_2:
        close_height_px = height_px_2
        far_height_px = height_px_1
    else:
        close_height_px = height_px_1
        far_height_px = height_px_2

    return close_height_px, far_height_px
项目:WashingMachine    作者:syangav    | 项目源码 | 文件源码
def rotate_bound(image, angle):
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)

    # grab the rotation matrix (applying the negative of the
    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])

    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))

    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY

    # perform the actual rotation and return the image
    return cv2.warpAffine(image, M, (nW, nH))
项目:sail    作者:GemHunt    | 项目源码 | 文件源码
def create_design_data_set(labeled_designs,design_crop_dir,image_dir,test):
    labels = ['heads','tails']
    if test:
        pixels_to_jitter = 0
        angles = 1
    else:
        pixels_to_jitter = 2
        angles = 100

    for label in labels:
        dir = image_dir + label + '/'
        if not os.path.exists(dir):
            os.makedirs(dir)

    for coin_id, label in labeled_designs.iteritems():
        before_rotate_size = 56
        for image_id in range(0,56):
            #dir = design_crop_dir + str(coin_id / 100) + '/'
            class_dir = image_dir + label + '/'
            #for angle in range(0,10):
            filename = str(coin_id).zfill(5) + str(image_id).zfill(2) + '.png'
            image = cv2.imread(design_crop_dir + filename)
            image = cv2.resize(image, (before_rotate_size, before_rotate_size), interpolation=cv2.INTER_AREA)
            for count in range(0,angles):
                angle = random.random() * 360
                center_x = before_rotate_size / 2 + (random.random() * pixels_to_jitter * 2) - pixels_to_jitter
                center_y = before_rotate_size / 2 + (random.random() * pixels_to_jitter * 2) - pixels_to_jitter
                rot_image = image.copy()
                m = cv2.getRotationMatrix2D((center_x, center_y), angle, 1)
                cv2.warpAffine(rot_image, m, (before_rotate_size, before_rotate_size), rot_image, cv2.INTER_CUBIC)
                # This is hard coded for 28x28.
                rot_image = cv2.resize(rot_image, (41, 41), interpolation=cv2.INTER_AREA)
                rot_image = rot_image[6:34, 6:34]
                rotated_filename = filename.replace('.png', str(count).zfill(2) + '.png')
                cv2.imwrite(class_dir + rotated_filename,rot_image)
    sys.exit()
项目:sail    作者:GemHunt    | 项目源码 | 文件源码
def get_rotated_crop(crop_dir, crop_id, crop_size, angle):
    filename = get_filename_from(crop_id,crop_dir)
    crop = cv2.imread(filename)
    if crop == None:
        print crop_id, 'None'
        return None

    crop = cv2.resize(crop, (crop_size, crop_size), interpolation=cv2.INTER_AREA)
    if angle == None:
        angle = 0

    print crop_dir, crop_id, crop_size, angle
    m = cv2.getRotationMatrix2D((crop_size / 2, crop_size / 2), angle, 1)
    cv2.warpAffine(crop, m, (crop_size, crop_size), crop, cv2.INTER_CUBIC)
    return crop
项目:GeneGAN    作者:Prinsphield    | 项目源码 | 文件源码
def align_2p(img, left_eye, right_eye):
    width = 256
    eye_width = 70

    transform = np.matrix([
            [1, 0, left_eye[0]],
            [0, 1, left_eye[1]],
            [0, 0, 1]
    ], dtype='float')

    th = np.pi + -np.arctan2(left_eye[1] - right_eye[1], left_eye[0] - right_eye[0])
    transform *= np.matrix([
            [np.cos(th), np.sin(th), 0],
            [-np.sin(th), np.cos(th), 0],
            [0, 0, 1]
    ], dtype='float')

    scale = np.sqrt((left_eye[1] - right_eye[1]) ** 2 + (left_eye[0] - right_eye[0]) ** 2) / eye_width
    transform *= np.matrix([
            [scale, 0, 0],
            [0, scale, 0],
            [0, 0, 1]
    ], dtype='float')

    transform *= np.matrix([
            [1, 0, -(width - eye_width) / 2],
            [0, 1, -width / 2.42],
            [0, 0, 1]
    ], dtype='float')

    transform = np.linalg.inv(transform)
    jmg = cv2.warpAffine(img, transform[:2], (width, width))
    return jmg