Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Module for processing images with Sobel operator
- # Authors FeeLyX, OPyshkin, Esesna
- # Version 1.2
- import numpy as np
- import cv2
- # Opening image from file
- imginput = cv2.imread('Garceev.jpg', cv2.IMREAD_GRAYSCALE)
- # Initializing dimensions of picture
- y = len(imginput)
- x = len(imginput[0])
- # Expanding edges of picture with zeros
- imgext = np.zeros((y + 2, x + 2))
- for i in range(y):
- for j in range(x):
- imgext[i + 1][j + 1] = imginput[i][j]
- # Declaring arrays for processed picture
- imgsobelx = np.zeros((y, x))
- #imgsobely = np.zeros((y, x))
- #imgsobel = np.zeros((y, x))
- # Initializing X mask
- sobelx = [
- [-1, 0, 1],
- [-2, 0, 2],
- [-1, 0, 1]
- ]
- # Initializing Y mask
- #sobely = [
- # [-1, -2, -1],
- # [0, 0, 0],
- # [1, 2, 1]
- # ]
- # Processing image
- for i in range(y):
- for j in range(x):
- piece = np.zeros((3, 3))
- for k in range(3):
- for l in range(3):
- piece[k][l] = imgext[i + k][j + l]
- imgsobelx[i][j] = np.sum(piece * sobelx)
- #imgsobely[i][j] = np.sum(piece * sobely)
- #imgsobel[i][j] = np.sqrt(imgsobelx[i][j] ** 2 +
- # imgsobely[i][j] ** 2)
- # Writing final pictures into files
- cv2.imwrite('sobelx.jpg', imgsobelx)
- #cv2.imwrite('sobely.jpg', imgsobely)
- #cv2.imwrite('sobel.jpg', imgsobel)
- # Waiting for any key pressed to exit
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement