python - 获取图像中矩形的数量

标签 python c++ opencv image-processing

我有一张如下图所示的图像,我想确定图像中矩形的数量。如果他们被填满,我知道该怎么做。

contours = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if imutils.is_cv2() else contours[1]
print len(contours)

但是如果矩形是空的,这就不起作用了。

我也不知道如何填充图像中的矩形。我知道如何填充使用 OpenCV 绘制的轮廓,但我不知道如何填充图像中已经存在的空矩形。

image with three rectangles

最佳答案

假设您已经尝试过形状检测器、线检测器等但没有成功,这是解决此问题的另一种方法。

如果这是灰度 PNG 图像,您可以使用按颜色分割来实现此目的。 我会这样处理:

count = 0
For each pixel in the image:
    if color(pixel) == white /*255*/
        count++
        floodfill using this pixel as a seed pixel and target color as count

no_of_rectangles = count - 1 /* subtract 1 since the background will be colored too*/

这假设矩形有连续的线,否则填充会泄漏到其他矩形。

关于python - 获取图像中矩形的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44657430/

相关文章:

c++ - DsGetDomainControllerInfo 返回 "Pointer to a pointer variable that receives an array"?我不明白

matlab 'bridge' 'shrink'和 'holes'在openCV中的等价形态学运算

python - 优化旋转和填充

python - 用 uuid 替换整数 id 字段

python - python 子进程模块古怪吗?

python - 如何捕获正在运行的程序的屏幕输出?

c++ - 为什么我在头文件中定义 `const int`时没有重复定义错误?

c++ - 施放 ssize_t 或 size_t

opencv - 通过能量驱动采样(SEEDS)提取的超像素

matlab - 使用Matlab修改文本文件