#include <opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include"opencv2/highgui/highgui.hpp"
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
#include<time.h>
using namespace std;
using namespace cv;
//------------------------------------------------------------------------------------------------
#define WINDOW_NAME "【程序窗口】" //为窗口标题定义的宏
Mat g_srcImage, g_dstImage, g_grayImage;
int g_nThresholdType = 3;
int g_nThresholdValue = 100;
void on_Threshold(int, void*);
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-----------------------------------------------------------------------------------------------
int main()
{
//载入原图
g_srcImage = imread("C:/Users/离子键/source/repos/Project8/1.jpg");//工程目录下需要有一张名为1.jpg的测试图像,且其尺寸需被2的N次方整除,N为可以缩放的次数
cvtColor(g_srcImage,g_grayImage,COLOR_RGB2GRAY);
namedWindow(WINDOW_NAME,WINDOW_AUTOSIZE);
createTrackbar("模式",WINDOW_NAME,&g_nThresholdType,4,on_Threshold);
createTrackbar("参数值", WINDOW_NAME, &g_nThresholdValue,255, on_Threshold);
on_Threshold(0,0);
while (1)
{
int key;
key = waitKey(20);
if ((char)key == 27){break;}
}
}
void on_Threshold(int, void*)
{
threshold(g_grayImage, g_dstImage, g_nThresholdValue, 255, g_nThresholdType);
imshow(WINDOW_NAME, g_dstImage);
}
运行结果: