我正在尝试编写一种算法来查找图像中每个像素到黑色边缘的最小距离。下面是一个示例图像:
这是目前的算法:
Starting from the pixel (R,C) I check every pixel around (R,C) that is d=1 pixels away from R,C. If I do not hit a black pixel, then I check every pixel around (R,C) that is d =2 pixels away from R,C ... and this carries on (with d increasing) until I find a black pixel and then I calculate the Euclidean Distance from R,C. Note that I am making sure that the pixels I check do not exceed the boundaries of the image).
但是,由于我对每个像素都执行此操作,因此算法非常慢。
有谁知道更快的方法吗?任何帮助将不胜感激。我使用 C++ 和 OpenCV 进行编码,因此任何使用这些的算法都是首选。
最佳答案
你想要做的是找到一个 distance transform的图像。您可以在函数distanceTransform
中找到OpenCV的实现。 。你会发现它相当快。
关于c++ - 求从像素到边缘的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18816839/