Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. from PIL import Image
  4.  
  5. image = "2YNGEOYOEH0YXBJV.png"
  6.  
  7. input = cv2.imread(image)
  8.  
  9. _, th = cv2.threshold(input, 70, 250, cv2.THRESH_BINARY)
  10.  
  11.  
  12. rgb_planes = cv2.split(th)
  13.  
  14. result_planes = []
  15. result_norm_planes = []
  16. for plane in rgb_planes:
  17. dilated_img = cv2.dilate(plane, np.ones((7, 7), np.uint8))
  18. bg_img = cv2.medianBlur(dilated_img, 11)
  19. diff_img = 255 - cv2.absdiff(plane, bg_img)
  20. norm_img = cv2.normalize(diff_img, dst=0, alpha=80, beta=250, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
  21. result_planes.append(diff_img)
  22. result_norm_planes.append(norm_img)
  23.  
  24. result = cv2.merge(result_norm_planes)
  25.  
  26. gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
  27.  
  28. lower = np.array([0, 0, 0])
  29. upper = np.array([150, 150, 150])
  30.  
  31. mask = cv2.inRange(result, lower, upper)
  32. res = cv2.bitwise_and(result, result, mask= mask)
  33.  
  34.  
  35. cv2.imwrite("th.png", th)
  36.  
  37.  
  38. input = cv2.imread(image)
  39.  
  40. _, th = cv2.threshold(input, 130, 160, cv2.THRESH_TOZERO)
  41.  
  42. cv2.imwrite("result.png", th)
  43.  
  44. image = "result.png"
  45. img = cv2.imread(image)
  46.  
  47. lower = np.array([0, 1, 1])
  48. upper = np.array([0, 255, 255])
  49. mask = cv2.inRange(img, lower, upper)
  50. roads = cv2.bitwise_and(img, img, mask=mask)
  51.  
  52. height = int(np.size(img, 0))
  53. width = int(np.size(img, 1))
  54.  
  55. for h in range(1, 500):
  56. for w in range(1, 500):
  57. color = str(roads[w, h])
  58. if color != "[0, 145, 153]":
  59. pass
  60. else:
  61. roads[h, w] = [0, 0, 255]
  62.  
  63. cv2.imwrite("roads.png", roads)
  64.  
  65. mask = Image.open("roads.png")
  66. mask1 = Image.open("th.png")
  67.  
  68. for i in range (1, 6523):
  69. for j in range (1, 7736):
  70. if mask1.getpixel((i,j))[0] != 0 or mask1.getpixel((i,j))[1] != 0 or \
  71. mask1.getpixel((i,j))[2] != 0:
  72. mask1.putpixel((i, j), (255, 255, 255))
  73. if (mask.getpixel((i, j))[0] != 0) or (mask.getpixel((i, j))[1] != 0) or (mask.getpixel((i, j))[2] != 0):
  74. mask1.putpixel((i, j), (0, 0, 0))
  75. mask1.save("dengi.jpg", "JPEG")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement