【问题标题】:using open cv to detect a rect使用opencv检测矩形
【发布时间】:2018-03-23 20:32:37
【问题描述】:

我目前正在使用 opencv 尝试检测一组标志中每个单独停车标志周围的矩形。使用“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

【问题讨论】:

  • 您希望外部矩形是唯一绘制的?
  • 是的,完全正确。我只是想为红色标志绘制外部矩形。我最终也会想要绿色标志的矩形,但我不想在这里这样做。
  • 能否也附上原图?
  • @ZdaR 刚刚上传了原图。
  • opencv scene text detection 在这里有用吗?我认为这是针对野生检测中的那种文本进行训练的 (python example)

标签: python opencv image-processing computer-vision polygon


【解决方案1】:

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

【讨论】:

  • 感谢您的提示!是的,我一直在研究合并轮廓。您对解决此问题的最佳方法有什么建议吗?
猜你喜欢
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多