[OpenCV Getting Started] Color detection/slider creation


My personal blog: Mouren·Blog
WeChat public account: Mouren's sack
CSDN: Cao Mouren



color detection

Convert HSV model

Color detection usually needs to be detected from HSV images, so first convert the original image into an HSV model. (Use the cvtColor function, refer to my article for details: [OpenCV Introduction] Some basic image processing )

inRange function

Function: Binarize the image, set the pixel value within the threshold range [lowerb, upperb] to white (255), and set the pixel value not within the threshold range to black (0).

Function definition:

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

Parameter explanation:

  • src: input image
  • lowerb: array or scalar containing the lower bound
  • upperb: Array or scalar containing the upper bound
  • dst: Output binarized image with the same size as the input image

In order to accurately detect the expected color, we must strictly select the values ​​​​of the three parameters H, S, and V of the lower limit lowerb and the upper limit upperb. Therefore, in order to find an accurate threshold, we usually use the createTrackbar function to generate a slider and change the parameters by dragging the slider after running the program. Let me introduce the createTrackbar function.

createTrackbar function----slider

Function: Create a slider, and change some parameters by dragging the slider after running the program.

Function definition:

int createTrackbar(
	const String& trackbarname, 
	const String& winname,
    int* value, 
    int count,
    TrackbarCallback onChange = 0,
    void* userdata = 0
    );

Parameter explanation:

  • trackbarname: the name of the slider
  • winname: the name of the window where the slider is located
  • value: an integer pointer, used to store the address of the variable changed by the slider
  • count: the maximum value of value
  • onChange: When you want to change some functions by dragging the slider, write your own defined functions here. The callback function is automatically called when the slider is dragged, and then the image is processed accordingly. (Default 0) Definition of TrackbarCallback class:
/** @brief Callback function for Trackbar see cv::createTrackbar
@param pos current position of the specified trackbar.
@param userdata The optional parameter.
 */
typedef void (*TrackbarCallback)(int pos, void* userdata);
  • userdata: user-defined data, use it to avoid the use of global variables

example

source code:

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
#ifdef _DEBUG
#pragma comment(lib,"opencv_world453d.lib")
#else
#pragma comment(lib,"opencv_world453.lib")
#endif // _DEBUG


int main()
{
    
    
    //控制取色区域的参数
    //先大概赋一些值,运行后用滑块改变各个参数选取要提取的颜色
    int hmin = 100, smin = 100, vmin = 100;
    int hmax = 200, smax = 200, vmax = 200;

    Mat imgHSV,imgDstColor;
    string path = "D:\\My Bags\\图片\\Test.jpg";
    Mat imgIn = imread(path);
    cvtColor(imgIn, imgHSV, COLOR_BGR2HSV);//转换HSV模型
    //创建一个窗口用于放置调整参数的滑块
    //命名“取色控制台”,WINDOW_NORMAL指可调整窗口大小
    namedWindow("取色控制台", WINDOW_NORMAL);
    //创建六个控制取色参数的滑块
    createTrackbar("色调下限", "取色控制台", &hmin, 180);
    createTrackbar("色调上限", "取色控制台", &hmax, 180);
    createTrackbar("饱和度下限", "取色控制台", &smin, 255);
    createTrackbar("饱和度上限", "取色控制台", &smax, 255);
    createTrackbar("明度下限", "取色控制台", &vmin, 255);
    createTrackbar("明度上限", "取色控制台", &vmax, 255);

    while (true)
    {
    
    
        Scalar lower(hmin, smin, vmin);//下限
        Scalar upper(hmax, smax, vmax);//上限
        inRange(imgHSV, lower, upper, imgDstColor);
        imshow("原图", imgIn);
        imshow("HSV图", imgHSV);
        imshow("色彩取样图", imgDstColor);
        waitKey(1);//每隔1ms刷新一次
    }
    return 0;
}

operation result:

Supongo que te gusta

Origin blog.csdn.net/ZBC010/article/details/120678246
Recomendado
Clasificación