Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ OpenCV use InRange to segment HSV colors

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "C++ OpenCV how to use InRange to segment HSV color", the explanation content in this article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "C++ OpenCV how to use InRange to segment HSV color"!

API function

void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)

Explanation in official documentation: Check if an array element is between two other array element values. The array here is usually the matrix Mat or vector. Note that the dst output from this function is a binarized image.

Use Example 1: For Single Channel Images

dst(I) = lowerb(I)0 ≤ src(I)0

< upperb(I)0 即,如果一幅灰度图像的某个像素的灰度值在指定的高、低阈值范围之内,则在dst图像中令该像素值为255,否则令其为0,这样就生成了一幅二值化的输出图像。 使用示例2:针对三通道图像 dst(I) = lowerb(I)0 ≤ src(I)0 < upperb(I)0 ∧ lowerb(I)1 ≤ src(I)1 < upperb(I)1 ∧lowerb(I)2 ≤ src(I)2 < upperb(I)2 即,每个通道的像素值都必须在规定的阈值范围内! 代码演示 我们再新建一个项目名为opencv--video2,按照配置属性(VS2017配置OpenCV通用属性),然后在源文件写入#include和main方法

I recorded a test6.mp4 video in advance here, we run this program here to see the original video.

From the video we can see that I took a blue massager and shook it back and forth in the camera.

Next, we will extract the blue color for segmentation through the InRange function. In the HSV color table in the article, we can see that the H range of blue is between 100-124, the S range is between 43-255, and the V range is between 46-255, as shown in the following figure:

So we define these two ranges in our code first

Then we need to convert the original image to HSV image when the video is played, and then display it after color segmentation through InRange.

The effect after running is the video at the beginning of the article. Below is a screenshot from the video.

In the video above, we can see that the blue ones have been acquired. Let's talk about the key points:

Images must first be converted to HSV images via cvtColor

The three values defined in Scalar are set with parameters H,S, and V

Thank you for reading, the above is "C++ OpenCV how to use InRange to split HSV color" content, after learning this article, I believe everyone on C++ OpenCV how to use InRange to split HSV color this problem has a deeper understanding, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report