Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image Segmentation in openCV

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

like image 295
Shreyas Pimpalgaonkar Avatar asked Sep 05 '26 20:09

Shreyas Pimpalgaonkar


1 Answers

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..

like image 56
Ramesh-X Avatar answered Sep 07 '26 10:09

Ramesh-X



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!