Is there a way using OpenCV and Python to perform road-segmentation, filling road with certain color

Summarizing The Problem

Using Python and cv2, I am in pursuit of reading “image.jpg” in, and identifying the road, setting the road to a color, and everything else to black. I would export it to an output jpg file.

My Next Attempts:

I will look deeper into gaussian blurs, luminance, and thresholds.

What I have Tried:

This simply just converts the image to greyscale, does canny edge detection, and draws lines over the solution of which are long enough. I would prefer my solution fills a color based off where the road is, without lines.

import cv2

# read image
img = cv2.imread("image.jpg")

# convert image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# apply Canny edge detection
edges = cv2.Canny(gray, 50, 150)

# apply HoughLinesP to detect lines in the image
lines = cv2.HoughLinesP(edges, 1, 3.1415/180, 50, minLineLength=200, maxLineGap=10) # 50

# iterate over the lines and draw them on the image
for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

# save the output image
cv2.imwrite("output.jpg", img)

Let me know if you know how to make it possible to color the road a specific color, and everything else another color. Thanks!

forget all that.

you need semantic segmentation. that’s an AI task.