Advertisement
sajid006

sobel

Nov 10th, 2021
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import cv2
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5. SIZE=250
  6.  
  7. def normalize(img):
  8. nImg = np.zeros(img.shape)
  9.  
  10. max_ = img.max()
  11. min_ = img.min()
  12.  
  13. for i in range(img.shape[0]):
  14. for j in range(img.shape[1]):
  15. nImg[i][j] = (img[i][j]-min_)/(max_-min_) * 255
  16.  
  17. return nImg
  18.  
  19. #img = cv2.imread("1st.png", cv2.IMREAD_GRAYSCALE)
  20. img = cv2.imread("Lena.jpg", cv2.IMREAD_GRAYSCALE)
  21. img = cv2.resize(img, (SIZE, SIZE))
  22. cv2.imshow("input", img)
  23. cv2.waitKey(0)
  24.  
  25. #cv2.destroyAllWindows()
  26.  
  27. sobel_x = np.array([[-1, 0, 1],
  28. [-2, 0, 2],
  29. [-1, 0, 1]])
  30.  
  31. sobel_y = np.array([[1, 2, 1],
  32. [0, 0, 0],
  33. [-1, -2, -1]])
  34.  
  35. sx = cv2.filter2D(src=img, ddepth=-1, kernel=sobel_x)
  36. #sx = normalize(sx)
  37.  
  38. cv2.imshow("sobel_x", sx)
  39. cv2.waitKey(0)
  40.  
  41. sy = cv2.filter2D(src=img, ddepth=-1, kernel=sobel_y)
  42. #sy = normalize(sy)
  43.  
  44. cv2.imshow("sobel_y", sy)
  45. cv2.waitKey(0)
  46.  
  47. tr = 50
  48.  
  49. sx = normalize(sx)
  50. sy = normalize(sy)
  51.  
  52. out = np.sqrt(np.multiply(sx, sx) + np.multiply(sy, sy))
  53. out = normalize(out)
  54. #out = np.array(out, dtype='uint8')
  55. out[out>=tr] = 255
  56. out[out<tr] = 0
  57.  
  58. cv2.imshow("output", out)
  59. cv2.waitKey(0)
  60.  
  61. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement