我有一张如下图所示的图像,我想确定图像中矩形的数量。如果他们被填满,我知道该怎么做。
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 绘制的轮廓,但我不知道如何填充图像中已经存在的空矩形。
最佳答案
假设您已经尝试过形状检测器、线检测器等但没有成功,这是解决此问题的另一种方法。
如果这是灰度 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/