Image cropping involves extracting a portion of an image by specifying a crop region. In scikit-image, you can use slicing and indexing to crop an image. Here’s an example in Python:
In this example, the input image is first loaded using the imread
function. The crop region is specified by slicing the image along both dimensions, such that the first and last quarter of the rows and columns are removed.
Image flipping in Python can be performed using the “cv2.flip” function from the OpenCV library. The “cv2.flip” function takes two arguments: the input image and a flip code. The flip code specifies the flipping to be performed and can be one of the following values:
- cv2.FLIP_HORIZONTAL: Flip the image horizontally
cv2.FLIP_VERTICAL: Flip the image vertically
cv2.FLIP_BOTH: Flip the image both horizontally and vertically
Flipping can be considered an extension of rotation, allowing left-right and up-down image flipping.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# reading the image
image = imread('index.png')
image = np.array(image)
imshow(image)
plt.title('Original Image')
Now, what do you do if you have to flip the images, again read the image and now in order to flip it? let’s say we are doing left to right flip. I can easily do that using the “fliplr()” function.
# flip image left-to-right
flipLR = np.fliplr(image)
plt.imshow(flipLR)
plt.title('Left to Right Flipped')
And these are the ways in which you can flip the images.
Image Preprocessing – Brightness Manipulation
Brightness manipulation in Python can be performed using the image library. The image library provides the exposure module, which includes the function of adjusting gamma that can be used to change the brightness of an image.
Images with different brightness can be used to make the model robust to changes in lighting conditions; this is important for systems that work in outdoor lightings, like CCTV cameras on traffic signals.
from skimage.exposure import adjust_gamma
# read the image
image = imread('index.png')
plt.title('Original Image')
imshow(image)
I am going to change the gamma value, and that changes the strength of the image. So, this is my bright image.
# brighten the image
bright = adjust_gamma(image,gamma=0.5,gain=1)
imshow(bright)
plt.title('Brightened IMage')
So, approximately I can make all of these changes to the image.
Conclusion
Scikit-image is a popular Python library for image processing that provides tools and functions for working with images. Here’s a summary of some of the critical features of image for image processing:
- Image I/O: image provides functions for reading and writing images to disk, including
imread
for reading an image andimsave
for saving an image. - Image restoration: image provides algorithms for restoring degraded images, including functions for removing noise and correcting for blurring or distortion.
- Image analysis: image provides functions for analyzing image properties, including histograms, gradient magnitude, and texture analysis.
- Image visualization: image provides functions for visualizing images and their properties, including plotting and displaying images, histograms, and other visual representations of image data.
Overall, image is a comprehensive and well-documented library for image processing that is widely used in scientific, medical, and industrial applications.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
Read the full article here