OPENCV the multi-channel image by sliding bar threshold

Source: Internet
Author: User
Tags types of functions

1. Threshold Value Segmentation

The threshold segmentation method is a region-based image segmentation technique. The basic principle is: By setting different feature thresholds, the pixel points of the image are divided into several classes. According to the information source based on the image thresholding algorithm, the Thresholding method can be divided into five categories: 1) based on the clustering method: In data clustering, the total data set is divided into sub-classes with similar attributes, for example, gray level clustering becomes two parts: foreground object part and background part. 2) Histogram-based method: The histogram of peaks, valleys and histogram of the smooth curve analysis. 3) Entropy-based method: The Entropy method divides the region into the background area and the foreground area, and the foreground area is usually the object part (in some thermal infrared images, the background part is the object). The method is implemented by minimizing an entropy function, which contains the retention information between the original image and the second-valued one. 4) based on spatial method: Using probability density function model, consider the similarity relation between pixels in global scope. 5) based on the adaptive method: The local method can not determine a single threshold, the adaptive threshold is dependent on the local image characteristics.

Here, we only introduce the threshold by combining the API function threshold in OPENCV. The threshold function is prototyped as follows:

doubledouble thresh,                 doubleint thresholdType)

Parameters:
First parameter: The address of the input grayscale image.
Second parameter: The address of the output image.
The third parameter: the size of the threshold when the threshold operation is performed.
Fourth parameter: Set the maximum grayscale value (this parameter is used in binary and inverse binary threshold operations).
Fifth parameter: The type of the threshold value.

The last parameter is a threshold type, which provides five types of functions ( images from OPENCV official website ).
1) binary thresholding, parameter value is 0.


It is well understood that the pixel value is greater than the threshold value set to 255, anyway set to 0;
2) Inverse binary thresholding


Similar to binary thresholding, except that the threshold value is set to 0 and vice versa is set to 255;
3) Truncation threshold value


pixels greater than this threshold in the image are set to this threshold value, which is less than the threshold value remains unchanged;
4) threshold value to 0


pixels greater than the threshold do not make any changes, the remaining gray values are all changed to 0;
5) Counter-threshold value to 0


Similar to thresholding, pixels greater than the threshold are set to 0 and the rest does not change.

In addition, the most widely used thresholding API function in OpenCV is the adaptivethreshold (Adaptive thresholding function), which references OpenCV documentation in detail.

2, Trackbar

OPENCV provides API function Createtrackbar, which allows us to easily interact with the program when setting parameters. The Creattrackbar function is prototyped as follows:

int  Createtrackbar (const  string  & Trackbarname, const  string  & Winname, int  * value , int  count, Trackbar Callback onchange=0 ,)  

The first argument, the const string& type of Trackbarname, represents the name of the track strip used to represent the track bar we created.
The second parameter, the const string& type of Winname, fills in the name of the window, indicating which window the track bar will be attached to, that is, a window name that corresponds to Namedwindow (), which is filled in when the window is created.
The third argument, the value of type int*, a pointer to an integral type that represents the position of the slider. And at the time of creation, the initial position of the slider is the current value of the variable.
The fourth parameter, the count of type int, represents the value of the maximum position that the slider can reach. PS: The value of the minimum position of the slider is always 0.
The fifth parameter, Trackbarcallback type of onchange, first note that he has a default value of 0. This is a pointer to the callback function that will be invoked each time the slider position changes. And the prototype of this function must be void Foo (int,void*), where the first parameter is the position of the track bar, the second parameter is the user data (see the sixth parameter below). If the callback is a null pointer, there is no call to the callback function, only the third parameter value changes.
The sixth parameter, void* type UserData, also has a default value of 0. This parameter is the data that the user passes to the callback function to handle the track bar event. If you use the third parameter, the value argument is a global variable, you can simply not take care of the UserData parameter.

The demo program is as follows:

#include <iostream>#include <cstring>#include "opencv2/core/core.hpp"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"using namespace STD;using namespaceCv//global variable definition and assignmentintThreshold_type =3;int ConstMax_type =4;//Threshold typeintThreshold_bvalue =0;//b Channel threshold settingintThreshold_gvalue =0;//g Channel threshold settingintThreshold_rvalue =0;//r Channel threshold settingint ConstMax_value =255;int ConstMax_binary_value =255; Mat SRC, DST;//Create an image vector vector<Mat>PlanesChar* Window_name ="Threshold Func";Char* Trackbar_type ="Trackbartype";//0:binary 1:binary Inverted 2:truncate                                   //3:to Zero 4:to Zero invertedChar* Trackbar_bvalue ="B_value";Char* Trackbar_gvalue ="G_value";Char* Trackbar_rvalue ="R_value";/// Custom Function declarationvoidThreshold_func (int,void* );intMain () {//Load a picturesrc = imread ("Test.jpg",1);//Convert a picture to a grayscale image  //cvtcolor (SRC, Src_gray, cv_rgb2gray);   //Split multi-channel image into several single-channel imagesSplit (SRC, planes);//Create a window to display picturesNamedwindow (Window_name, cv_window_autosize);//Create sliders to control threshold valuesCreatetrackbar (Trackbar_type, Window_name, &threshold_type, Max_type, Threshold_fun  c); Createtrackbar (Trackbar_bvalue, Window_name, &threshold_bvalue, Max_value, Threshol  D_FUNC); Createtrackbar (Trackbar_gvalue, Window_name, &threshold_gvalue, Max_value, Threshol  D_FUNC); Createtrackbar (Trackbar_rvalue, Window_name, &threshold_rvalue, Max_value, Threshol D_FUNC);//Initialize a custom threshold functionThreshold_func (0,0);//wait for the user to press. If it is ESC, exit the wait process.    while(true)  {intC c = Waitkey ( -);if( (Char) c = = -)      { Break; }   }}//Custom threshold functionvoidThreshold_func (int,void* ){/ * 0: Binary threshold 1: Inverse binary threshold 2: Truncation threshold 3:0 threshold 4: Anti 0 threshold * /Dst.create (Src.size (), Src.type ()); vector<Mat>Thredplanes;  Split (Dst,thredplanes); Threshold (planes[0], thredplanes[0], Threshold_bvalue, max_binary_value,threshold_type); Threshold (planes[1], thredplanes[1], Threshold_gvalue, max_binary_value,threshold_type); Threshold (planes[2], thredplanes[2], Threshold_rvalue, max_binary_value,threshold_type);//re-merge three single-channel images into one triple-channel imageMerge (THREDPLANES,DST);//Display DST imagesImshow (Window_name, DST);}

Operation Result:

Program Description:
1) First read a picture, if the picture color type is BGR three-channel type, then separated into three single-channel images.

vector<Mat> planes;      //创建图像向量,用来存放src分割后的单通道图像split(src, planes);      //分割原始图像为若干单通道图像,split函数原型为                         //void split(const Mat& mtx, vector<Mat>& mv)                         //对偶运算为void merge(const vector<Mat>& mv, OutputArray dst)

2) Create a window to display the picture to verify the conversion results
3) Create the slider bar.
First slider action: Select threshold type: binary, inverse binary, truncate, 0, anti 0.
Two, three or four slide bar effect: Select the size of the BGR channel (after the split) threshold respectively.
4) Wait for the user to drag the scroll bar to enter the threshold type and the size of the threshold, or the user type ESC to exit the program.

3. Nao Red ball recognition

We do red ball recognition in the remote Environment of the NAO robot, the external environment such as illumination has a great influence on the recognition result, so the BGR color space is generally converted into HSV space. Regardless of the choice of color space, multi-channel threshold segmentation is an important step to ensure the subsequent recognition. In addition, the use of the NAO robot camera to obtain images, white balance and exposure parameters such as the setting is also important, if the use of sliding bar to find the right parameters, it is also an efficient way!

OPENCV the multi-channel image by sliding bar threshold

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.