選択領域の色反転表示
C++
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
using namespace cv;
Rect selection;
int select_object;
/* prototype declaration */
void on_mouse(int event, int x, int y, int flags, void* param);
int
main(int argc, char *argv[])
{
// (1)load a source image as is
const char *imagename = argc > 1 ? argv[1] : "../image/bambi.png";
Mat src_img = imread(imagename, -1);
if(!src_img.data)
return -1;
// (2)create a window and set the callback function for mouse events
namedWindow("Image", 1);
cvSetMouseCallback("Image", (CvMouseCallback)(&on_mouse), &src_img);
// (3)show the source image with an invert area, and quit when 'esc' pressed
while(1) {
Mat dst_img = src_img.clone();
if(select_object && selection.width > 0 && selection.height > 0) {
Mat roi(dst_img, selection);
bitwise_xor(roi, Scalar::all(255), roi);
}
imshow("Image", dst_img);
int key = waitKey(10);
if((char)key==27)
break;
}
return 0;
}
void
on_mouse(int event, int x, int y, int flags, void* param)
{
static Point2i origin;
Mat *img = static_cast<Mat*>(param);
// (4)calculate coordinates of selected area (by Click and Drag)
if(select_object) {
selection.x = CV_IMIN(x, origin.x);
selection.y = CV_IMIN(y, origin.y);
selection.width = selection.x + CV_IABS(x - origin.x);
selection.height = selection.y + CV_IABS(y - origin.y);
selection.x = CV_IMAX(selection.x, 0);
selection.y = CV_IMAX(selection.y, 0);
selection.width = CV_IMIN( selection.width, img->cols );
selection.height = CV_IMIN( selection.height, img->rows );
selection.width -= selection.x;
selection.height -= selection.y;
}
// (5)process a start and a finish selecting events (i.e. button-up and -down)
switch(event) {
case CV_EVENT_LBUTTONDOWN:
origin = Point2i(x,y);
selection = Rect(x, y, 0, 0);
select_object = 1;
break;
case CV_EVENT_LBUTTONUP:
select_object = 0;
break;
}
}
// (1)画像を読み込みます.
指定された画像を,そのままに(色の変換をせずに)読み込みます.つまり,カラー画像はカラー画像として,グレースケール画像はグレースケール画像として読み込まれます.
// (2)ウィンドウを作成し,マウスイベントに対するコールバック関数を設定します.
C++インタフェースには, cvSetMouseCallback に相当するものが存在しないので,この関数をそのまま利用します.
// (3)入力画像を表示し,”esc”キーが押されると終了します.
入力画像を表示し,”esc”キーが押された場合にプログラムを終了します.その際に bitwise_xor を用いて,選択領域と白い矩形との排他的論理和をとることによって,選択領域の色を反転します.また,選択領域を求めるために必要なマウスイベントは,on_mouse 関数で処理されます.
// (4)マウスドラッグによって選択された領域の頂点座標を計算します.
マウスドラッグ中は,選択領域が変化するので,その場合の選択領域の頂点座標と幅,高さをイベントの度に計算します.
// (5)マウスドラッグの開始と終了イベントを処理します.
マウスドラッグの開始(ボタンダウン)と終了(ボタンアップ)のイベントを処理します.



