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
trackbarnameName of the created trackbar. 軌跡條的名字
winnameName of the window that will be used as a parent of the created trackbar. 指定窗口的名字
valueOptional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable. 一個整型變量的指針,一般表示滑塊的初始位置。
countMaximal 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. 
userdataUser 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
winnameName of the window in the window caption that may be used as a window identifier. 窗口名稱
flagsFlags 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);
}

效果:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章