使用open cv来检测rect

时间:2018-03-23 20:32:37

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

我目前正在使用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

1 个答案:

答案 0 :(得分:0)

Instead of passing each contour to cv2.boundingRect() you want to merge them all in a single array and then pass to that function, then you get the bounding box you need to crop. But before doing that you need to remove outliers/noise, the contours that are too far from the others.