如何使用opencv python在图像上找到道路?

时间:2018-08-31 09:26:23

标签: python opencv image-processing computer-vision

我有一张航拍图片Aerial Image。 我需要使用OpenCV和Python在其上寻找道路。我在HSV空间中使用了分割来去除图片中不必要的部分,然后应用了MorphologyEx并最终得到了MorphologyEx Image。接下来我该怎么做才能在这张图片中找到正确的道路?

我的代码:

while(True):
    ret, frame = cap.read()
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    imgHsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)

    mask = cv.inRange(imgHsv, (21, 0, 0), (35, 225, 225))

    imask = mask > 0

    roads = np.zeros_like(frame, np.uint8)

    roads[imask] = frame[imask]

    #bluredRoadsMask = cv.medianBlur(roads, 7)
    grayRoads = cv.cvtColor(roads, cv.COLOR_BGR2GRAY)
    phi = 1
    theta = 1
    maxIntensity = 255.0

    newImage = (maxIntensity/phi) * (grayRoads/(maxIntensity/theta))**2
    newImage = np.array(newImage, dtype=np.uint8)

    kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (7, 7))
    # cross = cv.getStructuringElement(cv.MORPH_CROSS, (3, 3))
    morphologyImg = cv.dilate(newImage, kernel, iterations=2)
    _, roadsThresh = cv.threshold(morphologyImg, 30, 225, cv.THRESH_BINARY)
    gaus_blur = cv.GaussianBlur(roadsThresh, (5, 5), 3, 3)
    erodeImg = cv.erode(gaus_blur, kernel, iterations=1)
    morphExImage = cv.morphologyEx(erodeImg, cv.MORPH_DILATE, kernel)


    roadsEdges = cv.Canny(morphExImage, 0, 0, apertureSize= 3)


    lines = cv.HoughLinesP(roadsEdges, 0.05, np.pi / 180, 0, 40, 1)
    # for x in range(0, len(lines)):
    #     for x1, y1, x2, y2 in lines[x]:
    #         cv.line(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)


    # cv.imshow("roads", linesImage)
    cv.imshow("HSV Segmentation", morphExImage)
    # cv.imshow("grayFrame", gray)
    # cv.imshow("canny Edges", threshold)
    # cv.imshow("blur", bluredImage)
    cv.imshow("frame", frame)
    if cv.waitKey(1) & 0xFF == ord('q'):
         break

感谢您的回答!

upd:使用霍夫变换Hough Transform

0 个答案:

没有答案