c++ - 求从像素到边缘的距离

标签 c++ opencv image-processing distance edge-detection

我正在尝试编写一种算法来查找图像中每个像素到黑色边缘的最小距离。下面是一个示例图像:

edges of face

这是目前的算法:

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/

相关文章:

python - 如何修复 "PATH"问题?

java - Android中如何查找匹配的关键点所属的 "train"图像的索引

visual-studio - OpenCV 调试/步入问题

php - 使用 Python/OpenCV 检测空心圆

OpenCV OutputArray 堆损坏?

image - 去除历史文档中的噪声和污点以进行 OCR 识别

c++ - std::merge 在 C++ 中如何工作?

c++ protobuf : how to iterate through fields of message?

c++ - 显式( bool )的用例是什么

c++ - 接受一个或两个参数的模板函数