LINEAR CONTRAST STRETCHING
# Example Python Program for contrast stretching
from PIL import Image
import cv2
# Method to process the red band of the image
def normalizeRed(intensity):
iI = intensity
minI = 86
maxI = 230
minO =0
maxO = 255
iO = (iI-minI)*(((maxO-minO)/(maxI-minI))+minO)
return iO
# Method to process the green band of the image
def normalizeGreen(intensity):
iI = intensity
minI = 90
maxI = 225
minO =0
maxO = 255
iO = (iI-minI)*(((maxO-minO)/(maxI-minI))+minO)
return iO
# Method to process the blue band of the image
def normalizeBlue(intensity):
iI = intensity
minI = 100
maxI = 210
minO =0
maxO = 255
iO = (iI-minI)*(((maxO-minO)/(maxI-minI))+minO)
return iO
# Create an image object
imageObject = [Link]("[Link]")
# Split the red, green and blue bands from the Image
multiBands = [Link]()
# Apply point operations that does contrast stretching on each color band
normalizedRedBand = multiBands[0].point(normalizeRed)
normalizedGreenBand = multiBands[1].point(normalizeGreen)
normalizedBlueBand = multiBands[2].point(normalizeBlue)
# Create a new image from the contrast stretched red, green and blue brands
normalizedImage = [Link]("RGB", (normalizedRedBand, normalizedGreenBand,
normalizedBlueBand))
# Display the image before contrast stretching
[Link]()
# Display the image after contrast stretching
[Link]()
normalizedImage = [Link]('[Link]')
INPUT
OUTPUT
Power-Law (Gamma) Transformation
import cv2
import numpy as np
# Open the image.
img = [Link]('[Link]')
# Trying 4 gamma values.
for gamma in [0.1, 0.5, 1.2, 2.2]:
# Apply gamma correction.
gamma_corrected = [Link](255*(img / 255) ** gamma, dtype = 'uint8')
# Save edited images.
[Link]('gamma_transformed'+str(gamma)+'.jpg', gamma_corrected)
[Link](0)
[Link]()
INPUT
OUTPUT
RGB TO HSI CONVERSION AFTER LINEAR CONTRAST STRECHING
import cv2
import numpy as np
import math
img1 = [Link]('[Link]')
def RGB_TO_HSI(img):
with [Link](divide='ignore', invalid='ignore'):
#Load image with 32 bit floats as variable type
bgr = np.float32(img)/255
#Separate color channels
blue = bgr[:,:,0]
green = bgr[:,:,1]
red = bgr[:,:,2]
#Calculate Intensity
def calc_intensity(red, blue, green):
return [Link](green + red, 3)
#Calculate Saturation
def calc_saturation(red, blue, green):
minimum = [Link]([Link](red, green), blue)
saturation = 1 - (3 / (red + green + blue + 0.1) * minimum)
return saturation
#Calculate Hue
def calc_hue(red, blue, green):
hue = [Link](red)
for i in range(0, [Link][0]):
for j in range(0, [Link][1]):
hue[i][j] = 0.5 * ((red[i][j] - green[i][j]) + (red[i][j] - blue[i][j])) / \
[Link]((red[i][j] - green[i][j])**2 +
((red[i][j] - blue[i][j]) * (green[i][j] - blue[i][j])))
hue[i][j] = [Link](hue[i][j])
if blue[i][j] <= green[i][j]:
hue[i][j] = hue[i][j]
else:
hue[i][j] = ((360 * [Link]) / 180.0) - hue[i][j]
return hue
#Merge channels into picture and return image
hsi = [Link]((calc_hue(red, blue, green), calc_saturation(red, blue, green),
calc_intensity(red, blue, green)))
return hsi
[Link]('image',RGB_TO_HSI(img1))
[Link](0)
[Link]()
Image without linear contrast stretching after hsi conversion
Image with linear contrast stretching after hsi conversion