Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def edgelinking(img_original):
- plt.subplot(1, 2, 1)
- plt.title("Original")
- MAX_X = len(img_original)
- MAX_Y = len(img_original[0])
- im2 = img_original.copy()
- im2[:, :, 0] = img_original[:, :, 2]
- im2[:, :, 2] = img_original[:, :, 0]
- plt.imshow(im2)
- plt.xticks([])
- plt.yticks([])
- gray = cv2.cvtColor(img_original, cv2.COLOR_BGR2GRAY)
- ret, thresh = cv2.threshold(gray, 115, 255, 0)
- contournImg = zero_image(gray)
- img = thresh
- orderStd = [(-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1), (-1,-1)]
- lastValue = img[0][0]
- lastC = 0
- for x, i in enumerate(thresh):
- for y, pixel in enumerate(i):
- if (x < MAX_X - 1 and x > 0) and (y < MAX_Y - 1 and y > 0):
- if thresh[x][y] == lastValue:
- continue
- if contournImg[x][y]:
- continue
- lastValue = img[x][y]
- contournImg[x][y] = 255
- while True:
- foundNeighboor = False
- for c in orderStd[0:lastC] + orderStd[lastC:len(orderStd)]:
- if img[x + c[0]][y + c[1]] != lastValue and not contournImg[x + c[0]][y + c[1]]:
- lastValue = img[x + c[0]][y + c[1]]
- lastC = orderStd.index(c) - 1
- if lastC < 0:
- lastC = 7
- foundNeighboor = True
- contournImg[x + c[0]][y + c[1]] = 255
- break
- if not foundNeighboor:
- break
- plt.subplot(1, 2, 2)
- plt.title("Contornada")
- plt.imshow(contournImg, cmap="gray")
- plt.xticks([])
- plt.yticks([])
- plt.show()
- return contournImg
Add Comment
Please, Sign In to add comment