Sunday, December 15, 2024

Have you considered image augmentation in image data generation - Part 1?

 Python has emerged as a programming language of choice when it comes to generating images through image augmentation techniques. Image augmentation techniques can provide a rich diversity of the training dataset and turbocharge the image recognition models. 

This post explores one of the fundamental image data augmentation techniques, random cropping. A future post discusses the random clipping.

Cropping and flipping are common image manipulation techniques that don't require explicit redefinition. They are often implemented using built-in functions or libraries like OpenCV and PIL.

Details of random cropping:

Here is the python code to randomly crop an image.

=========

def random_crop(img, crop_size):


    h, w = img.shape[:2]

    x = np.random.randint(0, w - crop_size[1])

    y = np.random.randint(0, h - crop_size[0])

    crop_img = img[y:y+crop_size[0], x:x+crop_size[1]]

    return crop_img

============================

The random cropping shown above does not directly use the OpenCV, it uses the functionality of NumPy arrays. 

Note:  Defining h, w =img.shape[:2] correctly picks the images height and width appropriate for NumPy array. The common channels of NumPy are [height, width, and  color-channel] in that order.

Also note:

NumPy arrays use zero-based indexing. This means the first element in an array has an index of 0, the second has an index of 1, and so on.

In the random_crop function:

x = np.random.randint(0, w - crop_size[1])

This line generates a random starting x-coordinate for the crop. w is the width of the image.

crop_size[1] is the width of the cropped region.

By subtracting crop_size[1] from w, we ensure that the starting x-coordinate plus the crop width doesn't exceed the image's width.

y = np.random.randint(0, h - crop_size[0])

This line generates a random starting y-coordinate for the crop.

h is the height of the image. crop_size[0] is the height of the cropped region.

Similarly, this ensures the starting y-coordinate plus the crop height doesn't exceed the image's height.

Random cropping is the same as cropping with randomness introduced to the randomness in choosing the cropping coordinates.

The following code is just regular cropping of an image:

====================

from PIL import Image

def crop_image(image_path, left, top, right, bottom):
"""
Crops an image using PIL.

Args:
image_path: Path to the image file.
left: X-coordinate of the top-left corner of the cropping region.
top: Y-coordinate of the top-left corner of the cropping region.
right: X-coordinate of the bottom-right corner of the cropping region.
bottom: Y-coordinate of the bottom-right corner of the cropping region.

Returns:
The cropped image as a PIL Image object.
"""
try:
img = Image.open(image_path)
cropped_img = img.crop((left, top, right, bottom))
return cropped_img
except Exception as e:
print(f"Error cropping image: {e}")
return None


# Example usage:
image_path = r"C:\Users\hoden\PycharmProjects\exploreImage\Images\TheKiss.jpg"
left = 100
top = 50
right = 300
bottom = 200

cropped_image = crop_image(image_path, left, top, right, bottom)

if cropped_image:
cropped_image.show()
cropped_image.save("cropped_image.jpg")

Now, Let us consider random cropping:

TBC--> TO BE CONTIUED

No comments: