SHARE
TWEET

Untitled

a guest Feb 14th, 2020 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Module for processing images with Sobel operator
  2. # Authors FeeLyX, OPyshkin, Esesna
  3. # Version 1.2
  4.  
  5. import numpy as np
  6. import cv2
  7.  
  8. # Opening image from file
  9. imginput = cv2.imread('Garceev.jpg', cv2.IMREAD_GRAYSCALE)
  10.  
  11. # Initializing dimensions of picture
  12. y = len(imginput)
  13. x = len(imginput[0])
  14.  
  15. # Expanding edges of picture with zeros
  16. imgext = np.zeros((y + 2, x + 2))
  17. for i in range(y):
  18.     for j in range(x):
  19.         imgext[i + 1][j + 1] = imginput[i][j]
  20.  
  21. # Declaring arrays for processed picture
  22. imgsobelx = np.zeros((y, x))
  23. #imgsobely = np.zeros((y, x))
  24. #imgsobel = np.zeros((y, x))
  25.  
  26. # Initializing X mask
  27. sobelx = [
  28.     [-1, 0, 1],
  29.     [-2, 0, 2],
  30.     [-1, 0, 1]
  31.     ]
  32.  
  33. # Initializing Y mask
  34. #sobely = [
  35. #    [-1, -2, -1],
  36. #    [0, 0, 0],
  37. #    [1, 2, 1]
  38. #    ]
  39.  
  40. # Processing image
  41. for i in range(y):
  42.     for j in range(x):
  43.         piece = np.zeros((3, 3))
  44.         for k in range(3):
  45.             for l in range(3):
  46.                 piece[k][l] = imgext[i + k][j + l]
  47.         imgsobelx[i][j] = np.sum(piece * sobelx)
  48.         #imgsobely[i][j] = np.sum(piece * sobely)
  49.         #imgsobel[i][j] = np.sqrt(imgsobelx[i][j] ** 2 +
  50.         #                         imgsobely[i][j] ** 2)
  51.  
  52. # Writing final pictures into files
  53. cv2.imwrite('sobelx.jpg', imgsobelx)
  54. #cv2.imwrite('sobely.jpg', imgsobely)
  55. #cv2.imwrite('sobel.jpg', imgsobel)
  56.  
  57. # Waiting for any key pressed to exit
  58. cv2.waitKey(0)
  59. cv2.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top