Created
July 4, 2017 18:26
-
-
Save NikaTsanka/74c862540bacd1c35ab52379dec032c7 to your computer and use it in GitHub Desktop.
Prewitt's Operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
# Open the image | |
img = np.array(Image.open('dancing-spider.jpg')).astype(np.uint8) | |
# Apply gray scale | |
gray_img = np.round(0.299 * img[:, :, 0] + | |
0.587 * img[:, :, 1] + | |
0.114 * img[:, :, 2]).astype(np.uint8) | |
# Prewitt Operator | |
h, w = gray_img.shape | |
# define filters | |
horizontal = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]) # s2 | |
vertical = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]) # s1 | |
# define images with 0s | |
newgradientImage = np.zeros((h, w)) | |
# offset by 1 | |
for i in range(1, h - 1): | |
for j in range(1, w - 1): | |
horizontalGrad = (horizontal[0, 0] * gray_img[i - 1, j - 1]) + \ | |
(horizontal[0, 1] * gray_img[i - 1, j]) + \ | |
(horizontal[0, 2] * gray_img[i - 1, j + 1]) + \ | |
(horizontal[1, 0] * gray_img[i, j - 1]) + \ | |
(horizontal[1, 1] * gray_img[i, j]) + \ | |
(horizontal[1, 2] * gray_img[i, j + 1]) + \ | |
(horizontal[2, 0] * gray_img[i + 1, j - 1]) + \ | |
(horizontal[2, 1] * gray_img[i + 1, j]) + \ | |
(horizontal[2, 2] * gray_img[i + 1, j + 1]) | |
verticalGrad = (vertical[0, 0] * gray_img[i - 1, j - 1]) + \ | |
(vertical[0, 1] * gray_img[i - 1, j]) + \ | |
(vertical[0, 2] * gray_img[i - 1, j + 1]) + \ | |
(vertical[1, 0] * gray_img[i, j - 1]) + \ | |
(vertical[1, 1] * gray_img[i, j]) + \ | |
(vertical[1, 2] * gray_img[i, j + 1]) + \ | |
(vertical[2, 0] * gray_img[i + 1, j - 1]) + \ | |
(vertical[2, 1] * gray_img[i + 1, j]) + \ | |
(vertical[2, 2] * gray_img[i + 1, j + 1]) | |
# Edge Magnitude | |
mag = np.sqrt(pow(horizontalGrad, 2.0) + pow(verticalGrad, 2.0)) | |
newgradientImage[i - 1, j - 1] = mag | |
plt.figure() | |
plt.title('dancing-spider-prewitt.png') | |
plt.imsave('dancing-spider-prewitt.png', newgradientImage, cmap='gray', format='png') | |
plt.imshow(newgradientImage, cmap='gray') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment