我目前正在使用 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)
最佳答案
与其将每个轮廓传递给 cv2.boundingRect()
,不如将它们全部合并到一个数组中,然后传递给该函数,然后得到需要裁剪的边界框。但在此之前,您需要移除离群值/噪声,即与其他轮廓相距太远的轮廓。
关于python - 使用opencv检测矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49457893/