歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

OpenCV3添加滑動條和鼠標事件到圖形界面

鼠標事件和滑動條控制在計算機視覺和OpenCV中非常有用,使用這些控件,用戶可以直接與圖形界面交互,改變輸入圖像或者變量的屬性值。

/*
    In this section, we are going to introduce you to the concepts of adding slider and
mouse events for basic interactions. To understand this correctly, we will create a small
project, where we paint green circles in the image using the mouse events and blur the
image with slider.
*/
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;

// Create a variable to save the position value in track
int blurAmount = 15;

// Trackbar call back function
static void onChange(int pos, void* userInput);

// Mouse callback
static void onMouse(int event, int x, int y, int, void* userInput);

int main(int argc, const char** argv)
{
    // Read images
    Mat boy = imread("../images/eating.jpg");

    // Create windows
    namedWindow("Boy");

    // Create a trackbar
    createTrackbar("Boy", "Boy", &blurAmount, 30, onChange, &boy);

    setMouseCallback("Boy", onMouse, &boy);

    // Call to onChange to init
    onChange(blurAmount, &boy);

    // wait app for a key to exit
    waitKey(0);

    // Destroy the windows
    destroyWindow("Boy");

    return 0;
}

// Trackbar call back function
static void onChange(int pos, void* userInput)
{
    if (pos <= 0) return;
    // Aux variable for result
    Mat imgBlur;

    // Get the pointer input image
    Mat* image = (Mat*)userInput;

    // Apply a blur filter
    blur(*image, imgBlur, Size(pos, pos));

    // Show the result
    imshow("Boy", imgBlur);
}

// Mouse callback
static void onMouse(int event, int x, int y, int, void* userInput)
{
    if (event != EVENT_LBUTTONDOWN) return;

    // Get the pointer input image
    Mat *image = (Mat*)userInput;

    // Draw circle
    circle(*image, Point(x, y), 10, Scalar(0, 255, 0), 3);

    // Call onChange to get blurred image
    onChange(blurAmount, image);
}

程序運行效果如下:

OpenCV官方教程中文版(For Python) PDF  http://www.linuxidc.com/Linux/2015-08/121400.htm

Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm

[翻譯]Ubuntu 14.04, 13.10 下安裝 OpenCV 2.4.9  http://www.linuxidc.com/Linux/2014-12/110045.htm

OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved