本文介绍了您如何在OpenCV上通过HSL保持较低的阈值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个项目正在进行中,需要进行白色检测,经过一番研究,我决定将隐蔽的RGB图像用于HSL图像,并脱粒保持亮度以获得白色,我与openCV一起使用,所以想知道是否存在是一种方法.在此处输入图片描述

There is a project that im working on which required the color white detection, after some research i decided to use covert RGB image to HSL image and thresh hold the lightness to get the color white, im working with openCV so wonder if there is a way to do it.enter image description here

推荐答案

您可以通过4个简单的步骤来做到这一点:

You can do it with 4 easy steps:

转换HLS

img = cv2.imread("HLS.png")
imgHLS = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)

获取L频道

Lchannel = imgHLS[:,:,1]

创建蒙版

#change 250 to lower numbers to include more values as "white"
mask = cv2.inRange(Lchannel, 250, 255)

将遮罩应用于原始图像

res = cv2.bitwise_and(img,img, mask= mask)

这也取决于您是什么白色,您可以更改值:)我在L通道中使用了inRange,但是您可以保存一步并执行

This also depends on what is white for you, and you may change the values :) I used inRange in the L channel but you can save one step and do

mask = cv2.inRange(imgHLS, np.array([0,250,0]), np.array([255,255,255]))

代替行:

Lchannel = imgHLS[:,:,1]
mask = cv2.inRange(Lchannel, 250, 255)

它更短,但是我首先用另一种方法使它更明确并显示我在做什么.

It is shorter, but I did it the other way first to make it more explicit and to show what I was doing.

图片:

结果:

结果看起来几乎就像是蒙版(几乎是二进制的),但是根据您的下限(我选择了250),您甚至可能会得到几乎是白色的颜色.

The result looks almost as the mask (almost binary), but depending on your lowerbound (I chose 250) you may get even some almost white colors.

这篇关于您如何在OpenCV上通过HSL保持较低的阈值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:44