Breaking

Be Strong Let's Try!

Sunday, 15 August 2021

What Is Transformation Operations And Its Application In Python Coding

 

Objective And Description:

The objective of this  is to implement thresholding on images to convert them to binary, perform different transformation operations on images.

Transformation operations help enhance the quality of the image by applying operations like log, inverse log and power on the entire image. Different type of transformation yields different results.

 Gray level transformation:

 There are three basic gray level transformation.

I.                     Linear

II.                   Logarithmic

III.                 Power – law the overall graph of these transitions has been shown below.




Power law transformations:

It includes nth power and nth root transformation. These transformations can be given by the expression: Digital Image Processing Lab Manual 1 s=cr^γ .This symbol γ is called gamma, due to which this transformation is also known as gamma transformation. Variation in the value of γ varies the enhancement of the images. Different display devices / monitors have their own gamma correction, that’s why they display their image at different intensity. This type of transformation is used for enhancing images for different type of display devices. The gamma of different display devices is different. For example Gamma of CRT lies in between of 1.8 to 2.5, that means the image displayed on CRT is dark.

 Correcting gamma:

 s=cr^γ s=cr^(1/2.5) The same image but with different gamma values has been shown here.

Log transformations:

The log transformations can be defined by this formula s = c log(r + 1). Where s and r are the pixel values of the output and the input image and c is a constant. The value 1 is added to each of the pixel value of the input image because if there is a pixel intensity of 0 in the image, then log (0) is equal to infinity. So 1 is added, to make the minimum value at least 1. During log transformation, the dark pixels in an image are expanded as compare to the higher pixel values. The higher pixel values are kind of compressed in log transformation.

Negative Transformation:

The second linear transformation is negative transformation, which is invert of identity transformation. In negative transformation, each value of the input image is subtracted from the L-1 and mapped onto the output image. This transformation is done by this formula s = (L – 1) – r So each value is subtracted by 255,so the lighter pixels become dark and the darker picture becomes light. And it results in image negative. \ Negative transformation Thresholding is the operation through which an image can be converted into a binary image/black & white i.e. having only two distinct levels. Threshold value can be the mean or median etc. of the image. Gray level Slicing is used to highlight a specific range of gray levels in an image Some Useful Commands:

1. To calculate the mean of 2D array using NumPy: my_mean = numpy.mean (my_array)

2. To calculate min (or max) of an array: my_min = numpy.amin(my_array)

3. To calculate the power of an array using NumPy: array_power = numpy.power (my_array, power)

4. To obtain percentile value. percentile_array = numpy.percentile(my_array, percentile)

5. To change data type of array. my_array = my_array.astype(numpy.uint16

There Are Some Practice Task For Implementing These Topics So let’s start:

 

 

1: Apply following transformation techniques on image provided and observe the output image of each transformation. I. Negative transformation II. Log transformation

 

(a)

Code:

import cv2 as cv
import numpy as np
img = cv.imread(
'einstein.tif',0)
rows= img.shape[
0]
colums = img.shape[
1]
print(rows,colums)

negative_image =
255 - img
cv.imshow(
'My Negative Image' , negative_image)
cv.waitKey(
0)

 

 

Output:

 




(b)

Code:

import cv2 as cv
import numpy as np
image = cv.imread(
'einstein.tif',0)
rows= image.shape[
0]
colums = image.shape[
1]       
print(rows,colums)           
m=np.max(image)
print(m)
x =
255/(np.log(1+m))
print(x)
Log_Transformation = x*(np.log(image+
1))          
Log_Transformation1 = np.array(Log_Transformation
,np.uint8)
cv.imshow(
'Orignal Image ',image)
cv.imshow(
'MY Log Transformed Image', Log_Transformation1) # show log transformed image
cv.waitKey(6000)

Output:



2: Apply the following transformation on an image.



 

(a)

Code:

import cv2 as cv
import numpy as np
image = cv.imread(
'einstein.tif',0)
dim = image.shape
rows= image.shape[
0]
colums = image.shape[
1]
print(rows,colums)
mi=np.min(image)
ma = np.max(image)
print('Min : ',min,'Max: ', max)
m = np.mean(image)
print('Mean : ',m)
image_1 = np.zeros(dim
,dtype=np.uint8)

for row in range(rows):
   
for col in range(colums):
       
if image[row][col] < m:
            image_1[row][col] =
255
       
else:
            image_1[row][col] =
0
cv.imshow('1ST CONDITION', image_1)
cv.waitKey(
0)

 

 

Output:



(b)

Code:

import cv2
import numpy
img = cv2.imread(
'einstein.tif',0)
shap = img.shape
r = shap[
0]
c = shap[
1]
meen = numpy.mean(img)
my_Image2 = numpy.zeros(shap
,dtype=numpy.uint8)
for rows in range(r):
   
for cols in range(c):
       
if img[rows][cols] < meen:
            my_Image2[rows][cols] =
255
       
else:
            my_Image2[rows][cols] =
0
cv2.imshow('My Second Condition', my_Image2)
cv2.waitKey(
0)

 

 

 

Output:



(c)

Code:

import cv2 as cv
import numpy as np
image = cv.imread(
'einstein.tif',0#read original image
dim = image.shape                             #image dimensions
rows= image.shape[0]                             #number of rows
colums = image.shape[1]                            #number of columnss
print(rows,colums)                                  #show rows and columns
mi=np.min(image)                              #minimum pixel
ma = np.max(image)                            #maximum pixel
print('Min : ',mi,'Max: ', ma)              #show min and max pixel
m = np.mean(image)                            #calculate mean
print('Mean : ',m)                          #print mean
Low_Limit =  m-20
High_Limit = m+20
print("Limits : ",Low_Limit," to ",High_Limit)  #show limits
image_1 = np.zeros(dim,dtype=np.uint8)        #new blank image

for row in range(rows):
   
for col in range(colums):
       
if image[row][col] <= High_Limit and image[row][col] >= Low_Limit:
            image_1[row][col] =
0
       
else:
            image_1[row][col] =
255
cv.imshow('My Third Condition', image_1)                #show  transformed image
cv.waitKey(0)

 

 

 

 

Output:



3: Apply Power Law transformation for the following values of γ (0.2, 0.5, 1.2 and 1.8) . Make sure to adjust data types accordingly.

 

Code:

import cv2 as cv
import numpy as np
image = cv.imread(
'einstein.tif',0)
dim = image.shape
rows= image.shape[
0]
colums = image.shape[
1]
print(rows,colums)

x = image/
255
y = np.power(x,0.2)                        #gamma = 0.2
z = 255*y
z1 = np.array(z
,dtype= np.uint8)
cv.imshow(
'The Value Of Gamma  = 0.2',z1)

x2 = np.power(x
,0.5)                        #gamma = 0.5
z2 = 255*x2
z2 = np.array(z2
,dtype= np.uint8)
cv.imshow(
'The Value Of Gamma  = 0.5',z2)
x3 = np.power(x
,1.2)                        #gamma = 1.2
z3 = 255*x3
z3 = np.array(z3
,dtype= np.uint8)
cv.imshow(
'The Value Of Gamma  = 1.2',z3)

x4 = np.power(x
,1.8)                        #gamma = 1.8
z4 = 255*x4
z4 = np.array(z4
,dtype= np.uint8)
cv.imshow(
'The Value Of Gamma = 1.8',z4)

cv.waitKey(
0)

 Output:

                                               

 



 



 



 



 

 

 

 

 

 

 

 

 

4:Apply Gray level slicing using lower limit 100 and upper limit 200. Set all these values to 210.

Code:

#Noor Khan Lab No 04
import cv2 as cv
import numpy as np
image = cv.imread(
'einstein.tif',0)
dim = image.shape
rows= image.shape[
0]
colums = image.shape[
1]
print(rows,colums)
image_1 = np.zeros(dim
,dtype=np.uint8)
#slicing
for row in range(rows):
   
for col in range(colums):
       
if image[row][col] <= 200 and image[row][col] >= 100:
            image_1[row][col] =
210
#show sliced image
cv.imshow('New Sliced Image', image_1)
cv.waitKey(
0)

 

Output:


What is Bit plane slicing:

 Bit plane slicing is a method of representing an image with one or more bits of the byte used for each pixel. One can use only MSB to represent the pixel, which reduces the original gray level to a binary image. Main objective of this technique is: To highlight the contribution made to the total image appearance by specific bits. a) Assuming that each pixel is represented by 8 bits, the image is composed of 8 1-bit planes. b) Plane 1 contains the least significant bit and plane 8 contains most significant c) Useful for analyzing the relative importance played by each bit of the image.

 


No comments:

Post a Comment

Pages