/*
author:Maracle
date:二〇一六年四月五日
content:Function to change between color and grayscale
representations of an image using GUI trackbar.
*/
#include<iostream>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
//全局变量
char* window_name = "Trackbar app";
Mat src;
int slider;
const int slider_max = 1;
//滑动条函数
void on_trackbar(int pos, void*)
{
Mat img_converted;
if (pos > 0)
{
cvtColor(src, img_converted, CV_RGB2GRAY);
}
else
img_converted = src;
imshow(window_name, img_converted);
}
int main()
{
src = imread("小黄人.png");
namedWindow(window_name);
imshow(window_name, src);
slider = 0;
createTrackbar("RGB<->GRAY", window_name, &slider, slider_max, on_trackbar);
while (char(waitKey(10)) != 'q'){}
return 0;
}
<img src="https://img-blog.csdn.net/20160405204332878?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" style="font-family: Arial, Helvetica, sans-serif;" alt="" />
<img src="https://img-blog.csdn.net/20160405204356519?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />