背景提取
基于Windows开发
HighGUI原生图形用户接口
HighGUI的图形用户输入工具只支持三个基本的交互,即按键、鼠标在图像区域点击、鼠标滚轮。
原始库的工具包的最大优势是它们速度快,使用简单,并且不需要安装额外的库。
使用cv::namedWindow()创建一个窗口
CV_EXPORTS_W void namedWindow(
const String& winname,
int flags = WINDOW_AUTOSIZE
);
如何销毁一个窗口
CV_EXPORTS_W void destroyWindow(const String& winname);
通过cv::imshow()显示图像
CV_EXPORTS_W void imshow(
const String& winname, //Name of the window.
InputArray mat //Image to be shown
);
更新窗口和cv::waitKey()函数
//Delay in milliseconds. 0 is the special value that means "forever"
CV_EXPORTS_W int waitKey(int delay = 0);
一个展示图像的例子
代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
static void help(char** argv) {
cout << "\n"
<< "Create a window and display an image\nCall:\n"
<< argv[0] << " image\n"
<< endl;
}
static void test() {
// Create a named window with the name of the file
//
string winname = "xiaojiejie";
string path = "renwu1.jpg";
cv::namedWindow(winname, cv::WINDOW_AUTOSIZE);
// Load the image from the given filename
//
cv::Mat img = cv::imread(path);
// Show the image in the named window
//
cv::imshow(winname, img);
// Idle until the user hits the Esc key
//
cv::waitKey(); //Any key will end the program
// Clean up and don't be piggies
//
cv::destroyWindow(winname);
exit(0);
}
int main(int argc, char** argv) {
// Document the interface
help(argv);
test();
system("pause");
return 0;
}
结果
鼠标事件
鼠标事件是通过更加传统的回调函数机制来完成。
typedef void (*MouseCallback)(
int event, //one of the cv::MouseEventTypes constants.
int x, //The x-coordinate of the mouse event.
int y, //The y-coordinate of the mouse event.
int flags, //one of the cv::MouseEventFlags constants.
void* userdata //The optional parameter.
);
CV_EXPORTS void setMouseCallback(
const String& winname, //Name of the window.
MouseCallback onMouse, //Callback function for mouse events. See OpenCV samples on how to specify and use the callback.
void* userdata = 0 //The optional parameter passed to the callback.
);
使用鼠标回调完成绘制方框的小程序
结果
代码
// Example 9-2. Toy program for using a mouse to draw boxes on the screen
#include <opencv2/opencv.hpp>
// Define our callback which we will install for
// mouse events
//
void my_mouse_callback(int event, int x, int y, int flags, void* param);
cv::Rect box;
bool drawing_box = false;
// A little subroutine to draw a box onto an image
//
void draw_box(cv::Mat& img, cv::Rect box) {
cv::rectangle(img,box.tl(),box.br(),
cv::Scalar(0x00, 0x00, 0xff) /* red */
);
}
static void help(char** argv) {
std::cout << "Example 9-2. Toy program for using a mouse to draw boxes on the screen"
<< "\nCall:\n" << argv[0] <<
"\n\nshows how to use a mouse to draw regions in an image. Esc to quit\n" << std::endl;
}
static void test() {
box = cv::Rect(-1, -1, 0, 0);
cv::Mat image(200, 200, CV_8UC3), temp;
image.copyTo(temp);
box = cv::Rect(-1, -1, 0, 0);
image = cv::Scalar::all(0);
cv::namedWindow("Box Example");
// Here is the crucial moment where we actually install
// the callback. Note that we set the value of 'params' to
// be the image we are working with so that the callback
// will have the image to edit.
//
cv::setMouseCallback(
"Box Example",
my_mouse_callback,
(void*)&image
);
// The main program loop. Here we copy the working image
// to the temp image, and if the user is drawing, then
// put the currently contemplated box onto that temp image.
// Display the temp image, and wait 15ms for a keystroke,
// then repeat.
//
for (;;) {
image.copyTo(temp);
if (drawing_box) draw_box(temp, box);
cv::imshow("Box Example", temp);
if (cv::waitKey(15) == 27) break;
}
}
int main(int argc, char** argv) {
help(argv);
test();
return 0;
}
// This is our mouse callback. If the user
// presses the left button, we start a box.
// When the user releases that button, then we
// add the box to the current image. When the
// mouse is dragged (with the button down) we
// resize the box.
//
void my_mouse_callback(int event, int x, int y, int flags, void* param)
{
cv::Mat& image = *(cv::Mat*) param;
switch (event) {
case cv::EVENT_MOUSEMOVE: {
if (drawing_box) {
box.width = x - box.x;
box.height = y - box.y;
}
}
break;
case cv::EVENT_LBUTTONDOWN: {
drawing_box = true;
box = cv::Rect(x, y, 0, 0);
}
break;
case cv::EVENT_LBUTTONUP: {
drawing_box = false;
if (box.width < 0) {
box.x += box.width;
box.width *= -1;
}
if (box.height < 0) {
box.y += box.height;
box.height *= -1;
}
draw_box(image, box);
}
break;
}
}
滑动条、滚动条和开关
HighGUI中提供了一个方便的滑动条(slider)单元。
//Example 9-3. Using a trackbar to create a “switch” that the user can turn on and off;
//this program plays a video and uses the switch to create a pause functionality
//
// An example program in which the user can draw boxes on the screen.
//
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
//
// Using a trackbar to create a "switch" that the user can turn on and off.
// We make this value global so everyone can see it.
//
int g_switch_value = 1;
void switch_off_function() { cout << "Pause\n"; }; //YOU COULD DO MORE
void switch_on_function() { cout << "Run\n"; };
// This will be the callback that we give to the trackbar.
//
void switch_callback(int position, void*) {
if (position == 0) {
switch_off_function();
}
else {
switch_on_function();
}
}
static void help(char ** argv) {
cout << "Example 9-3. Using a trackbar to create a “switch” that the user can turn on and off"
<< "\n this program plays a video and uses the switch to create a pause functionality."
<< "\n\nCall:\n" << argv[0] << " <path/video_file>"
<< "\n\nShows putting a pause button in a video; Esc to quit\n" << endl;
}
static void test()
{
cv::Mat frame; // To hold movie images
cv::VideoCapture g_capture;
g_capture.open("video1.avi");
// Name the main window
//
cv::namedWindow("Example", 1);
// Create the trackbar. We give it a name,
// and tell it the name of the parent window.
//
cv::createTrackbar(
"Switch",
"Example",
&g_switch_value,
1,
switch_callback
);
// This will cause OpenCV to idle until
// someone hits the Esc key.
//
for (;;) {
if (g_switch_value) {
g_capture >> frame;
if (frame.empty()) break;
cv::imshow("Example", frame);
}
if (cv::waitKey(10) == 27) break; //esc
}
}
int main(int argc, char** argv) {
help(argv);
test();
return 0;
}