簡體   English   中英

如何計算精巧圖像中的歐幾里得距離

[英]How to calculate the Euclidean distance in a canny image

我有一條黑線和一條紫線。 我想以最有效和最快的方式計算canny圖像中每個紫色像素點與最近的黑線之間的距離。 我該怎么做? 有沒有opencv-python function?

實際圖像

精明的形象

我認為這很接近但還沒有檢查太多:

#!/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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM