【发布时间】:2020-02-22 22:40:40
【问题描述】:
我一直在使用 OpenCV 来检测方形障碍物。到目前为止,这是我在应用过滤器和 canny 后得到的图像。
我试图识别的障碍是水平的,三个垂直的矩形是地板上的指导线。我的目标是只保留水平矩形,将其与其他矩形分开,但在应用 find Contours 后我只得到我是一个包含所有形状的对象。这是我一直在使用的代码,以便仅按其面积来确定最大的矩形:
# find the biggest countour (c) by the area
if contours != 0:
if not contours:
print("Empty")
else:
bigone = max(contours, key=cv2.contourArea) if max else None
area = cv2.contourArea(bigone)
if area > 10000:
x, y, w, h = cv2.boundingRect(bigone)
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv2.putText(img, "Obstacle", (x+w/2, y-20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
pts = np.array(
[[[x, y], [x+w, y], [x+w, y+h], [x, y+h]]], dtype=np.int32)
cv2.fillPoly(mask, pts, (255, 255, 255))
#values = img[np.where((mask == (255, 255, 255)).all(axis=2))]
res = cv2.bitwise_and(img, mask) # View only the obstacle
obs_area = w*h
print(obs_area)
if obs_area <= 168000:
command_publisher.publish("GO")
cv2.putText(
img, "GO", (380, 400), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 255), 1)
else:
command_publisher.publish("STOP")
cv2.putText(img, "STOP", (380, 400),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 255), 1)
# show the output image
cv2.imshow("Image", img)
cv2.waitKey(1)
这是我得到的结果:
有没有办法通过某种过滤器或算法将我的障碍物与地板上的线条分开?
这是一个可以使用的示例图像:
【问题讨论】: