Include the header file, there are a lot of doubts, and when you want to include what kind of header files
#include<opencv2/opencv.hpp>
//#include<opencv2/core/core.hpp>
//#include<opencv2/highgui/highgui.hpp>
//#include<opencv2/imgproc/imgproc.hpp>
//#include<iostream>
#include<math.h>
using namespace cv;
using namespace std;
Definition of variables and function declarations
Mat src, gray, dst;
int threshold_value =127;
int type_value=2;
int type_max = 4;
int threshold_max = 255;
const char*output_title = "binary image";
void Threshold_Demo(int, void*);
The main function:
int main(int angc, char** argv)
{
src = imread("1.jpg");
cvtColor(src, gray, COLOR_RGB2GRAY);//转为灰度图。
imshow("载入灰度图", gray);
//另一想法是直接 src=imread("1.jpg",0);//载入灰度图
//imshow("载入灰度图", src);
namedWindow(output_title, WINDOW_AUTOSIZE);
createTrackbar("阈值", output_title, &threshold_value, threshold_max, Threshold_Demo);//滑块名称,窗口名称,改变的量,量的最大值,
createTrackbar("模式", output_title, &type_value, type_max, Threshold_Demo);
Threshold_Demo(0, 0);
waitKey(0);
return(0);
}
Threshold_Demo () function
void Threshold_Demo(int, void*)
{
printf("%d", THRESH_BINARY);
threshold(gray, dst, threshold_value, threshold_max, type_value);
imshow(output_title, dst);
}