Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Tue Nov 30 12:45:12 2021
- @author: Sajid
- """
- import numpy as np
- from scipy import ndimage
- import cv2
- def gaussian_kernel(size, sigma=1):
- size = int(size) // 2
- x, y = np.mgrid[-size:size+1, -size:size+1]
- normal = 1 / (2.0 * np.pi * sigma**2)
- g = np.exp(-((x**2 + y**2) / (2.0*sigma**2))) * normal
- return g
- def sobel_filters(img):
- Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float32)
- Ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float32)
- Ix = cv2.filter2D(img, -1, Kx)
- Ix = cv2.convertScaleAbs(Ix)
- Iy = cv2.filter2D(img, -1, Ky)
- Iy = cv2.convertScaleAbs(Iy)
- sobel_img = np.hypot(Ix, Iy).astype(np.uint8)
- sobel_img = cv2.convertScaleAbs(sobel_img)
- theta = np.arctan2(Iy, Ix)
- return sobel_img,theta
- def non_max_suppression(img, D):
- M, N = img.shape
- Z = np.zeros((M,N), dtype=np.int32)
- angle = D * 180. / np.pi
- angle[angle < 0] += 180
- for i in range(1,M-1):
- for j in range(1,N-1):
- try:
- q = 255
- r = 255
- #angle 0
- if (0 <= angle[i,j] < 22.5) or (157.5 <= angle[i,j] <= 180):
- q = img[i, j+1]
- r = img[i, j-1]
- #angle 45
- elif (22.5 <= angle[i,j] < 67.5):
- q = img[i+1, j-1]
- r = img[i-1, j+1]
- #angle 90
- elif (67.5 <= angle[i,j] < 112.5):
- q = img[i+1, j]
- r = img[i-1, j]
- #angle 135
- elif (112.5 <= angle[i,j] < 157.5):
- q = img[i-1, j-1]
- r = img[i+1, j+1]
- if (img[i,j] >= q) and (img[i,j] >= r):
- Z[i,j] = img[i,j]
- else:
- Z[i,j] = 0
- except IndexError as e:
- pass
- return Z
- def threshold(img, lowThresholdRatio=0.05, highThresholdRatio=0.09):
- highThreshold = img.max() * highThresholdRatio;
- lowThreshold = highThreshold * lowThresholdRatio;
- M, N = img.shape
- res = np.zeros((M,N), dtype=np.int32)
- weak = np.int32(25)
- strong = np.int32(255)
- strong_i, strong_j = np.where(img >= highThreshold)
- zeros_i, zeros_j = np.where(img < lowThreshold)
- weak_i, weak_j = np.where((img <= highThreshold) & (img >= lowThreshold))
- res[strong_i, strong_j] = strong
- res[weak_i, weak_j] = weak
- return (res, weak, strong)
- def hysteresis(img, weak, strong=255):
- M, N = img.shape
- for i in range(1, M-1):
- for j in range(1, N-1):
- if (img[i,j] == weak):
- try:
- if ((img[i+1, j-1] == strong) or (img[i+1, j] == strong) or (img[i+1, j+1] == strong)
- or (img[i, j-1] == strong) or (img[i, j+1] == strong)
- or (img[i-1, j-1] == strong) or (img[i-1, j] == strong) or (img[i-1, j+1] == strong)):
- img[i, j] = strong
- else:
- img[i, j] = 0
- except IndexError as e:
- pass
- return img
- img = cv2.imread('can.jpg',cv2.IMREAD_GRAYSCALE)
- cv2.imshow('Input Image', img)
- kernel = gaussian_kernel(5)
- smoothed_image = cv2.filter2D(img, -1, kernel)
- smoothed_image = cv2.convertScaleAbs(smoothed_image)
- cv2.imshow('Noiseless Image',smoothed_image)
- sobel_img,theta = sobel_filters(smoothed_image)
- cv2.imshow('Sobel Filtered',sobel_img)
- noMaximg = non_max_suppression(sobel_img, theta)
- noMaximg = cv2.convertScaleAbs(noMaximg)
- cv2.imshow('Non Maximum Suppression',noMaximg)
- thresimg,w,s = threshold(noMaximg)
- thresimg = cv2.convertScaleAbs(thresimg)
- cv2.imshow('Double thresholding', thresimg)
- output = hysteresis(thresimg, w, s)
- cv2.imshow('Final Output',output)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement