OpenCVでGUIを使うhighgui

main.cpp

/*!
  \example example_4-1.cpp スクリーンにマウスで矩形を描画するサンプル

  \author Satofumi KAMIMURA

  $Id$
*/

#include <highgui.h>
#include <cv.h>


namespace
{
    const char* window_name = "Box Example";
    CvRect box_;
    bool is_drawing_box_ = false;


    void draw_box(IplImage* img, const CvRect& rect)
    {
        cvRectangle(img,
                    cvPoint(box_.x, box_.y),
                    cvPoint(box_.x + box_.width, box_.y + box_.height),
                    cvScalar(0xff, 0x00, 0x00));
    }


    void my_mouse_callback(int event, int x, int y, int flags, void *param)
    {
        IplImage* image = (IplImage*)param;

        switch (event) {
        case CV_EVENT_MOUSEMOVE: {
            if (is_drawing_box_) {
                box_.width = x - box_.x;
                box_.height = y - box_.y;
            }
        }
            break;

        case CV_EVENT_LBUTTONDOWN: {
            is_drawing_box_ = true;
            box_ = cvRect(x, y, 0, 0);
        }
            break;

        case CV_EVENT_LBUTTONUP: {
            is_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;
        }
    }
}


int main(int argc, char *argv[])
{
    // 初期化
    box_ = cvRect(-1, -1, 0, 0);

    IplImage* image = cvCreateImage(cvSize(200, 200), IPL_DEPTH_8U, 3);
    cvZero(image);
    IplImage* temp = cvCloneImage(image);

    cvNamedWindow(window_name);

    // マウスのコールバック関数を登録する
    cvSetMouseCallback(window_name, my_mouse_callback, (void*)image);

    // メイン処理
    // コールバックが発生するのを待つ
    enum {
        Wait_msec = 15,
        ESC_key = 27,
    };
    while (true) {
        cvCopyImage(image, temp);
        if (is_drawing_box_) {
            draw_box(temp, box_);
        }
        cvShowImage(window_name, temp);

        if (cvWaitKey(Wait_msec) == ESC_key) {
            break;
        }
    }

    // メモリを解放する
    cvReleaseImage(&image);
    cvReleaseImage(&temp);
    cvDestroyWindow(window_name);

    return 0;
}

main2.cpp

/*!
  \example example_4-2.cpp トラックバーを用いたボタンの実現

  \author Satofumi KAMIMURA

  $Id$
*/

#include <highgui.h>
#include <iostream>

using namespace std;


namespace
{
    const char* window_name = "Demo Window";
    int switch_value_ = 0;


    void switch_on_function(void)
    {
        cout << "on" << endl;
    }


    void switch_off_function(void)
    {
        cout << "off" << endl;
    }


    void switch_callback(int position)
    {
        if (position == 0) {
            switch_off_function();
        } else {
            switch_on_function();
        }
    }
}


int main(int argc, char *argv[])
{
    cvNamedWindow(window_name);

    // トラックバーの作成
    cvCreateTrackbar("Switch", window_name, &switch_value_, 1, switch_callback);

    // ESC が押されるまで待機
    enum {
        Wait_msec = 15,
        ESC_key = 27,
    };
    while (true) {
        if (cvWaitKey(Wait_msec) == ESC_key) {
            break;
        }
    }

    cvDestroyWindow(window_name);
    return 0;
}

main3.cpp

#include <cv.h>
#include <highgui.h>

using namespace cv;

/// Global Variables
const int alpha_slider_max = 100;
int alpha_slider;
double alpha;
double beta;

/// Matrices to store images
Mat src1;
Mat src2;
Mat dst;

/**
 * @function on_trackbar
 * @brief Callback for trackbar
 */
void on_trackbar( int, void* )
{
 alpha = (double) alpha_slider/alpha_slider_max ;
 beta = ( 1.0 - alpha );

 addWeighted( src1, alpha, src2, beta, 0.0, dst);

 imshow( "Linear Blend", dst );
}

int main( int argc, char** argv )
{
 /// Read image ( same size, same type )
 src1 = imread("image/mac.jpg");
 src2 = imread("image/win.jpg");

 if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
 if( !src2.data ) { printf("Error loading src2 \n"); return -1; }

 /// Initialize values
 alpha_slider = 0;

 /// Create Windows
 namedWindow("Linear Blend", 1);

 /// Create Trackbars
 char TrackbarName[50];
 sprintf( TrackbarName, "Alpha x %d", alpha_slider_max );

 createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );

 /// Show some stuff
 on_trackbar( alpha_slider, 0 );

 /// Wait until user press some key
 waitKey(0);
 return 0;
}

Makefile

CXXFLAGS = \
-I/usr/local/include \
-I/usr/local/include/opencv

LDFLAGS = \
-L/usr/local/lib \
-lopencv_core \
-lopencv_highgui \
-lopencv_features2d \
-lopencv_imgproc \
-lopencv_nonfree

all: main3.cpp
	g++-4.2 $(CXXFLAGS) $(LDFLAGS)  main3.cpp -o main