如何从HFS分割图像中提取轮廓 [英] How to extract contours from HFS segmented image

查看:44
本文介绍了如何从HFS分割图像中提取轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从HFS模型输出中获取轮廓?我正在尝试检测地板.任何帮助将不胜感激.

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

推荐答案

由于地板具有特定的颜色范围,我们可以使用 cv2.inRange()设置颜色阈值.我们将图像转换为HSV格式,然后使用上下阈值生成二进制分割的蒙版

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()

这篇关于如何从HFS分割图像中提取轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆