Python cv2.transform() Examples

The following are 23 code examples of cv2.transform(). 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: functional.py    From albumentations with MIT License 7 votes vote down vote up
def keypoint_rotate(keypoint, angle, rows, cols, **params):
    """Rotate a keypoint by angle.

    Args:
        keypoint (tuple): A keypoint `(x, y, angle, scale)`.
        angle (float): Rotation angle.
        rows (int): Image height.
        cols (int): Image width.

    Returns:
        tuple: A keypoint `(x, y, angle, scale)`.

    """
    matrix = cv2.getRotationMatrix2D(((cols - 1) * 0.5, (rows - 1) * 0.5), angle, 1.0)
    x, y, a, s = keypoint[:4]
    x, y = cv2.transform(np.array([[[x, y]]]), matrix).squeeze()
    return x, y, a + math.radians(angle), s 
Example #2
Source File: rectangle_tracker.py    From python-opencv-rectangle-tracker with Apache License 2.0 6 votes vote down vote up
def affTransform(pts, A):
    """Transforms a list of points, `pts`,
    using the affine transform `A`."""
    src = np.zeros((len(pts), 1, 2))
    src[:, 0] = pts
    dst = cv2.transform(src, A)
    return np.array(dst[:, 0, :], dtype='float32') 
Example #3
Source File: image_process.py    From python_grabber with MIT License 6 votes vote down vote up
def sepia(img):
    kernel = np.float32([
        [0.272, 0.534, 0.131],
        [0.349, 0.686, 0.168],
        [0.393, 0.769, 0.189]])
    return cv2.transform(img, kernel) 
Example #4
Source File: auto_border_utils.py    From python_video_stab with MIT License 6 votes vote down vote up
def extreme_corners(frame, transforms):
    """Calculate max drift of each frame corner caused by stabilizing transforms

    :param frame: frame from video being stabilized
    :param transforms: VidStab transforms attribute
    :return: dictionary of most extreme x and y values caused by transformations
    """
    h, w = frame.shape[:2]
    frame_corners = np.array([[0, 0],  # top left
                              [0, h - 1],  # bottom left
                              [w - 1, 0],  # top right
                              [w - 1, h - 1]],  # bottom right
                             dtype='float32')
    frame_corners = np.array([frame_corners])

    min_x = min_y = max_x = max_y = 0
    for i in range(transforms.shape[0]):
        transform = transforms[i, :]
        transform_mat = vidstab_utils.build_transformation_matrix(transform)
        transformed_frame_corners = cv2.transform(frame_corners, transform_mat)

        delta_corners = transformed_frame_corners - frame_corners

        delta_y_corners = delta_corners[0][:, 1].tolist()
        delta_x_corners = delta_corners[0][:, 0].tolist()
        min_x = min([min_x] + delta_x_corners)
        min_y = min([min_y] + delta_y_corners)
        max_x = max([max_x] + delta_x_corners)
        max_y = max([max_y] + delta_y_corners)

    return {'min_x': min_x, 'min_y': min_y, 'max_x': max_x, 'max_y': max_y} 
Example #5
Source File: faces_detect.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def original_roi(self):
        """ :class: `numpy.ndarray`: The original region of interest of the mask in the
        source frame. """
        points = np.array([[0, 0],
                           [0, self.stored_size - 1],
                           [self.stored_size - 1, self.stored_size - 1],
                           [self.stored_size - 1, 0]], np.int32).reshape((-1, 1, 2))
        matrix = cv2.invertAffineTransform(self._affine_matrix)
        roi = cv2.transform(points, matrix).reshape((4, 2))
        logger.trace("Returning: %s", roi)
        return roi 
Example #6
Source File: functional.py    From albumentations with MIT License 5 votes vote down vote up
def linear_transformation_rgb(img, transformation_matrix):
    result_img = cv2.transform(img, transformation_matrix)

    return result_img 
Example #7
Source File: functional.py    From albumentations with MIT License 5 votes vote down vote up
def keypoint_shift_scale_rotate(keypoint, angle, scale, dx, dy, rows, cols, **params):
    x, y, a, s, = keypoint[:4]
    height, width = rows, cols
    center = (width / 2, height / 2)
    matrix = cv2.getRotationMatrix2D(center, angle, scale)
    matrix[0, 2] += dx * width
    matrix[1, 2] += dy * height

    x, y = cv2.transform(np.array([[[x, y]]]), matrix).squeeze()
    angle = a + math.radians(angle)
    scale = s * scale

    return x, y, angle, scale 
Example #8
Source File: apply_image_effects.py    From aws-builders-fair-projects with Apache License 2.0 5 votes vote down vote up
def sepia_cv(image_path:str)->Image:
    """
    Optimization on the sepia filter using cv2 
    """

    image = Image.open(image_path)

    # Load the image as an array so cv knows how to work with it
    img = np.array(image)

    # Apply a transformation where we multiply each pixel rgb 
    # with the matrix for the sepia

    filt = cv2.transform( img, np.matrix([[ 0.393, 0.769, 0.189],
                                          [ 0.349, 0.686, 0.168],
                                          [ 0.272, 0.534, 0.131]                                  
    ]) )

    # Check wich entries have a value greather than 255 and set it to 255
    filt[np.where(filt>255)] = 255

    # Create an image from the array 
    img_sepia = Image.fromarray(filt)
    print(type(img_sepia))
    cv2.imshow("image", img_sepia)

    return

#--- 
Example #9
Source File: LandmarksProcessor.py    From DeepFaceLab with GNU General Public License v3.0 5 votes vote down vote up
def transform_points(points, mat, invert=False):
    if invert:
        mat = cv2.invertAffineTransform (mat)
    points = np.expand_dims(points, axis=1)
    points = cv2.transform(points, mat, points.shape)
    points = np.squeeze(points)
    return points 
Example #10
Source File: _base.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def _rotate_face(face, rotation_matrix):
        """ Rotates the detection bounding box around the given rotation matrix.

        Parameters
        ----------
        face: :class:`DetectedFace`
            A :class:`DetectedFace` containing the `x`, `w`, `y`, `h` detection bounding box
            points.
        rotation_matrix: numpy.ndarray
            The rotation matrix to rotate the given object by.

        Returns
        -------
        :class:`DetectedFace`
            The same class with the detection bounding box points rotated by the given matrix.
        """
        logger.trace("Rotating face: (face: %s, rotation_matrix: %s)", face, rotation_matrix)
        bounding_box = [[face.left, face.top],
                        [face.right, face.top],
                        [face.right, face.bottom],
                        [face.left, face.bottom]]
        rotation_matrix = cv2.invertAffineTransform(rotation_matrix)

        points = np.array(bounding_box, "int32")
        points = np.expand_dims(points, axis=0)
        transformed = cv2.transform(points, rotation_matrix).astype("int32")
        rotated = transformed.squeeze()

        # Bounding box should follow x, y planes, so get min/max for non-90 degree rotations
        pt_x = min([pnt[0] for pnt in rotated])
        pt_y = min([pnt[1] for pnt in rotated])
        pt_x1 = max([pnt[0] for pnt in rotated])
        pt_y1 = max([pnt[1] for pnt in rotated])
        width = pt_x1 - pt_x
        height = pt_y1 - pt_y

        face.x = int(pt_x)
        face.y = int(pt_y)
        face.w = int(width)
        face.h = int(height)
        return face 
Example #11
Source File: aligner.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def get_original_roi(self, mat, size, padding=0):
        """ Return the square aligned box location on the original image """
        logger.trace("matrix: %s, size: %s. padding: %s", mat, size, padding)
        matrix = self.transform_matrix(mat, size, padding)
        points = np.array([[0, 0], [0, size - 1], [size - 1, size - 1], [size - 1, 0]], np.int32)
        points = points.reshape((-1, 1, 2))
        matrix = cv2.invertAffineTransform(matrix)
        logger.trace("Returning: (points: %s, matrix: %s", points, matrix)
        return cv2.transform(points, matrix) 
Example #12
Source File: aligner.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def transform_points(self, points, mat, size, padding=0):
        """ Transform points along matrix """
        logger.trace("points: %s, matrix: %s, size: %s. padding: %s", points, mat, size, padding)
        matrix = self.transform_matrix(mat, size, padding)
        points = np.expand_dims(points, axis=1)
        points = cv2.transform(points, matrix, points.shape)
        retval = np.squeeze(points)
        logger.trace("Returning: %s", retval)
        return retval 
Example #13
Source File: aligner.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def transform(self, image, mat, size, padding=0):
        """ Transform Image """
        logger.trace("matrix: %s, size: %s. padding: %s", mat, size, padding)
        matrix = self.transform_matrix(mat, size, padding)
        interpolators = get_matrix_scaling(matrix)
        retval = cv2.warpAffine(image, matrix, (size, size), flags=interpolators[0])
        return retval 
Example #14
Source File: aligner.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def extract(self, image, face, size):
        """ Extract a face from an image """
        logger.trace("size: %s", size)
        padding = int(size * 0.1875)
        alignment = get_align_mat(face)
        extracted = self.transform(image, alignment, size, padding)
        logger.trace("Returning face and alignment matrix: (alignment_matrix: %s)", alignment)
        return extracted, alignment 
Example #15
Source File: faces_detect.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def _adjust_affine_matrix(self, mask_size, affine_matrix):
        """ Adjust the affine matrix for the mask's storage size

        Parameters
        ----------
        mask_size: int
            The original size of the mask.
        affine_matrix: numpy.ndarray
            The affine matrix to transform the mask at original size to the parent frame.

        Returns
        -------
        affine_matrix: numpy,ndarray
            The affine matrix adjusted for the mask at its stored dimensions.
        """
        zoom = self.stored_size / mask_size
        zoom_mat = np.array([[zoom, 0, 0.], [0, zoom, 0.]])
        adjust_mat = np.dot(zoom_mat, np.concatenate((affine_matrix, np.array([[0., 0., 1.]]))))
        logger.trace("storage_size: %s, mask_size: %s, zoom: %s, original matrix: %s, "
                     "adjusted_matrix: %s", self.stored_size, mask_size, zoom, affine_matrix.shape,
                     adjust_mat.shape)
        return adjust_mat 
Example #16
Source File: faces_detect.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def load_reference_face(self, image, size=64, coverage_ratio=0.625, dtype=None):
        """ Align a face in the correct dimensions for reference against the output from a model.

        Parameters
        ----------
        image: numpy.ndarray
            The image that contains the face to be aligned
        size: int
            The size of the face in pixels to be fed into the model
        coverage_ratio: float, optional
            the ratio of the extracted image that was used for training. Default: `0.625`
        dtype: str, optional
            Optionally set a ``dtype`` for the final face to be formatted in. Default: ``None``

        Notes
        -----
        This method must be executed to get access to the following `properties`:
            - :func:`reference_face`
            - :func:`reference_landmarks`
            - :func:`reference_matrix`
            - :func:`reference_interpolators`
        """
        logger.trace("Loading reference face: (size: %s, coverage_ratio: %s, dtype: %s)",
                     size, coverage_ratio, dtype)

        self.reference["size"] = size
        self.reference["padding"] = self._padding_from_coverage(size, coverage_ratio)
        self.reference["matrix"] = get_align_mat(self)

        face = AlignerExtract().transform(image,
                                          self.reference["matrix"],
                                          size,
                                          self.reference["padding"])
        self.reference["face"] = face if dtype is None else face.astype(dtype)

        logger.trace("Loaded reference face. (face_shape: %s, matrix: %s)",
                     self.reference_face.shape, self.reference_matrix) 
Example #17
Source File: faces_detect.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def add_mask(self, name, mask, affine_matrix, interpolator, storage_size=128):
        """ Add a :class:`Mask` to this detected face

        The mask should be the original output from  :mod:`plugins.extract.mask`
        If a mask with this name already exists it will be overwritten by the given
        mask.

        Parameters
        ----------
        name: str
            The name of the mask as defined by the :attr:`plugins.extract.mask._base.name`
            parameter.
        mask: numpy.ndarray
            The mask that is to be added as output from :mod:`plugins.extract.mask`
            It should be in the range 0.0 - 1.0 ideally with a ``dtype`` of ``float32``
        affine_matrix: numpy.ndarray
            The transformation matrix required to transform the mask to the original frame.
        interpolator, int:
            The CV2 interpolator required to transform this mask to it's original frame.
        storage_size, int (optional):
            The size the mask is to be stored at. Default: 128
        """
        logger.trace("name: '%s', mask shape: %s, affine_matrix: %s, interpolator: %s)",
                     name, mask.shape, affine_matrix, interpolator)
        fsmask = Mask(storage_size=storage_size)
        fsmask.add(mask, affine_matrix, interpolator)
        self.mask[name] = fsmask 
Example #18
Source File: AverageFace.py    From Machine-Learning-Study-Notes with Apache License 2.0 5 votes vote down vote up
def generateImage(self):
        width = self._shape[0]
        height = self._shape[1]
        eyePoint = [(0.34 * width, height / 2.2), (0.66 * width, height / 2.2)]
        boundPoint = [(0, 0), (width / 2.0, 0), (width - 1, 0), (width - 1, height / 2.0), (width - 1, height - 1),
                      (width / 2.0, height - 1), (0, height - 1), (0, height / 2.0)]
        pointsAvg = np.array([(0, 0)] * (len(self._pointsArray[0]) + len(boundPoint)), np.float32)
        numImages = len(self._imageList)
        pointsNorm = []
        imagesNorm = []
        for point, image in zip(self._pointsArray, self._imageList):
            eyePointSrc = [point[self._keyPoint[0]], point[self._keyPoint[1]]]
            transform = AverageFace.similarityTransform(eyePointSrc, eyePoint)
            img = cv2.warpAffine(image, transform, (width, height))
            points = np.reshape(point, [len(self._pointsArray[0]), 1, 2])
            points = np.reshape(cv2.transform(points, transform), [len(self._pointsArray[0]), 2])
            points = np.append(points, boundPoint, 0)
            pointsAvg = pointsAvg + points / numImages
            pointsNorm.append(points)
            imagesNorm.append(img)
        rect = (0, 0, width, height)
        triangleList = AverageFace.calculateDelaunayTriangles(rect, pointsAvg)
        output = np.zeros((width, height, 3), dtype=np.float32)
        for i in range(len(imagesNorm)):
            img = np.zeros([width, height, 3], dtype=np.float32)
            for j in range(len(triangleList)):
                tin = []
                tout = []
                for k in range(3):
                    pIn = pointsNorm[i][triangleList[j][k]]
                    pIn = AverageFace.constrainPoint(pIn, width, height)
                    pOut = pointsAvg[triangleList[j][k]]
                    pOut = AverageFace.constrainPoint(pOut, width, height)
                    tin.append(pIn)
                    tout.append(pOut)
                AverageFace.warpTriangle(imagesNorm[i], img, tin, tout)
            output = output + img
        self._output = output / len(imagesNorm) 
Example #19
Source File: utils.py    From df with Mozilla Public License 2.0 4 votes vote down vote up
def load_images_masked(image_paths, convert=None,blurSize=35):
  basePath = os.path.split(image_paths[0])[0]
  alignments = os.path.join(basePath,'alignments.json')
  alignments = json.loads( open(alignments).read() )

  all_images    = []
  landmarks     = []


  pbar = tqdm(alignments)
  for original,cropped,mat,points in pbar:
    pbar.set_description('loading '+basePath)
    cropped = os.path.split(cropped)[1]
    cropped = os.path.join(basePath,cropped)
    if cropped in image_paths and os.path.exists(cropped):
      cropped = cv2.imread(cropped).astype(float)

      mat = numpy.array(mat).reshape(2,3)
      points = numpy.array(points).reshape((-1,2))

      mat = mat*160
      mat[:,2] += 42

      facepoints = numpy.array( points ).reshape((-1,2))

      mask = numpy.zeros_like(cropped,dtype=numpy.uint8)

      hull = cv2.convexHull( facepoints.astype(int) )
      hull = cv2.transform( hull.reshape(1,-1,2) , mat).reshape(-1,2).astype(int)

      cv2.fillConvexPoly( mask,hull,(255,255,255) )

      kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(15,15))

      mask = cv2.dilate(mask,kernel,iterations = 1,borderType=cv2.BORDER_REFLECT )

      facepoints = cv2.transform( numpy.array( points ).reshape((1,-1,2)) , mat).reshape(-1,2).astype(int)

      mask = mask[:,:,0]

      all_images.append(  numpy.dstack([cropped,mask]).astype(numpy.uint8) )
      landmarks.append( facepoints )

  return numpy.array(all_images),numpy.array(landmarks) 
Example #20
Source File: faces_detect.py    From faceswap with GNU General Public License v3.0 4 votes vote down vote up
def load_feed_face(self, image, size=64, coverage_ratio=0.625, dtype=None,
                       is_aligned_face=False):
        """ Align a face in the correct dimensions for feeding into a model.

        Parameters
        ----------
        image: numpy.ndarray
            The image that contains the face to be aligned
        size: int
            The size of the face in pixels to be fed into the model
        coverage_ratio: float, optional
            the ratio of the extracted image that was used for training. Default: `0.625`
        dtype: str, optional
            Optionally set a ``dtype`` for the final face to be formatted in. Default: ``None``
        is_aligned_face: bool, optional
            Indicates that the :attr:`image` is an aligned face rather than a frame.
            Default: ``False``

        Notes
        -----
        This method must be executed to get access to the following `properties`:
            - :func:`feed_face`
            - :func:`feed_interpolators`
        """
        logger.trace("Loading feed face: (size: %s, coverage_ratio: %s, dtype: %s, "
                     "is_aligned_face: %s)", size, coverage_ratio, dtype, is_aligned_face)

        self.feed["size"] = size
        self.feed["padding"] = self._padding_from_coverage(size, coverage_ratio)
        self.feed["matrix"] = get_align_mat(self)
        if is_aligned_face:
            original_size = image.shape[0]
            interp = cv2.INTER_CUBIC if original_size < size else cv2.INTER_AREA
            face = cv2.resize(image, (size, size), interpolation=interp)
        else:
            face = AlignerExtract().transform(image,
                                              self.feed["matrix"],
                                              size,
                                              self.feed["padding"])
        self.feed["face"] = face if dtype is None else face.astype(dtype)

        logger.trace("Loaded feed face. (face_shape: %s, matrix: %s)",
                     self.feed_face.shape, self.feed_matrix) 
Example #21
Source File: faces_detect.py    From faceswap with GNU General Public License v3.0 4 votes vote down vote up
def load_aligned(self, image, size=256, dtype=None, force=False):
        """ Align a face from a given image.

        Aligning a face is a relatively expensive task and is not required for all uses of
        the :class:`~lib.faces_detect.DetectedFace` object, so call this function explicitly to
        load an aligned face.

        This method plugs into :mod:`lib.aligner` to perform face alignment based on this face's
        ``landmarks_xy``. If the face has already been aligned, then this function will return
        having performed no action.

        Parameters
        ----------
        image: numpy.ndarray
            The image that contains the face to be aligned
        size: int
            The size of the output face in pixels
        dtype: str, optional
            Optionally set a ``dtype`` for the final face to be formatted in. Default: ``None``
        force: bool, optional
            Force an update of the aligned face, even if it is already loaded. Default: ``False``

        Notes
        -----
        This method must be executed to get access to the following `properties`:
            - :func:`original_roi`
            - :func:`aligned_landmarks`
            - :func:`aligned_face`
            - :func:`adjusted_interpolators`
        """
        if self.aligned and not force:
            # Don't reload an already aligned face
            logger.trace("Skipping alignment calculation for already aligned face")
        else:
            logger.trace("Loading aligned face: (size: %s, dtype: %s)", size, dtype)
            padding = int(size * self._extract_ratio) // 2
            self.aligned["size"] = size
            self.aligned["padding"] = padding
            self.aligned["matrix"] = get_align_mat(self)
            self.aligned["face"] = None
        if image is not None and (self.aligned["face"] is None or force):
            logger.trace("Getting aligned face")
            face = AlignerExtract().transform(image, self.aligned["matrix"], size, padding)
            self.aligned["face"] = face if dtype is None else face.astype(dtype)

        logger.trace("Loaded aligned face: %s", {k: str(v) if isinstance(v, np.ndarray) else v
                                                 for k, v in self.aligned.items()
                                                 if k != "face"}) 
Example #22
Source File: data_augment.py    From keras-frcnn with Apache License 2.0 4 votes vote down vote up
def augment(img_data, config, augment=True):
	assert 'filepath' in img_data
	assert 'bboxes' in img_data
	assert 'width' in img_data
	assert 'height' in img_data

	img_data_aug = copy.deepcopy(img_data)

	img = cv2.imread(img_data_aug['filepath'])

	if augment:
		rows, cols = img.shape[:2]

		if config.use_horizontal_flips and np.random.randint(0, 2) == 0:
			img = cv2.flip(img, 1)
			for bbox in img_data_aug['bboxes']:
				x1 = bbox['x1']
				x2 = bbox['x2']
				bbox['x2'] = cols - x1
				bbox['x1'] = cols - x2

		if config.use_vertical_flips and np.random.randint(0, 2) == 0:
			img = cv2.flip(img, 0)
			for bbox in img_data_aug['bboxes']:
				y1 = bbox['y1']
				y2 = bbox['y2']
				bbox['y2'] = rows - y1
				bbox['y1'] = rows - y2


		if config.random_rotate:
			M = cv2.getRotationMatrix2D((cols/2, rows/2), np.random.randint(-config.random_rotate_scale, config.random_rotate_scale), 1)
			img = cv2.warpAffine(img, M, (cols, rows), flags=cv2.INTER_CUBIC, borderMode= cv2.BORDER_REPLICATE)
			for bbox in img_data_aug['bboxes']:
				K = np.array([[bbox['x1'],bbox['y1']],[bbox['x2'],bbox['y2']],[bbox['x1'],bbox['y2']],[bbox['x2'],bbox['y1']]])
				K = cv2.transform(K.reshape(4,1,2),M)[:,0,:]

				(x1, y1) = np.min(K, axis=0)
				(x2, y2) = np.max(K, axis=0)

				bbox['x1'] = x1
				bbox['x2'] = x2
				bbox['y1'] = y1
				bbox['y2'] = y2

	return img_data_aug, img 
Example #23
Source File: rectangle_tracker.py    From python-opencv-rectangle-tracker with Apache License 2.0 4 votes vote down vote up
def persTransform(pts, H):
    """Transforms a list of points, `pts`,
    using the perspective transform `H`."""
    src = np.zeros((len(pts), 1, 2))
    src[:, 0] = pts
    dst = cv2.perspectiveTransform(src, H)
    return np.array(dst[:, 0, :], dtype='float32')