最終更新: mikk_ni3_92 2008年05月09日(金) 14:06:57履歴
現在地 >> メニュー >> サンプルコード::OpenCV >> OpenCV::4分木
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
/***********[プロトタイプ宣言]********************/
void Mouse( int event, int x, int y, int flags ,void *imgA);
void DRAW_TREE_4( IplImage *imgA,CvPoint *pts, int x,int y, int dim);
/************[ここから、メイン関数]********************/
int main( int argc, char **argv)
{
static CvSize window_size={300,300};
IplImage *imgA = cvCreateImage(window_size,IPL_DEPTH_8U,3);
cvSet (imgA, cvScalarAll (255), 0);
cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
cvShowImage("window",imgA);
cvSetMouseCallback("window", Mouse,imgA);
cvWaitKey(0); // 0秒待つ => ずっと入力待ち
cvReleaseImage(& imgA);
cvDestroyWindow("window");
return 0;
}
/*************[ここから コールバック]***********************/
const int dimension = 3;
void Mouse( int event, int x, int y, int flags ,void *imgA) // コールバック関数
{
IplImage *clone = cvCloneImage( (IplImage *)imgA );
CvPoint *pts = (CvPoint *) cvAlloc (sizeof (CvPoint) * 2);
pts[0] = cvPoint(0,0);
pts[1] = cvPoint(clone->width,clone->height);
switch(event)
{
case CV_EVENT_LBUTTONDOWN:
DRAW_TREE_4( clone, pts ,x,y,dimension);
cvShowImage("window",clone);
break;
default:
break;
}
cvReleaseImage(&clone);
cvFree(&pts);
}
/*************[ここから、各種関数]******************/
void DRAW_TREE_4( IplImage *imgA, CvPoint *pts ,int x,int y, int dim)
{
if(dim <= 0)
{
cvRectangle(imgA,pts[0],pts[1],CV_RGB(0,0,255),CV_FILLED);
return;
}
CvPoint *middle = (CvPoint *) cvAlloc (sizeof (CvPoint) * 1);
middle->x = pts[0].x + int( ((pts[1].x - pts[0].x)/2.0) + 0.5 );
middle->y = pts[0].y + int( ((pts[1].y - pts[0].y)/2.0) + 0.5 );
cvLine(imgA,cvPoint( middle->x ,pts[0].y), cvPoint( middle->x ,pts[1].y),CV_RGB(255,0,0) );
cvLine(imgA,cvPoint( pts[0].x ,middle->y ), cvPoint( pts[1].x ,middle->y),CV_RGB(255,0,0) );
if( middle->x <= x )
{
pts[0].x = middle->x;
}else
{
pts[1].x = middle->x;
}
if(middle->y <= y)
{
pts[0].y = middle->y;
}else
{
pts[1].y = middle->y;
}
cvFree(&middle);
DRAW_TREE_4( imgA, pts ,x,y,dim-1);
}
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
/***********[プロトタイプ宣言]********************/
void Mouse( int event, int x, int y, int flags ,void *imgA);
void DRAW_TREE_4( IplImage *imgA,CvPoint *pts, int x,int y, int dim);
/************[ここから、メイン関数]********************/
int main( int argc, char **argv)
{
static CvSize window_size={300,300};
IplImage *imgA = cvCreateImage(window_size,IPL_DEPTH_8U,3);
cvSet (imgA, cvScalarAll (255), 0);
cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
cvShowImage("window",imgA);
cvSetMouseCallback("window", Mouse,imgA);
cvWaitKey(0); // 0秒待つ => ずっと入力待ち
cvReleaseImage(& imgA);
cvDestroyWindow("window");
return 0;
}
/*************[ここから コールバック]***********************/
const int dimension = 3;
void Mouse( int event, int x, int y, int flags ,void *imgA) // コールバック関数
{
IplImage *clone = cvCloneImage( (IplImage *)imgA );
CvPoint *pts = (CvPoint *) cvAlloc (sizeof (CvPoint) * 2);
pts[0] = cvPoint(0,0);
pts[1] = cvPoint(clone->width,clone->height);
switch(event)
{
case CV_EVENT_LBUTTONDOWN:
DRAW_TREE_4( clone, pts ,x,y,dimension);
cvShowImage("window",clone);
break;
default:
break;
}
cvReleaseImage(&clone);
cvFree(&pts);
}
/*************[ここから、各種関数]******************/
void DRAW_TREE_4( IplImage *imgA, CvPoint *pts ,int x,int y, int dim)
{
if(dim <= 0)
{
cvRectangle(imgA,pts[0],pts[1],CV_RGB(0,0,255),CV_FILLED);
return;
}
CvPoint *middle = (CvPoint *) cvAlloc (sizeof (CvPoint) * 1);
middle->x = pts[0].x + int( ((pts[1].x - pts[0].x)/2.0) + 0.5 );
middle->y = pts[0].y + int( ((pts[1].y - pts[0].y)/2.0) + 0.5 );
cvLine(imgA,cvPoint( middle->x ,pts[0].y), cvPoint( middle->x ,pts[1].y),CV_RGB(255,0,0) );
cvLine(imgA,cvPoint( pts[0].x ,middle->y ), cvPoint( pts[1].x ,middle->y),CV_RGB(255,0,0) );
if( middle->x <= x )
{
pts[0].x = middle->x;
}else
{
pts[1].x = middle->x;
}
if(middle->y <= y)
{
pts[0].y = middle->y;
}else
{
pts[1].y = middle->y;
}
cvFree(&middle);
DRAW_TREE_4( imgA, pts ,x,y,dim-1);
}