Skip to content Skip to sidebar Skip to footer

How To Extract Contours From Hfs Segmented Image

How to grab contours from HFS model output? I'm trying to detect floor. Any help would be appreciated.

Solution 1:

Since the floor has a specific color range, we can color threshold using cv2.inRange(). We convert the image to HSV format then use a lower and upper threshold to generate a binary segmented mask

lower = np.array([0, 31, 182])
upper = np.array([57, 75, 209])

To find the floor contour, we can just find contours on the mask image. Here's the result with the floor highlighted in green

import numpy as np
import cv2

# Color threshold
image = cv2.imread('1.jpg')
original = image.copy()
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 31, 182])
upper = np.array([57, 75, 209])
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(original,original,mask=mask)

# Find floor contour on mask
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(original,[c], -1, (36,255,12), 2)

cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.imshow('original', original)
cv2.waitKey()

Post a Comment for "How To Extract Contours From Hfs Segmented Image"