3

I have an image of a road.

I applied color thresholding to it and got a mask of yellow and white markers (cv2.inRange)

Then I applied got contours of specific area on the mask to remove the noise (cv2.findContours)

I have obtained good mask which has whites as lanes and black everywhere else.

However, I cannot get the three lanes into separate arrays - I tried watershed algorithm, it gives me the boundaries of the lanes, however doesn't separate them into different arrays.

My desired result is to have three separate arrays, each containing all the pixel numbers of each lane.

I have warped the image as well.

the below screenshot is the bitwise and of mask and original warped image.

enter image description here

1 Answers1

1

You can find contours and fill them and use as masks. To find contours, you can use cv2.findContours() function in OpenCV. You can find an example in OpenCV Docs.
As in the docs, you can get contours by,

_, contours, _ = cv2.findContours(your_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

The the variable contours will have a list of contours. In your case each lane will be added as a separate contours.

Then as described in this answer, you can create masks.

masks = []
for contour in contours:
    img = np.zeros( (height, width) ) 
    cv2.fillPoly(img, pts =[contours], color=(255,255,255))
    masks.append(img)

And also you can use cv2.drawContours function, set thickness=cv2.FILLED to create the masks.

Modification

First make sure all the black areas are (0, 0, 0) in rgb values. Then you can try values in here for the second argument and values in here for the third argument of findContours() function..

Ramesh-X
  • 4,853
  • 6
  • 46
  • 67
  • Oh right, had tried that as well, but missed it, I was getting a lot of contours (around 10-15 per image), is there any way I can filter them to get only the desired ones? – Shreyas Pimpalgaonkar Jun 20 '18 at 13:36
  • Thanks a lot, this gives me the desired result, however the lanes are scrambled. Is there a way that I can sort them from left to right? – Shreyas Pimpalgaonkar Jun 21 '18 at 08:20
  • Here a **contours** variable is a list of `contour`s and a `contour` is a list of coordinates. Maybe you can try to separate them by the coordinate values of the `contour`s. No other method came into my mind.. – Ramesh-X Jun 21 '18 at 08:33
  • @Ramesh-X you can help me in https://stackoverflow.com/questions/60978380/how-to-remove-the-background-of-the-image-of-interest-using-opencv-on-android – Carlos Diego Apr 30 '20 at 15:21