[英]How to calculate the Euclidean distance in a canny image
我認為這很接近但還沒有檢查太多:
#!/usr/bin/env python3
import cv2
import numpy as np
# Load input image
im = cv2.imread('PBv6H.png')
# DEBUG Get list of all unique colours in image
# np.unique(im.reshape((-1,3)),axis=0)
# Find purple pixels
purplepixels = np.where(np.all(im==[164,73,163],axis=-1))
# Make black and white image with only the black pixels from original
bw = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
_, bw = cv2.threshold(bw,1,255,cv2.THRESH_BINARY)
# DEBUG cv2.imwrite('bw.png', bw)
# Now calculate the distance from every pixel to the nearest black one
# Every pixel in "dst" image has a brightness equal to its distance to nearest black pixel
dst = cv2.distanceTransform(bw, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
# Print out the distance to the nearest black pixel for each purple pixel
for y,x in zip(purplepixels[0], purplepixels[1]):
print(f'[{y},{x}]: {dst[y,x]}')
這是距離變換圖像 - 像素越亮,距離黑色像素越遠:
這是閾值化的黑白圖像:
關鍵詞:OpenCV,圖像處理,距離變換,距離變換,cv2.distancetransform,Python。
考慮對其中一種線型的圖片進行“距離變換”。 然后,對於其他線型的任何點,立即查看距離。
或者把你的線變成多邊形/折線。 這極大地減少了數據,並將您的問題變成了幾何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.