Guest User

Untitled

a guest
Sep 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. def edgelinking(img_original):
  2. plt.subplot(1, 2, 1)
  3. plt.title("Original")
  4. MAX_X = len(img_original)
  5. MAX_Y = len(img_original[0])
  6.  
  7. im2 = img_original.copy()
  8. im2[:, :, 0] = img_original[:, :, 2]
  9. im2[:, :, 2] = img_original[:, :, 0]
  10. plt.imshow(im2)
  11. plt.xticks([])
  12. plt.yticks([])
  13.  
  14. gray = cv2.cvtColor(img_original, cv2.COLOR_BGR2GRAY)
  15. ret, thresh = cv2.threshold(gray, 115, 255, 0)
  16. contournImg = zero_image(gray)
  17.  
  18. img = thresh
  19. orderStd = [(-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1), (-1,-1)]
  20. lastValue = img[0][0]
  21. lastC = 0
  22. for x, i in enumerate(thresh):
  23. for y, pixel in enumerate(i):
  24. if (x < MAX_X - 1 and x > 0) and (y < MAX_Y - 1 and y > 0):
  25. if thresh[x][y] == lastValue:
  26. continue
  27. if contournImg[x][y]:
  28. continue
  29. lastValue = img[x][y]
  30. contournImg[x][y] = 255
  31. while True:
  32. foundNeighboor = False
  33. for c in orderStd[0:lastC] + orderStd[lastC:len(orderStd)]:
  34. if img[x + c[0]][y + c[1]] != lastValue and not contournImg[x + c[0]][y + c[1]]:
  35. lastValue = img[x + c[0]][y + c[1]]
  36. lastC = orderStd.index(c) - 1
  37. if lastC < 0:
  38. lastC = 7
  39. foundNeighboor = True
  40. contournImg[x + c[0]][y + c[1]] = 255
  41. break
  42. if not foundNeighboor:
  43. break
  44.  
  45. plt.subplot(1, 2, 2)
  46. plt.title("Contornada")
  47. plt.imshow(contournImg, cmap="gray")
  48. plt.xticks([])
  49. plt.yticks([])
  50. plt.show()
  51.  
  52. return contournImg
Add Comment
Please, Sign In to add comment