Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

python - How to define a threshold value to detect only green colour objects in an image :Opencv


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update:

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

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

enter image description here


Original answer:

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

We use the range (in hsv): (36,0,0) ~ (86,255,255) for this sunflower.


The source image:

enter image description here

The masked green regions:

enter image description here

More steps:

enter image description here


The core source code:

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,25,25) ~ (86, 255,255)
# mask = cv2.inRange(hsv, (36, 25, 25), (86, 255,255))
mask = cv2.inRange(hsv, (36, 25, 25), (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)

Similar:

  1. Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...