Converting Image Format
RGB to Grayscale
rgb2gray module of skimage package is used to convert a 3-channel RGB Image to one channel monochrome image. In order to apply filters and other processing techniques, the expected input is a two-dimensional vector i.e. a monochrome image.
skimage.color.rgb2gray() function is used to convert an RGB image to Grayscale format
Syntax : skimage.color.rgb2gray(image)
Parameters : image : An image – RGB format
Return : The image – Grayscale format
Code:
Python3
# Importing Necessary Libraries from skimage import data from skimage.color import rgb2gray import matplotlib.pyplot as plt # Setting the plot size to 15,15 plt.figure(figsize = ( 15 , 15 )) # Sample Image of scikit-image package coffee = data.coffee() plt.subplot( 1 , 2 , 1 ) # Displaying the sample image plt.imshow(coffee) # Converting RGB image to Monochrome gray_coffee = rgb2gray(coffee) plt.subplot( 1 , 2 , 2 ) # Displaying the sample image - Monochrome # Format plt.imshow(gray_coffee, cmap = "gray" ) |
Output:
Explanation: By using rgb2gray() function, the 3-channel RGB image of shape (400, 600, 3) is converted to a single-channel monochromatic image of shape (400, 300). We will be using grayscale images for the proper implementation of thresholding functions. The average of the red, green, and blue pixel values for each pixel to get the grayscale value is a simple approach to convert a color picture 3D array to a grayscale 2D array. This creates an acceptable gray approximation by combining the lightness or brightness contributions of each color band.
The HSV (Hue, Saturation, Value) color model remaps the RGB basic colors into dimensions that are simpler to comprehend for humans. The RGB color space describes the proportions of red, green, and blue in a colour. In the HSV color system, colors are defined in terms of Hue, Saturation, and Value.
skimage.color.rgb2hsv() function is used to convert an RGB image to HSV format
Syntax : skimage.color.rgb2hsv(image)
Parameters : image : An image – RGB format
Return : The image – HSV format
Code:
Python3
# Importing Necessary Libraries from skimage import data from skimage.color import rgb2hsv import matplotlib.pyplot as plt # Setting the plot size to 15,15 plt.figure(figsize = ( 15 , 15 )) # Sample Image of scikit-image package coffee = data.coffee() plt.subplot( 1 , 2 , 1 ) # Displaying the sample image plt.imshow(coffee) # Converting RGB Image to HSV Image hsv_coffee = rgb2hsv(coffee) plt.subplot( 1 , 2 , 2 ) # Displaying the sample image - HSV Format hsv_coffee_colorbar = plt.imshow(hsv_coffee) # Adjusting colorbar to fit the size of the image plt.colorbar(hsv_coffee_colorbar, fraction = 0.046 , pad = 0.04 ) |
Output:
Image Segmentation using Python’s scikit-image module
The process of splitting images into multiple layers, represented by a smart, pixel-wise mask is known as Image Segmentation. It involves merging, blocking, and separating an image from its integration level. Splitting a picture into a collection of Image Objects with comparable properties is the first stage in image processing. Scikit-Image is the most popular tool/module for image processing in Python.