c++ - 计算二维平面中黑白形状的大小

标签 c++ algorithm opencv size

我想计算图片中任意形状的黑色像素的数量。可能有多个对象,如底部图片所示。

我怀疑这个问题可以用动态规划来解决,即逐行遍历像素并添加黑色像素。我只是不知道如何正确合并两个部分的大小。

我很确定有算法可以解决我的问题,但我显然使用了错误的搜索词。

能否请您为我提供一个好的(快速)算法,如果该算法是用 c++ 编写的并且与 OpenCV 库中的 Mat 兼容,则加分。 ;)

Example Image

这张(缩放的)图片的结果类似于:左上角的对象为 15,大 Blob 为 60,...

最佳答案

我想我找到了解决方案(显然欢迎更好的解决方案!):

将大小计算集成到 Connected Component Algorithm 中.

在连通分量算法中,我们生成一个新图像,其中有标签(数字)而不是黑色像素。一个区域的所有像素都具有相同的标签。

CC-Algo 的新功能是一个表,其中存储了每个标签的像素总量。这样我就知道每个连接的组件的正确大小。

Process the image from left to right, top to bottom:
1.) If the next pixel to process is white:
    do nothing
2.) If the next pixel to process is black:
    i.) If only one of its neighbors (top or left) is black, copy its label and +1 in the size table for that label.
    ii.) If both are black and have the same label, copy it and +1 in the size table for that label.
    iii.) If they have different labels Copy the label from the left. Update the equivalence table and +1 in the size table for the left label.
iv.) Otherwise, assign a new label and update the size table with that label and value 1.

• Re-label with the smallest of equivalent labels and update size table accordingly

关于c++ - 计算二维平面中黑白形状的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21230301/

相关文章:

android - 如何在Android模拟器上安装openCV

c++ - LB_SETHORIZONTALEXTENT 和 SetHorizo​​ntalExtent 的区别

c++ - 使用c++/WinRT Direct3D UWP Game DR模板Direct X11的错误消息

计算 nCr 的 Java 程序抛出算术异常 "Divide by zero"

opencv - 均值漂移跟踪如何工作? (使用直方图)

c++ - 使用 Qt 设计 OpenCV GUI

c++ - 发布版本中的程序崩溃

c++ - Xed2-Intel64 库 - 发出 MOV 操作码

python - 列表在合并排序的递归循环中创建后返回无类型

performance - 这个 Haskell 合并代码的运行时间是多少