如何定义阈值以仅检测图像中的绿色对象:Opencv [英] How to define a threshold value to detect only green colour objects in an image :Opencv

查看:1861
本文介绍了如何定义阈值以仅检测图像中的绿色对象:Opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想检测自然环境中捕获的图像中的绿色物体。如何定义它?因为在这里我要通过阈值让我们说'x',通过使用这个x我想只获得一种颜色的绿色对象(白色)其他必须出现在另一种颜色(黑色)
请指导我这样做。提前谢谢。

I just want to detect only green objects from an image which captured in natural environment.How to define it? Because in here I want to pass the threshold value let's say 'x', by using this x I want to get only green colour objects in to one colour(white) others are must appear in another colour(black) Please guide me to do this. thanks in advance.

推荐答案

更新

我制作了 HSV 色彩映射表。使用此地图比以前查找颜色范围更简单准确

I make a HSV colormap. It's more easy and accurate to find the color range using this map than before.

也许我应该更改使用(40,40,40)〜(70,255,255)在hsv 中查找绿色

And maybe I should change use (40, 40,40) ~ (70, 255,255) in hsv to find the green.

原始答案


  1. 转换为 HSV 色彩空间,

  2. 使用 cv2.inRange(hsv,hsv_lower,hsv_higher)获取绿色遮罩。

  1. Convert to HSV color-space,
  2. Use cv2.inRange(hsv, hsv_lower, hsv_higher) to get the green mask.

我们使用范围(以hsv为单位)(36,0,0)〜(86,255,255)这个向日葵

源图像:

蒙面绿色区域:

更多步骤:

核心源代码:

import cv2
import numpy as np

## Read
img = cv2.imread("sunflower.jpg")

## convert to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

## mask of green (36,0,0) ~ (70, 255,255)
mask = cv2.inRange(hsv, (36, 0, 0), (70, 255,255))

## slice the green
imask = mask>0
green = np.zeros_like(img, np.uint8)
green[imask] = img[imask]

## save 
cv2.imwrite("green.png", green)

这篇关于如何定义阈值以仅检测图像中的绿色对象:Opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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