0% found this document useful (0 votes)
71 views9 pages

Dip Assignment No 4

This document appears to be an assignment for a digital image processing course. It contains code to: 1. Create a mask from user-input values and size. It then applies the mask as a filter to an image. 2. Add padding of a specified size to an image, either with zeros or copying neighboring pixels. 3. Normalize an image to values between 0-255. It then applies predefined masks as filters and displays the results.

Uploaded by

Tahreem Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views9 pages

Dip Assignment No 4

This document appears to be an assignment for a digital image processing course. It contains code to: 1. Create a mask from user-input values and size. It then applies the mask as a filter to an image. 2. Add padding of a specified size to an image, either with zeros or copying neighboring pixels. 3. Normalize an image to values between 0-255. It then applies predefined masks as filters and displays the results.

Uploaded by

Tahreem Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Fatima Jinnah Women University

Department of Computer Science


--------------------------------------------------------------------------------------

m
er as
ASSIGNMENT # 04

co
eH w
o.
rs e
ou urc
Subject: Digital Image Processing
o

Submitted to: Ma’am Andleeb Yousuf


aC s
vi y re

Semester: BCSVI (B)


ed d

Submitted by: Shaffaq Siddique…….... (025)


ar stu

Submission Date: 12th June, 2020


is
Th
sh

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

print("Enter the Value of mask")


aC s

for i in range(mask_size):
vi y re

for j in range(mask_size):
mask[i][j]=float(input())
ed d

print("The Mask is:\n\n",mask)


ar stu

im=cv2.imread('cameraman.TIF')
def padding_img(image):
is

image = cv2.copyMakeBorder(im, 10,10,10,10, cv2.BORDER_CONSTANT)


Th

cv2.imshow(' Image', image)


def filter_img(img):
sh

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

Padding Filter Normalized

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

from __future__ import print_function


import numpy as np
import cv2
im=cv2.imread('cameraman.TIF')
mask1= np.array([[1/9,1/9,1/9],
[1/9,1/9,1/9],
[1/9,1/9,1/9]],np.float32)
mask2 = np.array([[1/25,1/25,1/25,1/25,1/25],
[1/25,1/25,1/25,1/25,1/25],
[1/25,1/25, 1/25,1/25,1/25],

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

[-1,0,1]], np.float32) # kernel should be floating point type


mask5= np.array([[-1,-2,-1],
ed d
ar stu

[0,1,0],
[1,2,1]], np.float32) # kernel should be floating point type
dst1 = cv2.filter2D(im, -1, mask1)
is
Th

dst2 = cv2.filter2D(im, -1, mask2)


dst3 = cv2.filter2D(im, -1, mask3)
dst4 = cv2.filter2D(im, -1, mask4)
sh

dst5 = cv2.filter2D(im, -1, mask5)


# ddepth = -1, means destination image has depth same as input image
cv2.imshow("Input", im)

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)

You might also like