python - 使用opencv检测矩形

标签 python opencv image-processing computer-vision polygon

我目前正在使用 opencv 尝试检测一组标志中每个单独 parking 标志周围的矩形。使用“findContours”和“approxPolyDP”让我接近了,但我想将轮廓合并到一个矩形中。使用“boundingRect”无效,因为形状中存在中断。

如果您对如何解决这个问题有任何建议,请告诉我。

这是我的代码:

image = cv2.imread(sign_directory)
# find all the 'red' shapes in the image
lower = np.array([0, 0, 110])
upper = np.array([100, 100, 250])
shapeMask = cv2.inRange(image, lower, upper)
# find the contours in the mask
(img, cnts, _) = cv2.findContours(shapeMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)

cv2.imshow("Image", image)
cv2.waitKey(0)

This is the image it produces

I want to get something like this so that I can crop it

This is the original

最佳答案

与其将每个轮廓传递给 cv2.boundingRect(),不如将它们全部合并到一个数组中,然后传递给该函数,然后得到需要裁剪的边界框。但在此之前,您需要移除离群值/噪声,即与其他轮廓相距太远的轮廓。

关于python - 使用opencv检测矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49457893/

相关文章:

python - 阻止 pydoc 运行我的 Python 程序

python - 返回数据框的按钮

python - 将 pyparsing 结果与节点链表相关联的模式

python-3.x - 如何实时检测对象并自动跟踪它,而不是用户必须在要跟踪的对象周围绘制边界框?

visual-c++ - 我们如何消除光照噪声?

algorithm - 如何生成与现有边界框至少按给定比例重叠的边界框?

python - 根据 Pandas 中现有列的功能创建新列的动态方法

c# - Emgucv 有 BackgroundSubtractorMOG 和 BackgroundSubtractorMOG2 吗?

android - 如何检测墙壁的长边以准备 mask 和重新着色

java - ImageJ:在 JAVA 中使用 run() 执行命令