Advertisement
sajid006

erode

Nov 10th, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 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("Picture1.png", cv2.IMREAD_GRAYSCALE)
  21. img = cv2.resize(img, (SIZE, SIZE))
  22. cv2.imshow("input", img)
  23. cv2.waitKey(0)
  24.  
  25. tr = 150
  26.  
  27. img[img>=tr] = 255
  28. img[img<tr] = 0
  29.  
  30. cv2.imshow("binary", img)
  31. cv2.waitKey(0)
  32.  
  33. element = np.array([[1, 1, 1],
  34. [1, 1, 1],
  35. [1, 1, 1]])
  36.  
  37. dil = cv2.dilate(img, element)
  38. cv2.imshow("dilated", dil)
  39. cv2.waitKey(0)
  40.  
  41. cv2.imshow("dilated_boundary", dil-img)
  42. cv2.waitKey(0)
  43.  
  44. er = cv2.erode(img, element)
  45. cv2.imshow("eroded", er)
  46. cv2.waitKey(0)
  47.  
  48. cv2.imshow("eroded_boundary", img-er)
  49. cv2.waitKey(0)
  50.  
  51. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement