OpenCV Pyth--过滤矩形以找到微型车的道路中心线。

问题描述 投票:0回答:1

我正在尝试过滤矩形,为微型汽车找到道路中心线,并帮助他们 "看 "到道路。我的总体目标是写一个人工智能,让人类可以和人工智能控制的汽车比赛。正如你所看到的,这个项目还处于早期阶段。我不太确定cv2.minAreaRect()是否是合适的函数。我也看了cv2.boundingRect()。我现在的想法是测量矩形的大小,然后过滤掉错误的矩形,这样就只剩下道路的中心线了。但我不确定这是否是最好的解决方案。我试着把第三张图中的道路中心线用红色的方式给你可视化。道路的中心线由长长的矩形痕迹显示,看起来是一样的。也许你们有什么办法可以帮助我。我会把我到目前为止的代码和我用代码制作的图片贴出来。我也会发一张图片,说明我对矩形的处理程度。谢谢你的帮助

图片中找到的矩形。image to find the rectangles in

在运行我的代码后:after running my code

我想保留的矩形rectangles i want to keep

import cv2
import numpy as np

# read image
image = cv2.imread('Carrera.png')
cv2.waitKey(0)

# set grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# show gray image
cv2.imshow('Gray', gray)
cv2.waitKey(0)

# change contrast/brightness
alpha = 50  # Contrast control (0-100)
beta = 0  # Brightness control (0-100)
adjusted = cv2.convertScaleAbs(gray, alpha=alpha, beta=beta)

# show high contrast image
cv2.imshow('Contrast', adjusted)
cv2.waitKey(0)

#  find canny edges
edged = cv2.Canny(adjusted, 30, 200)

# set blur
blur = cv2.blur(edged, (3, 3))

# find contours
contours, hierarchy = cv2.findContours(blur, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

# show canny edges
cv2.imshow('Canny Edges', blur)
cv2.waitKey(0)

# number of contours found
print("Number contours = " + str(len(contours)))

# paint rectangles
for cnt in contours:
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(image, [box], -1, (0, 255, 0), 1)

# show final image
cv2.imshow('Rectangles', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
python opencv filter rectangles
1个回答
0
投票

你想通过使用矩形的长度来消除不必要的矩形。你已经有了这些数据,通过 minAreaRect().

它的输出是有序的,这是持 rect 在你的代码中。

center    The rectangle mass center.
size      Width and height of the rectangle.
angle     The rotation angle in a clockwise direction.

下面是输出结果,以及如何使用 rect 以消除不必要的矩形。

输出:

enter image description here

编码待补。

# paint rectangles
for cnt in contours:
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    if(rect[1][0]<110 and rect[1][0]>40 and rect[1][1]<70 and rect[1][1]>40):
       cv2.drawContours(image, [box], -1, (0, 255, 0), 1)
© www.soinside.com 2019 - 2024. All rights reserved.