OpenCV find contours based on the pixel value

Viewed 407

I have an image as shown and using OpenCV I want to find the contours based on the color of each of the contours. The pixel values in this image are 0,1 and 2.

Image

What I am looking for is to get 2 yellow contours, 1 big green contour and 1 small green contour inside the second yellow contour on right

Opencv gives me just two contours here. What is the best way to achieve it?

My code so far:

import cv2 as cv
import numpy as np

img = cv.imread("seg_img.png",cv.IMREAD_UNCHANGED)

# find contour
contours, _ = cv.findContours(image =img, mode = cv.RETR_TREE,method = cv.CHAIN_APPROX_SIMPLE)

# loop through the contours and find area of each contour
for i,cnt in enumerate(contours):
   print(cv.contourArea(cnt))

Pixel values

np.unique(img)
2
0 Answers
Related