最終更新: mikk_ni3_92 2008年04月29日(火) 15:12:43履歴
現在地 >> メニュー >> サンプルコード::OpenCV >> OpenCV::シェルピンスキーのギャスケット >> OpenCV::シェルピンスキーのギャスケット2
#include <iostream>
#include <string>
#include <cv.h>
#include <highgui.h>
using namespace std;
namespace
{
const int dimension = 5;
}
/*******[プロトタイプ宣言]*******/
void DRAW_FRACTAL(IplImage *imgA);
void DRAW_SHELPINSKI(IplImage *imgA,CvPoint p1, CvPoint p2,CvPoint p3,int dim);
/*********[メイン関数]****************/
CvSize win_size={300,300};
int main(int argc,char**argv)
{
IplImage *imgA = cvCreateImage(win_size,IPL_DEPTH_8U,3);
cvSet (imgA, cvScalarAll (255), 0);
DRAW_FRACTAL(imgA);
cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
cvShowImage("window",imgA);
cvWaitKey(0); // 0秒待つ => ずっと入力待ち
cvReleaseImage(& imgA);
cvDestroyWindow("window");
return 0;
}
/**********[ ここから各種関数 ]***************/
void DRAW_FRACTAL(IplImage *imgA)
{
CvPoint a ={150,10},b={10,290},c={290,290};
DRAW_SHELPINSKI(imgA,a,b,c,dimension);
}
void DRAW_SHELPINSKI(IplImage *imgA,CvPoint p1, CvPoint p2,CvPoint p3,int dim)
{
if(dim == 0)
{
CvPoint *pts = (CvPoint *) cvAlloc (sizeof (CvPoint) * 3);
pts[0].x = p1.x; pts[0].y = p1.y;
pts[1].x = p2.x; pts[1].y = p2.y;
pts[2].x = p3.x; pts[2].y = p3.y;
cvFillConvexPoly (imgA, pts, 3, CV_RGB (0, 255, 0));
cvFree (&pts);
return;
}
CvPoint p4,p5,p6;
p4.x = int( ((p1.x + p2.x)/2.0) + 0.5 );
p4.y = int( ((p1.y + p2.y)/2.0) + 0.5 );
p5.x = int( ((p2.x + p3.x)/2.0) + 0.5 );
p5.y = int( ((p2.y + p3.y)/2.0) + 0.5 );
p6.x = int( ((p1.x + p3.x)/2.0) + 0.5 );
p6.y = int( ((p1.y + p3.y)/2.0) + 0.5 );
DRAW_SHELPINSKI(imgA,p1,p4,p6,dim-1);
DRAW_SHELPINSKI(imgA,p4,p2,p5,dim-1);
DRAW_SHELPINSKI(imgA,p6,p5,p3,dim-1);
}
#include <iostream>
#include <string>
#include <cv.h>
#include <highgui.h>
using namespace std;
namespace
{
const int dimension = 5;
}
/*******[プロトタイプ宣言]*******/
void DRAW_FRACTAL(IplImage *imgA);
void DRAW_SHELPINSKI(IplImage *imgA,CvPoint p1, CvPoint p2,CvPoint p3,int dim);
/*********[メイン関数]****************/
CvSize win_size={300,300};
int main(int argc,char**argv)
{
IplImage *imgA = cvCreateImage(win_size,IPL_DEPTH_8U,3);
cvSet (imgA, cvScalarAll (255), 0);
DRAW_FRACTAL(imgA);
cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
cvShowImage("window",imgA);
cvWaitKey(0); // 0秒待つ => ずっと入力待ち
cvReleaseImage(& imgA);
cvDestroyWindow("window");
return 0;
}
/**********[ ここから各種関数 ]***************/
void DRAW_FRACTAL(IplImage *imgA)
{
CvPoint a ={150,10},b={10,290},c={290,290};
DRAW_SHELPINSKI(imgA,a,b,c,dimension);
}
void DRAW_SHELPINSKI(IplImage *imgA,CvPoint p1, CvPoint p2,CvPoint p3,int dim)
{
if(dim == 0)
{
CvPoint *pts = (CvPoint *) cvAlloc (sizeof (CvPoint) * 3);
pts[0].x = p1.x; pts[0].y = p1.y;
pts[1].x = p2.x; pts[1].y = p2.y;
pts[2].x = p3.x; pts[2].y = p3.y;
cvFillConvexPoly (imgA, pts, 3, CV_RGB (0, 255, 0));
cvFree (&pts);
return;
}
CvPoint p4,p5,p6;
p4.x = int( ((p1.x + p2.x)/2.0) + 0.5 );
p4.y = int( ((p1.y + p2.y)/2.0) + 0.5 );
p5.x = int( ((p2.x + p3.x)/2.0) + 0.5 );
p5.y = int( ((p2.y + p3.y)/2.0) + 0.5 );
p6.x = int( ((p1.x + p3.x)/2.0) + 0.5 );
p6.y = int( ((p1.y + p3.y)/2.0) + 0.5 );
DRAW_SHELPINSKI(imgA,p1,p4,p6,dim-1);
DRAW_SHELPINSKI(imgA,p4,p2,p5,dim-1);
DRAW_SHELPINSKI(imgA,p6,p5,p3,dim-1);
}