Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Image

Enhancement
Content
Ø Negative Transformation
Ø Log Transformation
Ø Power Law Transformation
Negative Transformation
• When an image is inverted, each of its pixel value ‘r’ is subtracted
from the maximum pixel value L-1 and the original pixel is replaced
with the result ‘s’.
• Image inversion or Image negation helps finding the details from the
darker regions of the image.
• The negative transformation has its applications in the areas
of Medical Imaging, Remote Sensing and others.
• s = 255 – r
Negative Transformation
from PIL import Image
import numpy as np
from pylab import *
img=Image.open('C:/Users/hp/Desktop/Images_ECE348/parrot.jpg')
figure(1)
img.show()
 
img1=img.convert('L')       # RGB to Gray-scale conversion
figure(2)
img1.show()
 
img_neg=img1.point(lambda x:255-x)    # Negative Transformation
img_neg.show()
Log Transformation
• The formula for Logarithmic transformation
s = c log(r + 1)  
• Here, r and s are the pixel values for input and 
output image. And c is constant. In the formula, 
we  can  see  that  1  is  added  to  each  pixel  value 
this  is  because  if  pixel  intensity  is  zero  in  the 
image  then  log(0)  is  infinity  so,  to  have 
minimum value one is added.
• When  log  transformation  is  done  dark  pixels 
are  expanded  as  compared  to  higher  pixel 
values.  In  log  transformation  higher  pixels  are 
compresses.
Log Transformation
from PIL import Image
import numpy as np
from pylab import *
img=Image.open('C:/Users/hp/Desktop/Images_ECE348/parrot.jpg')
figure(1)
img.show()
img1=img.convert('L')       
figure(2)
img1.show() 
img_log=img1.point(lambda x: 255*np.log10(1+x/255))
figure(3)
img_log.show()
Power Law Transformation
• Power  Law  Transformation  is  of  two  types  of 
transformation nth  power transformation and nth 
root transformation.
• The formula for Power law transformation is
s = cr ^ γ  
• Here, γ is gamma, by which this transformation 
is known as gamma transformation.
• All  display  devices  have  their  own  gamma 
correction. That is why images are displayed at 
different intensity.
• These  transformations  are  used  for  enhancing 
images.
Power Law Transformation
from PIL import Image
import numpy as np
from pylab import *
img=Image.open('C:/Users/hp/Desktop/Images_ECE348/pa
rrot.jpg')
figure(1)
img.show()
img1=img.convert('L')       
figure(2)
img1.show()
 img_log=img1.point(lambda x:255*(x/255)**2)
figure(3)
img_log.show()
Image Blurring
Image Blurring
The cv2.medianBlur is used to blur the images.
import cv2
import numpy
from matplotlib import pyplot as plt
image=cv2.imread('C:/Users/hp/Desktop/Images_ECE348/parrot.jpg')
img=cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 
plt.subplot(231),plt.imshow(img)
gray=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
plt.subplot(232),plt.imshow(gray,cmap='gray')

image_MedianBlur=cv2.medianBlur(gray,7)
plt.subplot(233),plt.imshow(image_MedianBlur,cmap='gray')

image_MedianBlur=cv2.medianBlur(gray,11)
plt.subplot(234),plt.imshow(image_MedianBlur,cmap='gray')

image_MedianBlur=cv2.medianBlur(gray,13)
plt.subplot(235),plt.imshow(image_MedianBlur,cmap='gray')

image_MedianBlur=cv2.medianBlur(gray,17)
plt.subplot(236),plt.imshow(image_MedianBlur,cmap='gray')
Image Blurring
import cv2
import numpy
from matplotlib import pyplot as plt
image=cv2.imread('C:/Users/hp/Desktop/Images_ECE348/parrot.jpg')
img=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
plt.subplot(221), plt.imshow(img)
gray=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
plt.subplot(222), plt.imshow(gray,cmap='gray')
image_GaussianBlur=cv2.GaussianBlur(gray,(9,9),10)                                         
#color conversion, size, sigma
plt.subplot(223), plt.imshow(image_GaussianBlur,cmap='gray')
image_GaussianBlur1=cv2.GaussianBlur(gray,(19,19),10)
plt.subplot(224), plt.imshow(image_GaussianBlur1,cmap='gray')
Image Restoration
The purpose of image restoration is to
"compensate for" or "undo" defects which
degrade an image.

Degradation comes in many forms such as


motion blur, noise, and camera misfocus. In
cases like motion blur, it is possible to come
up with an very good estimate of the actual
blurring function and "undo" the blur to
restore the original image. In cases where the
image is corrupted by noise, the best we may
hope to do is to compensate for the
degradation it caused
# image restoration
import numpy as np
import cv2

from matplotlib import pyplot as plt

src = cv2.imread('C:/Users/User/Desktop/1.jpg')
image = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY )

sharpen_kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])


blur = cv2.GaussianBlur(image, (3,3), 0)
Restoredimg = cv2.filter2D(blur, -1, sharpen_kernel)

cv2.imshow('orimg',image)
cv2.imshow('restoredimg.png',Restoredimg)
#cv2.imwrite('restoredimg.png',Restoredimg)
cv2.imshow('blur',blur)
cv2.waitKey(0)
IMAGE SEGMENTATION
Segmentation

17
Segmentation

18
DEFINITION
We  can  divide  or  partition  the  image  into  various  parts 
called segments. It’s not a great idea to process the entire 
image  at  the  same  time  as  there  will  be  regions  in  the 
image which do not contain any information. By dividing 
the  image  into  segments,  we  can  make  use  of  the 
important  segments  for  processing  the  image.  That,  in  a 
nutshell, is how image segmentation works.
Image segmentation using
Morphological operations in
Python
import numpy as np
import cv2
from matplotlib import pyplot as plt
path = r'C:\Users\kirti\Desktop\1.jpg'
src = cv2.imread(path)
window_name = 'Image'
image = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY )
ret, thresh = cv2.threshold(image, 0, 255,
cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
cv2.imshow('image', thresh)
cv2.waitKey(0)
K means clustering
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
path = r'C:\Users\kirti\Desktop\1.jpg'
src = cv2.imread(path)
window_name = 'Image'
image = cv2.cvtColor(src,
cv2.COLOR_BGR2GRAY )
K means clustering
reshapedImage = np.float32(image.reshape(-1, 3))
numberOfClusters = 2

stopCriteria = (cv.TERM_CRITERIA_EPS +
cv.TERM_CRITERIA_MAX_ITER, 100, 0.1)

ret, labels, clusters = cv.kmeans(reshapedImage,


numberOfClusters, None, stopCriteria, 10,
cv.KMEANS_RANDOM_CENTERS)

clusters = np.uint8(clusters)
K means clustering
intermediateImage =
clusters[labels.flatten()]
clusteredImage =
intermediateImage.reshape((image.shape))

cv.imwrite("clusteredImage.jpg",
clusteredImage)
cv2.imshow('image',clusteredImage)
cv2.waitKey(0)
CANNY EDGE
DETECTION
removedCluster = 1

cannyImage = np.copy(image).reshape((-1, 3))


cannyImage[labels.flatten() == removedCluster] =
[0, 0, 0]

cannyImage =
cv.Canny(cannyImage,100,200).reshape(image.sh
ape)
cv.imwrite("cannyImage.jpg", cannyImage)
cv2.imshow('image',cannyImage)
cv2.waitKey(0)
Thank you

You might also like