You are on page 1of 6

College of Electrical & Mechanical Engineering, NUST

Department of Computer & Software Engineering

DIGITAL IMAGE PROCESSING

LAB 7

SUBMITTED BY:

Amna Ahmed

SYNDICATE:

CE 42 A

REGISTERATION NO:

345952

Digital Image Processing Amna Ahmed - 345952


College of Electrical & Mechanical Engineering, NUST
Department of Computer & Software Engineering

LAB # 04: Edge Detection & Segmentation


Lab Objective:
The objective of this lab is to apply differential spatial filters ,edge detectors and image
segmentation techniques

LAB TASKS:

Code:
import numpy as np
import math
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('input2.png', 0)
gx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
gy = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
G = np.sqrt((gx)**2 + (gy)**2)
v = np.arctan(gy, gx)
angle = np.degrees(v)
#print(angle)
#if phase is 45 and 90
for i in range(angle.shape[0]):
for j in range(angle.shape[1]):
if angle[i, j] == 45 and angle[i, j] == 90:
angle[i, j] = angle[i, j]
cv2.imshow("Magnitude equals to 45 and 90", angle)
cv2.waitKey()
#if mag is top 30%
maximum = np.amax(G)
thirty = maximum * 0.7
for i in range(G.shape[0]):
for j in range(G.shape[1]):
if G[i, j] >= thirty:
G[i, j] = G[i, j]

Digital Image Processing Amna Ahmed - 345952


College of Electrical & Mechanical Engineering, NUST
Department of Computer & Software Engineering
cv2.imshow("Top 30%", G)
cv2.waitKey()
# for top 30 and 45 and 90
for i in range(G.shape[0]):
for j in range(G.shape[1]):
if G[i, j] >= thirty and angle[i, j] == 90 and angle[i, j] == 45:
G[i, j] = (angle[i, j]) + (G[i, j])
cv2.imshow("Top 30% and 45 and 90", G)
cv2.waitKey()

Output:

Digital Image Processing Amna Ahmed - 345952


College of Electrical & Mechanical Engineering, NUST
Department of Computer & Software Engineering

Digital Image Processing Amna Ahmed - 345952


College of Electrical & Mechanical Engineering, NUST
Department of Computer & Software Engineering

Code:
import numpy as np
import math
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('input.png', 0)
dims = np.shape(img)
r, c = img.shape
mean_th = np.zeros((r, c), dtype=np.uint8)
median_th = np.zeros((r, c), dtype=np.uint8)
mean = np.mean(img)
median = np.median(img)
for i in range(0, r):
for j in range(0, c):
if img[i, j] <= mean:
mean_th[i, j] = 0
else:
mean_th[i, j] = 255
for i in range(0, r):
for j in range(0, c):
if img[i, j] <= median:
median_th[i, j] = 0
else:
median_th[i, j] = 255
cv2.imshow('Original Image', img)
cv2.imshow('Image 01', mean_th)
cv2.imshow('Image 02', median_th)
cv2.waitKey()

Output:

Digital Image Processing Amna Ahmed - 345952


College of Electrical & Mechanical Engineering, NUST
Department of Computer & Software Engineering

Code:
import numpy as np
import math
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('input.png', 0)
r, c = img.shape
l_th = np.zeros((r, c), dtype=np.uint8)
for i in range(0, r, 3):
for j in range(0, c, 3):
new_img = img[i:i+3, j:j+3]
Mean = np.mean(new_img)
Mean = Mean-3
if img[i, j] <= Mean:
l_th[i, j] = 0
else:
l_th[i, j] = 255
cv2.imshow('', l_th)
cv2.waitKey()

Output:

Digital Image Processing Amna Ahmed - 345952

You might also like