如何定义阈值以仅检测图像中的绿色对象:Opencv [英] How to define a threshold value to detect only green colour objects in an image :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
.
原始答案:
- 转换为
HSV
色彩空间, - 使用
cv2.inRange(hsv,hsv_lower,hsv_higher)
获取绿色遮罩。
- Convert to
HSV
color-space, - 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屋!