【发布时间】:2021-03-16 10:33:26
【问题描述】:
【问题讨论】:
-
你的意思是你想知道,对于每个单独的紫色像素,到最近的黑色像素的距离?
-
是的,你没看错。我将在精明的图像上执行此操作。
标签: python opencv image-processing euclidean-distance canny-operator
【问题讨论】:
标签: python opencv image-processing euclidean-distance canny-operator
考虑对其中一种线型的图片进行“距离变换”。然后,对于其他线型的任何点,立即查看距离。
或者将你的线变成多边形/折线。这极大地减少了数据,并将您的问题变成了几何问题。
【讨论】:
我认为这很接近但还没有检查太多:
#!/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、图像处理、距离变换、distancetransform、cv2.distancetransform、Python。
【讨论】:
cv2.distanceTransform()改成dst, labels = cv2.distanceTransformWithLabels(bw, cv2.DIST_L2, cv2.DIST_MASK_PRECISE, labelType=cv2.DIST_LABEL_PIXEL)
scipy.ndimage.morphology.distance_transform_edt()
edt, inds = ndimage.distance_transform_edt(bw, return_indices=True)