[OpenCV introduction] color detection / slider creation

My personal blog: Mou Ren · Blog WeChat official account: gunny's sack CSDN: Cao mouren Color detection ...
Convert HSV model
inRange function

My personal blog: Mou Ren · Blog
WeChat official account: gunny's sack
CSDN: Cao mouren

Color detection

Convert HSV model

Color detection is usually detected from HSV image, so first convert the original image into HSV model. (use cvtColor function. For details, please refer to my article: [OpenCV introduction] some basic image processing)

inRange function

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

Function definition:

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

Parameter interpretation:

  • src: input image
  • lowerb: an array or scalar containing the lower boundary
  • upperb: an array or scalar containing the upper boundary
  • dst: output a binary 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 H, S and V parameters of the lower limit lowerb and the upper limit upperb. Therefore, in order to find the accurate threshold, we usually generate the slider with the createTrackbar function, and change the parameters by dragging the slider after running the program. Let me introduce the createTrackbar function.

createTrackbar function -- slider

The function creates a slider and changes 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 interpretation:

  • trackbarname: the name of the slider
  • winname: the name of the window where the slider is located
  • value: integer pointer, used to store the address of the variable changed by the slider
  • Maximum value of count: value
  • onChange: when you want to change some functions by dragging the slider, write the self-defined function here. When you drag the slider, the callback function will be called automatically, and then the image will be 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, which can avoid using 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() { //Parameters controlling the color pickup area //First assign some values roughly, and then use the slider to change various parameters to select the color to be extracted int hmin = 100, smin = 100, vmin = 100; int hmax = 200, smax = 200, vmax = 200; Mat imgHSV,imgDstColor; string path = "D:\\My Bags\\picture\\Test.jpg"; Mat imgIn = imread(path); cvtColor(imgIn, imgHSV, COLOR_BGR2HSV);//Convert HSV model //Create a window to place a slider for adjusting parameters //Name the "color finder console", and WINDOW_NORMAL refers to the adjustable window size namedWindow("Color finder console", WINDOW_NORMAL); //Create six sliders that control the take color parameters createTrackbar("Lower tone limit", "Color finder console", &hmin, 180); createTrackbar("Upper hue limit", "Color finder console", &hmax, 180); createTrackbar("Lower saturation limit", "Color finder console", &smin, 255); createTrackbar("Upper saturation limit", "Color finder console", &smax, 255); createTrackbar("Lower lightness limit", "Color finder console", &vmin, 255); createTrackbar("Upper lightness limit", "Color finder console", &vmax, 255); while (true) { Scalar lower(hmin, smin, vmin);//lower limit Scalar upper(hmax, smax, vmax);//upper limit inRange(imgHSV, lower, upper, imgDstColor); imshow("Original drawing", imgIn); imshow("HSV chart", imgHSV); imshow("Color sampling diagram", imgDstColor); waitKey(1);//Refresh every 1ms } return 0; }

Operation results:

9 October 2021, 08:59 | Views: 9566

Add new comment

For adding a comment, please log in
or create account

0 comments