我一直在使用OpenCV来检测方形障碍物。到目前为止,这是我应用过滤器和canny后得到的图像。
我试图确定的障碍是水平的,三个垂直的矩形是地板上的引导线。我的目标是只保留水平矩形,将其与其他矩形分开,但在应用“查找轮廓”后,我只得到一个包含所有形状的对象。这是我一直在使用的代码,以便按面积仅为最大的矩形添加鳍:
# 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)
这就是我得到的结果:
有没有办法用某种过滤器或算法将我的障碍物从地板上的线条中分离出来
以下是要使用的示例图像:
在您的图像中,问题似乎是白色矩形。我的方法是检查每一行,如果这一行包含许多接近白色的像素(255255),那么就把这一行变成黑色
这是我的密码:
消除白色矩形:
结果:
下面是使用Python/OpenCV实现这一点的一种方法
输入:
饱和通道图像:
阈值图像:
形态学清理图像:
输入时的轮廓:
相关问题 更多 >
编程相关推荐