Dip Assignment No 4
Dip Assignment No 4
m
er as
ASSIGNMENT # 04
co
eH w
o.
rs e
ou urc
Subject: Digital Image Processing
o
Tasks
This study source was downloaded by 100000796410279 from CourseHero.com on 04-13-2021 14:24:59 GMT -05:00
https://www.coursehero.com/file/65828228/DIP-ASSIGNMENT-NO-4docx/
Assignment # 04 Digital Image Processing
Question # 01:
Part a.
Write generic functions for each of the following.
Takes input mask size and their numeric values from the user and create the mask.
Take image and padding size as argument and add padding on the image
(either zeros or copy neighboring pixel)
Take image and filter as argument and apply the filter to the image.
Take image as input and normalize the image between 0 – 255.
Code:
import cv2
m
er as
import numpy as np
co
print("""
eH w
o.
Enter the Size of the Mask: """)
mask_size= int(input()) rs e
ou urc
mask= np.zeros((mask_size,mask_size), dtype='float')
o
for i in range(mask_size):
vi y re
for j in range(mask_size):
mask[i][j]=float(input())
ed d
im=cv2.imread('cameraman.TIF')
def padding_img(image):
is
img_rst = cv2.filter2D(im,-1,mask)
cv2.imshow("outpir",img_rst)
def norm_img(img):
2
This study source was downloaded by 100000796410279 from CourseHero.com on 04-13-2021 14:24:59 GMT -05:00
https://www.coursehero.com/file/65828228/DIP-ASSIGNMENT-NO-4docx/
Assignment # 04 Digital Image Processing
norm_img = np.zeros((800,800))
final_img = cv2.normalize(im, norm_img, 0, 255, cv2.NORM_MINMAX)
cv2.imshow('Normalized Image', final_img)
cv2.waitKey(0)
padding_img(im)
cv2.waitKey(0)
filter_img(im)
cv2.waitKey(0)
norm_img(im)
cv2.waitKey(0)
m
er as
cv2.destroyAllWindows()
co
eH w
Output:
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh
3
This study source was downloaded by 100000796410279 from CourseHero.com on 04-13-2021 14:24:59 GMT -05:00
https://www.coursehero.com/file/65828228/DIP-ASSIGNMENT-NO-4docx/
Assignment # 04 Digital Image Processing
m
er as
Part b.
co
eH w
Now Apply the following masks on the image and display the results
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
Code:
sh
"""
Created on Sun Jul 12 16:30:57 2020
@author: Shaffaq
"""
4
This study source was downloaded by 100000796410279 from CourseHero.com on 04-13-2021 14:24:59 GMT -05:00
https://www.coursehero.com/file/65828228/DIP-ASSIGNMENT-NO-4docx/
Assignment # 04 Digital Image Processing
m
er as
[1/25,1/25,1/25,1/25,1/25],
co
eH w
[1/25,1/25,1/25,1/25,1/25]], np.float32) # kernel should be floating point type
o.
mask3= np.array([[-1,-2,-1],
rs e
ou urc
[0,1,0],
[1,2,1]], np.float32) # kernel should be floating point type
o
mask4= np.array([[-1,0,1],
aC s
[-2,0,-2],
vi y re
[0,1,0],
[1,2,1]], np.float32) # kernel should be floating point type
dst1 = cv2.filter2D(im, -1, mask1)
is
Th
5
This study source was downloaded by 100000796410279 from CourseHero.com on 04-13-2021 14:24:59 GMT -05:00
https://www.coursehero.com/file/65828228/DIP-ASSIGNMENT-NO-4docx/
Assignment # 04 Digital Image Processing
cv2.waitKey(0)
cv2.imshow("Mask1", dst1)
cv2.waitKey(0)
cv2.imshow("Mask2", dst2)
cv2.waitKey(0)
cv2.imshow("Mask3", dst3)
cv2.waitKey(0)
cv2.imshow("Mask4", dst4)
cv2.waitKey(0)
cv2.imshow("Mask5", dst5)
m
er as
cv2.waitKey(0)
co
eH w
cv2.destroyAllWindows()
o.
Output:
rs e
ou urc
o
aC s
Input Image:
vi y re
ed d
ar stu
is
Th
sh
6
This study source was downloaded by 100000796410279 from CourseHero.com on 04-13-2021 14:24:59 GMT -05:00
https://www.coursehero.com/file/65828228/DIP-ASSIGNMENT-NO-4docx/
Assignment # 04 Digital Image Processing
m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh
7
This study source was downloaded by 100000796410279 from CourseHero.com on 04-13-2021 14:24:59 GMT -05:00
https://www.coursehero.com/file/65828228/DIP-ASSIGNMENT-NO-4docx/
Assignment # 04 Digital Image Processing
Question # 02:
Write your own function to implement median() filter. Apply median filter on saltpep.tif
image to remove salt and pepper noise.
Code:
import cv2
import numpy as np
img = cv2.imread('saltpep.TIF')
median = cv2.medianBlur(img, 5)
compare = np.concatenate((img, median), axis=1) #side by side comparison
cv2.imshow('img', compare)
m
er as
cv2.waitKey(0)
co
eH w
cv2.destroyAllWindows
o.
Output:
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh
CONCLUSION
8
This study source was downloaded by 100000796410279 from CourseHero.com on 04-13-2021 14:24:59 GMT -05:00
https://www.coursehero.com/file/65828228/DIP-ASSIGNMENT-NO-4docx/
Assignment # 04 Digital Image Processing
After solving the given assignment, I have come to know more about the ways through which an
image can be manipulated and quality can be enhanced. No wonder that median filter is widely
used in digital image processing. It actually removes noise from an image and improves the
result of later processing. I’ve come to learn about different techniques of masking as well i.e.
how we can mask different portions of the image and how we can hide the desired parts to get
the desired result.
REFERENCES
https://www.geeksforgeeks.org/mahotas-median-filter/amp/
https://www.geeksforgeeks.org/spatial-filtering-and-its-types/#:~:text=Flight%20(ToF)
%20Sensors-,Spatial%20Filtering%20and%20its%20Types,mask%20traverses%20all%20image
%20pixels.
m
er as
https://note.nkmk.me/en/python-opencv-numpy-alpha-blend-mask/
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh
9
This study source was downloaded by 100000796410279 from CourseHero.com on 04-13-2021 14:24:59 GMT -05:00
https://www.coursehero.com/file/65828228/DIP-ASSIGNMENT-NO-4docx/
Powered by TCPDF (www.tcpdf.org)