OpenCV中轨迹条的创建

OpenCV中有自带的轨迹条创建函数createTrackbar(),如下:

int cv::createTrackbar  ( const String  trackbarname, 
    const String  winname, 
    int *  value, 
    int  count, 
    TrackbarCallback  onChange = 0, 
    void *  userdata = 0 
  )

Creates a trackbar and attaches it to the specified window.

在特定的窗口中创建一个轨迹条。

The function createTrackbar creates a trackbar (a slider or range control) with the specified name and range, assigns a variable value to be a position synchronized with the trackbar and specifies the callback function onChange to be called on the trackbar position change. The created trackbar is displayed in the specified window winname.

简单的来说就是创建一个轨迹条,每当改变位置就通过回调函数是实现同步。

Parameters
trackbarname Name of the created trackbar. 轨迹条的名字
winname Name of the window that will be used as a parent of the created trackbar. 指定窗口的名字
value Optional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable. 一个整型变量的指针,一般表示滑块的初始位置。
count Maximal position of the slider. The minimal position is always 0. 滑块可以表示的最大值

onChange

回调函数

Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated. 
userdata User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.

回调函数的形式必须是void xxx(int x,void*),第一个参数表示滑块的位置(注意是int型,此处不是指针),第二个参数为用户数据,,默认值为0。回调函数是用对特定事件的一个响应,不应直接调用

创建窗口的函数namedWindow()

void cv::namedWindow  ( const String  winname,
    int  flags = WINDOW_AUTOSIZE
  )
Parameters
winname Name of the window in the window caption that may be used as a window identifier. 窗口名称
flags Flags of the window. The supported flags are: (cv::WindowFlags) 标记,默认自适应

Flags for cv::namedWindow.

flags有一下几种类型:

Enumerator
WINDOW_NORMAL 
Python: cv.WINDOW_NORMAL

the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size. 

WINDOW_AUTOSIZE 
Python: cv.WINDOW_AUTOSIZE

the user cannot resize the window, the size is constrainted by the image displayed. 

WINDOW_OPENGL 
Python: cv.WINDOW_OPENGL

window with opengl support. 

WINDOW_FULLSCREEN 
Python: cv.WINDOW_FULLSCREEN

change the window to fullscreen. 

WINDOW_FREERATIO 
Python: cv.WINDOW_FREERATIO

the image expends as much as it can (no ratio constraint). 

WINDOW_KEEPRATIO 
Python: cv.WINDOW_KEEPRATIO

the ratio of the image is respected. 

WINDOW_GUI_EXPANDED 
Python: cv.WINDOW_GUI_EXPANDED

status bar and tool bar 

WINDOW_GUI_NORMAL 
Python: cv.WINDOW_GUI_NORMAL

old fashious way

示例:

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;

static void on_ContrastAndBright(int, void*);//回调函数

int g_nContrastValue;//对比度的初始值
int g_nBrightValue;//亮度的初始值
Mat g_srcImage, g_dstImage;//输入图像和输出图像

int main()
{
	g_srcImage = imread("1.jpg");
	if (!g_srcImage.data)
	{
		printf("error");
		return false;
	}
	//定义输入图像是和输入图像同尺寸、同数据类型的零矩阵
	g_dstImage = Mat::zeros(g_srcImage.size(), g_srcImage.type());
	g_nContrastValue = 80;
	g_nBrightValue = 80;
	namedWindow("【效果窗口】");
	createTrackbar("对比度", "【效果窗口】", &g_nContrastValue,
		300, on_ContrastAndBright);
	createTrackbar("亮  度", "【效果窗口】", &g_nBrightValue,
		200, on_ContrastAndBright);
	on_ContrastAndBright(g_nContrastValue, 0);
	on_ContrastAndBright(g_nBrightValue, 0);
	waitKey();
	destroyAllWindows();
	return 0;

}

static void on_ContrastAndBright(int, void*)
{
	namedWindow("【原始窗口】");
	for (int y = 0; y < g_srcImage.rows; y++)
	{
		for (int x = 0; x < g_srcImage.cols; x++)
		{
			for (int c = 0; c < 3; c++)
			{
				g_dstImage.at<Vec3b>(y, x)[c] =
					saturate_cast<uchar>((g_nContrastValue*0.01)
						*(g_srcImage.at<Vec3b>(y, x)[c]) + g_nBrightValue);
			}
		}
	}
	imshow("【原始窗口】", g_srcImage);
	imshow("【效果窗口】", g_dstImage);
}

效果:




猜你喜欢

转载自blog.csdn.net/SweetWind1996/article/details/80852594
今日推荐