Digital Image Processing
(Program From 1 to 10)
Name: Gulshan Kumar
Course: MCA 3rd Sem
Roll No. : 22PGMCA15
1. Write a program to demonstrate quantization.
import [Link] as plt
import numpy as np
im = [Link]('[Link]')
[Link](im)
[Link]('Original Image')
[Link]()
im_d = [Link](float)
c0 = [Link](im_d, 2)
[Link](c0)
[Link]('Zero bit Image')
[Link]()
c1 = [Link]([Link](im_d/2), 2)
[Link](c1)
[Link]('First bit Image')
[Link]()
c2 = [Link]([Link](im_d/4), 2)
[Link](c2)
[Link]('Second bit Image')
[Link]()
c3 = [Link]([Link](im_d/8), 2)
[Link](c3)
[Link]('Third bit Image')
[Link]()
c4 = [Link]([Link](im_d/16), 2)
[Link](c4)
[Link]('Fourth bit Image')
[Link]()
c5 = [Link]([Link](im_d/32), 2)
[Link](c5)
[Link]('Fifth bit Image')
[Link]()
c6 = [Link]([Link](im_d/64), 2)
[Link](c6)
[Link]('Sixth bit Image')
[Link]()
c7 = [Link]([Link](im_d/128), 2)
[Link](c7)
[Link]('Seventh bit Image')
[Link]()
cc = 2*(2*(2*(2*(2*(2*(2*c7+c6)+c5)+c4)+c3)+c2)+c1)+c0
[Link]([Link](np.uint8))
[Link]('Reconstructed Original Image')
[Link]()
2. Write a program to demonstrate down sampling.
import cv2
import [Link] as plt
im = [Link]('[Link]', 0)
[Link](im, cmap='gray')
[Link]('Original image')
[Link]()
[Link]([Link](), bins=256, range=[0,256])
[Link]('tight')
[Link]('Image Histogram')
[Link]()
im_h = [Link](im)
[Link](im_h, cmap='gray')
[Link]('Histogram equalized image')
[Link]()
[Link](im_h.ravel(), bins=256, range=[0,256])
[Link]('tight')
[Link]('Histogram of equalized image')
[Link]()
3. Write a program to perform bit plain slicing.
import cv2
import numpy as np
import [Link] as plt
I = [Link]('[Link]', cv2.IMREAD_GRAYSCALE)
[Link](2,2,1)
[Link](I, cmap='gray')
[Link]('Original Image')
H = [Link]((20, 20))
H[10, :] = 1
H = H / [Link](H)
MotionBlur = cv2.filter2D(I, -1, H, borderType=cv2.BORDER_REPLICATE)
[Link](2,2,2)
[Link](MotionBlur, cmap='gray')
[Link]('Motion Blurred Image')
H = [Link](cv2.MORPH_ELLIPSE, (10, 10))
blurred = cv2.filter2D(I, -1, H, borderType=cv2.BORDER_REPLICATE)
[Link](2,2,3)
[Link](blurred, cmap='gray')
[Link]('Blurred Image')
H = [Link]([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
sharpened = cv2.filter2D(I, -1, H, borderType=cv2.BORDER_REPLICATE)
[Link](2,2,4)
[Link](sharpened, cmap='gray')
[Link]('Sharpened Image')
[Link]()
4. Write a program to demonstrate histogram equalization.
import numpy as np
import [Link] as plt
from [Link] import fftshift, fft2
from PIL import Image
a = [Link]((256, 256))
a[78:178, 78:178] = 1
[Link](a), [Link]('Image with a square')
[Link]()
af = fftshift(fft2(a))
[Link]([Link](1 + [Link](af))), [Link]('Fourier transform of square image')
[Link]()
x, y = [Link]([Link](1, 257), [Link](1, 257))
b = (x + y < 329) & (x + y > 182) & (x - y > -67) & (x - y < 73)
[Link](b), [Link]('Rotated square image')
[Link]()
bf = fftshift(fft2(b))
[Link]([Link](1 + [Link](bf))), [Link]('Fourier transform of rotated square
image')
[Link]()
x, y = [Link]([Link](-128, 218), [Link](-128, 128))
c = [Link]([Link]('[Link]'))
[Link](c), [Link]('Cameraman image')
[Link]()
cf = fftshift(fft2(c))
[Link]([Link](1 + [Link](cf))), [Link]('Fourier transform of Cameraman
image')
[Link]()
p, q = [Link]([Link](-128, 218), [Link](-128, 128))
r = [Link](p**2 + q**2)
s = (r < 15)
cf = fftshift(fft2(s))
[Link](s), [Link]('Circle')
[Link]()
[Link]([Link](1 + [Link](cf))), [Link]('Fourier transform of Circle')
[Link]()
5. Write a program to smoothing and sharpening using spatial filters.
import numpy as np
import [Link] as plt
from [Link] import fftshift, fft2, ifft2
cm = [Link]('[Link]')
[Link](cm)
[Link]('Input image')
[Link]()
cf = fftshift(fft2(cm))
[Link]([Link](1 + [Link](cf)))
[Link]('Fourier transform of input image')
[Link]()
x, y = [Link]([Link](-128, 128), [Link](-128, 128))
z = [Link](x**2 + y**2)
c = (z < 15) # Ideal lowpass filter
cfl = cf * c
[Link]([Link](1 + [Link](cfl)))
[Link]('Ideal lowpass filter')
[Link]()
cfli = ifft2(cfl)
[Link]([Link](cfli))
[Link]('Image smoothing using ideal lowpass filter')
[Link]()
c = (z > 15) # Ideal highpass filter
cfh = cf * c
[Link]([Link](1 + [Link](cfh)))
[Link]('Ideal highpass filter')
[Link]()
cfhi = ifft2(cfh)
[Link]([Link](cfhi))
[Link]('Image sharpening using ideal highpass filter')
[Link]()
6. Write a program to generate Fourier transform of an image.
# Assignment 6: Noise generation
import cv2
import numpy as np
# Load the image
tw = [Link]('[Link]')
[Link]('Input image', tw)
[Link](0)
[Link]()
# Add salt and pepper noise
t_sp = [Link]('[Link]')
noise = [Link](0, 2, size=t_sp.shape[:2])
t_sp[noise == 0] = 0
t_sp[noise == 1] = 255
[Link]('Image with salt & pepper noise', t_sp)
[Link](0)
[Link]()
# Add Gaussian noise
t_ga = [Link]('[Link]')
mean = 0
stddev = 50
noise = [Link](mean, stddev, size=t_ga.shape).astype(np.uint8)
t_ga = [Link](t_ga, noise)
[Link]('Image with Gaussian noise', t_ga)
[Link](0)
[Link]()
# Add speckle noise
t_spk = [Link]('[Link]')
noise = [Link](*t_spk.shape).astype(np.uint8)
t_spk = t_spk + t_spk * noise
[Link]('Image with speckle noise', t_spk)
[Link](0)
[Link]()
# Add periodic noise
t_pn = [Link]('[Link]')
s = t_pn.shape
x, y = [Link]([Link](s[1]), [Link](s[0]))
p = [Link](x / 3 + y / 5) + 1
t_pn = (t_pn.astype(np.float32) + p / 2) / 2
[Link]('Image with periodic noise', t_pn)
[Link](0)
[Link]()
7. Write a program to perform image smoothing and sharpening
using Ideal filters.
# Assignment 7: Noise removal
import cv2
import numpy as np
# Read the input image
t = [Link]('[Link]', 0)
[Link]('Input image', t)
[Link](0)
[Link]()
# Add salt and pepper noise to the image
t_sp = [Link]('[Link]', 0)
t_sp = [Link](t_sp, cv2.COLOR_BGR2GRAY)
noise = [Link](0, 2, size=t_sp.shape)
t_sp[noise == 0] = 0
t_sp[noise == 1] = 255
[Link]('Image with salt & pepper noise', t_sp)
[Link](0)
[Link]()
# Apply 3x3 averaging filter
a3 = [Link]((3, 3), dtype=np.float32) / 9
t_sp_a3 = cv2.filter2D(t_sp, -1, a3)
[Link]('3x3 averaging', t_sp_a3)
[Link](0)
[Link]()
# Apply 7x7 averaging filter
a7 = [Link]((7, 7), dtype=np.float32) / 49
t_sp_a7 = cv2.filter2D(t_sp, -1, a7)
[Link]('7x7 averaging', t_sp_a7)
[Link](0)
[Link]()
# Apply median filtering
t_sp_m3 = [Link](t_sp, 3)
[Link]('Median filtering', t_sp_m3)
[Link](0)
[Link]()
8. Write a program to add different noise to an image.
# Assignment 8: Dilation and erosion
import cv2
import numpy as np
import [Link] as plt
# Read the image
t = [Link]('[Link]', 0)
# Convert the image to binary
t = [Link](t, 127, 255, cv2.THRESH_BINARY)[1]
# Create a square structuring element
sq = [Link]((2, 2), np.uint8)
# Perform dilation
td = [Link](t, sq, iterations=1)
# Display the original image
[Link](1, 4, 1)
[Link](t, cmap='gray')
[Link]('Original image')
# Display the dilated image
[Link](1, 4, 2)
[Link](td, cmap='gray')
[Link]('Dilated image')
# Perform erosion
ce = [Link](t, sq, iterations=1)
# Display the eroded image
[Link](1, 4, 3)
[Link](ce, cmap='gray')
[Link]('Eroded image')
# Create a larger square structuring element
sq = [Link]((3, 3), np.uint8)
# Perform erosion with the larger structuring element
ce = [Link](t, sq, iterations=1)
# Display the eroded image with the larger structuring element
[Link](1, 4, 4)
[Link](ce, cmap='gray')
[Link]('Eroded image')
# Show all the plots
[Link]()
9. Write a program to remove noise from an image.
# Assignment 9: Boundary detection
import cv2
import numpy as np
import [Link] as plt
im = [Link]('[Link]', 0)
r = [Link](im, 127, 255, cv2.THRESH_BINARY)[1]
sq = [Link]((3,3), np.uint8)
re = [Link](r, sq)
r_int = cv2.bitwise_and(r, cv2.bitwise_not(re))
[Link](2,2,1),[Link](im, cmap='gray'), [Link]('Input image')
[Link](2,2,2),[Link](r_int, cmap='gray'), [Link]('Internal boundary')
rd = [Link](r, sq)
r_ext = cv2.bitwise_and(rd, cv2.bitwise_not(r))
r_grad = cv2.bitwise_and(rd, cv2.bitwise_not(re))
[Link](2,2,3),[Link](r_ext, cmap='gray'), [Link]('External boundary')
[Link](2,2,4),[Link](r_grad, cmap='gray'), [Link]('Morphological
gradient')
[Link]()
10. Write a program to demonstrate dilation and erosion.
# Assignment 10: Morphological Opening and closing
import cv2
import numpy as np
# Read the image
c = [Link]('[Link]', 0)
# Convert the image to binary
c = [Link](c, 127, 255, cv2.THRESH_BINARY)[1]
# Generate random noise
x = [Link](*[Link])
# Apply noise to the image
d1 = [Link](x <= 0.05)
d2 = [Link](x >= 0.95)
c[d1] = 0
c[d2] = 1
# Display the image with noise
[Link]('Circle image with noise', c)
[Link](0)
# Define the structuring elements
sq = [Link]((3, 3), np.uint8)
cr = [Link]([[0, 1, 0], [1, 1, 1], [0, 1, 0]], np.uint8)
# Perform closing and opening operations with square structuring element
cf1 = [Link](c, cv2.MORPH_CLOSE, sq)
cf1 = [Link](cf1, cv2.MORPH_OPEN, sq)
# Display the result
[Link]('Closing & Opening with Square SE', cf1)
[Link](0)
# Perform closing and opening operations with cross structuring element
cf2 = [Link](c, cv2.MORPH_CLOSE, cr)
cf2 = [Link](cf2, cv2.MORPH_OPEN, cr)
# Display the result
[Link]('Closing & Opening with cross SE', cf2)
[Link](0)
# Close all windows
[Link]()