Breaking

Be Strong Let's Try!

Monday, 30 August 2021

How To Generate Color Image Processing Using Python Coding | Digital Image Processing

Objective:

Objective of this topic is to familiarize with different color space eg: RGB and HSI 

Description: 

Color image processing: The use of color image processing is motivated by two principle factors. 

i. Color is a powerful descriptor 

ii. Humans can distinguish between thousands of colors shades or intensities compared to about only two dozen shades of gray We have two types of color image processing Pseudo color processing: Pseudo-color processing:

Pseudo-color processing is a technique that maps each of the grey levels of a black and white image into an assigned color. This colored image, when displayed, can make the identification of certain features easier for the observer. The mappings are computationally simple and fast. This makes pseudo-color an attractive technique for use on digital image processing systems that are designed to be used in the interactive mode. 

Full color image processing:

Full color image processing fall into two major categories, in the first category we process each component image individually and then form a composite processed color image from individually process components while in second category we work with color pixels directly. 

Color model: 

The color model aims to facilitate the specifications of colors in some standard way. Different types of color models are used in multiple fields like in hardware, in multiple applications of creating animation, etc. 

RGB Model:

 In the RGB model, each color appears in its primary components of red, green and blue. This model is based on a Cartesian coordinate system.

How To Generate Color Image Processing Using Python Coding | Digital Image Processing
HSI Model (Hue, Saturation, Intensity):
 It is a very important and attractive color model because it represents the colors the same way as the human eye senses colors Hue is a color component that describes a pure color (pure yellow, orange or red) Saturation component represents the measure of the degree to which color is mixed with white color. 
0 degree – Red 
120 degree – Green 
240 degree – Blue 
60 degree – Yellow
 300 degree – Magenta 
Intensity Range is [0, 1] 0 means white 1 means black RGB to HSV conversion formula: The R,G, B values are divided by 255 to change the range from 
0..255 to 0..1: 
R' = R/255
G' = G/255
B' = B/255 
Cmax = max(R', G', B') 
Cmin = min(R', G', B') 
Δ = Cmax - Cmin 
Hue calculation: 



Saturation calculation:


Value calculation: 
V = Cmax
Some of the practical example to understand this topic:
1: Load these RGB images
How To Generate Color Image Processing Using Python Coding | Digital Image Processing

Convert it to HSV color space using above mentioned equations. Display each channel (Hue, saturation and intensity/value) separately.

Code

import cv2 as cv
import numpy as np
img = cv.imread('
freestudystrom.tif') # read input image
dims = np.shape(img)
rows = dims[
0]
cols = dims[
1]
# Function to Convert RGB image to HSV image
def HSVimage(img):
    Hue = np.zeros((rows, cols),
dtype=np.uint32)
    Saturation = np.zeros((rows, cols),
dtype=np.float)
    Intensity = np.zeros((rows, cols),
dtype=np.float)
   
for i in range(rows):
       
for j in range(cols):           
            value = img[i, j]
           
#normalize
           
B = value[0] / 255
           
G = value[1] / 255
           
R = value[2] / 255
           
array = [B, G, R]
            Cmax = np.max(array)
            Cmin = np.min(array)
            Delta = Cmax - Cmin
           
# Hue Calculation
            
if Delta == 0:
                Hue[i, j] =
0
           
elif Cmax == R:
                Hue[i, j] =
60 * np.mod((G - B) / Delta, 6)
           
elif Cmax == G:
                Hue[i, j] =
60 * (((B - R) / Delta) + 2)
           
elif Cmax == B:
                Hue[i, j] =
60 * (((R - G) / Delta) + 4)
           
# Saturation Calculation
           
if Cmax == 0:
                Saturation[i, j] =
0
           
else:
                Saturation[i, j] = (Delta / Cmax)*
255
           
# Intensity Value
           
Intensity[i, j] = Cmax*255
   
return Hue,Saturation,Intensity
# Normalization
def normalization(out):
    minimum = np.min(out)
    maximum = np.max(out)
    out = np.zeros((rows, cols),
dtype=np.uint8)
   
for i in range(0, rows):
       
for j in range(0, cols):           
            out[i][j] = (
255*(out[i][j]-minimum))//(maximum-minimum)
   
return out
Hue, Saturation, Intensity = HSVimage(img)
# Applying Normalization
Hue = normalization(Hue)
Saturation = normalization(Saturation)
Intensity = normalization(Intensity)
# display all channels and overall result
cv.imshow('Hue', Hue)
cv.imshow(
'Saturation', Saturation)
cv.imshow(
'Intensity ', Intensity)
Output = cv.merge([Hue,Saturation,Intensity])             
#merge all channels to get final image
cv.imshow('HSV Image ', Output)
cv.waitKey(
0)

Output:

How To Generate Color Image Processing Using Python Coding | Digital Image Processing


2: Study “rgb2hsv” command to convert the image in (RGB-full-color-cube) to HSV color space. And compare the results with the previously calculated HSV image.

Code

import cv2 as cv
import numpy as np
img = cv.imread('
freestudystrom.tif') # read input image
dims = np.shape(img)
rows = dims[
0]
cols = dims[
1]
# Function to Convert RGB image to HSV image
def HSVimage(img):
    Hue = np.zeros((rows, cols),
dtype=np.uint32)
    Saturation = np.zeros((rows, cols),
dtype=np.float)
    Intensity = np.zeros((rows, cols),
dtype=np.float)
   
for i in range(rows):
       
for j in range(cols):           
            value = img[i, j]
           
#normalize
           
B = value[0] / 255
           
G = value[1] / 255
           
R = value[2] / 255
           
array = [B, G, R]
            Cmax = np.max(array)
            Cmin = np.min(array)
            Delta = Cmax - Cmin
           
# Hue Calculation
           
if Delta == 0:
                Hue[i, j] =
0
           
elif Cmax == R:
                Hue[i, j] =
60 * np.mod((G - B) / Delta, 6)
           
elif Cmax == G:
                Hue[i, j] =
60 * (((B - R) / Delta) + 2)
           
elif Cmax == B:
                Hue[i, j] =
60 * (((R - G) / Delta) + 4)
           
# Saturation Calculation
           
if Cmax == 0:
                Saturation[i, j] =
0
           
else:
                Saturation[i, j] = (Delta / Cmax)*
255
           
# Intensity Value
           
Intensity[i, j] = Cmax*255
   
return Hue,Saturation,Intensity
# Normalization
def normalization(out):
    minimum = np.min(out)
    maximum = np.max(out)
    out = np.zeros((rows, cols),
dtype=np.uint8)
   
for i in range(0, rows):
       
for j in range(0, cols):           
            out[i][j] = (
255*(out[i][j]-minimum))//(maximum-minimum)
   
return out
Hue, Saturation, Intensity = HSVimage(img)
# Applying Normalization
Hue = normalization(Hue)
Saturation = normalization(Saturation)
Intensity = normalization(Intensity)
# display all channels and overall result
cv.imshow('Hue', Hue)
cv.imshow(
'Saturation', Saturation)
cv.imshow(
'Intensity ', Intensity)
Output = cv.merge([Hue,Saturation,Intensity])             
#merge all channels to get final image
cv.imshow('HSV Image ', Output)
cv.waitKey(
0)

 

Output:

How To Generate Color Image Processing Using Python Coding | Digital Image Processing


3: Read an RGB image and apply Gaussian filter to add smoothing effect on it, and sobel filter to see gradients. display the results.

Code

import numpy as np
import cv2 as cv
import math as m
img = cv.imread(
'freestudystrom.tif',cv.IMREAD_COLOR) # Read input image
b,g,r = cv.split(img)   #split the channels
dims = np.shape(img)    #dimensions of input image
rows = dims[0]
cols = dims[
1]
B_img = cv.GaussianBlur(b,(
3,3),cv.BORDER_DEFAULT)  #smoothen blue channel
G_img = cv.GaussianBlur(g,(3,3),cv.BORDER_DEFAULT)  #smoothen green channel
R_img = cv.GaussianBlur(r,(3,3),cv.BORDER_DEFAULT)  #smoothen red channel
Output = cv.merge([B_img,G_img,R_img])              #merge all channels to get final image
#show all channels blurred and overall blurred image
cv.imshow('Blue Channel', B_img)
cv.imshow(
'Green Channel  ', G_img)
cv.imshow(
' Red Channel', R_img)
cv.imshow(
'Blurred Image', Output)
cv.waitKey(
0)

Output:

How To Generate Color Image Processing Using Python Coding | Digital Image Processing



       



No comments:

Post a Comment

Pages