Color Detection

In this chapter we are going to learn how to detect colors in images. So you can use this to create an object detector of a specific color as well. Now we are going to change BGR to HSV, let’s create this image. So we will write here mat and let’s write our imageHSV. Then we are going to print it out. So let’s to use the inrange function to actually collect our what do you color. So we are going to write inRange and we will write the imageHSV. So we will write that this is our image and then we will define a lower limit and an upper limit now usually you can define exactly what color do you want. But in reality because of the lighting and different shawows the color will not be exactly one value. So it will be a range of values. So it is always good to use that range. So we will have a range with lower and upper values and then the output will be our mask. So we will call it the output image will be our mask. So we need to define the mask so this will be our new image. And then we also need to define the lower and upper limit. So we can define here scalar. Because it is just three values we can define lower as h minimum. So this is the hue minimum and then we have saturation minimum and then we have the value minimum. Same way we are going to have max so let’s copy that, and right here upper and instead of minimum we will write max. now these values you can hard code them and you can write them directly here the other way that what I prefer is to write here. Integer h minimum is equals to let’s say zero. Now this color I have already picked out and I know what exactly this color is so I’m writing those values over here but I will later show you how to find a different color as well so let’s put this here and then we can write s minimum is 110 then v minimum is 153. So max is equals to 19, then s max is equals to 240 and v max is equals to 255. So now if we print out or if we display our image which is basically the mask.

Next, the question is how did I find these values and a lot of times you might want to use your own values. So how can you do that now the thing is because there are six values involoed changing all of these every time and running it again will be very painful and it will take a long long time. So what we can do is we can create track bars which will allow us to change values in real time, and then we can find the minimum and the maximum values. So how can we declare this track bar. So opencv has a built -in functionality. For this and whenever we are using trackbar, we will have to use a while loop, so we are going to create a while. So we do not need to convert or imports it again and again. So we will create a while after that so we will write here while true and then we are going to close this here and we will change this to 1. Then let’s to create trackbars.

通过轨迹栏来确定颜色的HSV值。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值