in

# Ahead and Backward Mapping for Pc Imaginative and prescient | by Javier Martínez Ojeda | Could, 2023

The ahead mapping course of consists of the straightforward picture transformation course of that has been mentioned within the introduction and within the earlier article: it iterates over all of the pixels of the picture, and the corresponding transformation is utilized to every pixel individually. Nevertheless, these instances wherein the brand new place of the reworked pixel falls exterior the picture area, an instance of which is proven beneath, have to be taken under consideration.

To hold out the ahead mapping course of, first outline a operate that receives as parameters the unique coordinates of a pixel. This operate will apply a metamorphosis to the unique pixel coordinates, and return the brand new coordinates of the pixel after the transformation. The next code instance exhibits the operate for the rotation transformation.

`def apply_transformation(original_x: int, original_y: int) -> Tuple[int, int]:# Outline the rotation matrix   rotate_transformation = np.array([[np.cos(np.pi/4), -np.sin(np.pi/4), 0],[np.sin(np.pi/4),  np.cos(np.pi/4), 0],[0, 0, 1]])# Apply transformation after setting homogenous coordinate to 1 for the unique vector.new_coordinates = rotate_transformation @ np.array([original_x, original_y, 1]).T# Spherical the brand new coordinates to the closest pixel   return int(np.rint(new_coordinates[0])), int(np.rint(new_coordinates[1]))`

After you have this operate, you solely must iterate over every pixel of the picture, apply the transformation and test if the brand new pixel coordinates are throughout the area of the unique picture. If the brand new coordinates are throughout the area, the pixel on the brand new coordinates of the brand new picture will take the worth that the unique pixel had within the unique picture. If it falls exterior the picture, the pixel is omitted.

`def forward_mapping(original_image: np.ndarray) -> np.ndarray:# Create the brand new picture with identical form as the unique onenew_image = np.zeros_like(original_image)for original_y in vary(original_image.form[1]):for original_x in vary(original_image.form[0]):# Apply rotation on the unique pixel's coordinatesnew_x, new_y = apply_transformation(original_x, original_y)# Verify if new coordinates fall contained in the picture's areaif 0 <= new_y < new_image.form[1] and 0 <= new_x < new_image.form[0]:new_image[new_x, new_y, :] = original_image[original_x, original_y, :]return new_image`

The results of making use of a rotation transformation with foward mapping could be seen within the picture beneath, the place on the left is the unique picture, and on the best the reworked picture. It is very important word that for this picture the origin of coordinates is within the higher left nook, so the picture rotates round that time anti-clockwise.

Concerning the results of the transformation, it may be seen how the reworked picture doesn’t have the full-black background that the unique one has, however as a substitute has many white stripes. This occurs, as talked about within the introduction, as a result of the pixels of the unique picture don’t at all times map to all of the pixels of the brand new picture. For the reason that new coordinates are calculated by rounding to the closest pixel, this leads to many intermediate pixels by no means receiving a price. On this case, as the brand new picture is initialized with all pixels clean, the pixels that haven’t been given a price through the transformation will stay clean, producing these white stripes within the reworked picture.

As well as, it ought to be famous that there’s one other notable drawback: overlaps. This drawback happens when two pixels of the unique picture are reworked to the identical pixel of the brand new picture. For the code used on this article, if there are two pixels of the unique picture that map to the identical pixel of the brand new picture, the brand new pixel will take the worth of the final unique pixel that has been reworked, overwriting the worth of the primary one which was already set.