E:/dev/ssip2006/snake/snake.cpp

Go to the documentation of this file.
00001 //#include "stdafx.h"
00002 
00003 // STL includes:
00004 #include <iostream>
00005 using namespace std;
00006 
00007 // OpenCV includes
00008 #include "highgui.h"
00009 
00010 // local includes
00011 #include "snake.h"
00012 #include "QtCvWrapper.h"
00013 
00014 
00016 Snake::Snake(QtCvWrapper* parent, IplImage* inImg) :
00017         m_parent(parent),
00018         m_inImg(0),
00019         m_snakeImg(0),
00020         m_alpha((float)1.0),
00021         m_beta((float)1.0),
00022         m_gamma((float)1.2),
00023         m_numSegments(200),
00024         m_numIteration(20)
00025 {
00026         m_inImg = cvCloneImage(inImg);
00027 
00028         m_winSize.width = 5;
00029         m_winSize.height = 5;
00030 
00031         m_snakeImg = cvCreateImage( cvSize(m_inImg->width, m_inImg->height), 
00032                 IPL_DEPTH_8U, 3);
00033         m_crit = cvTermCriteria(CV_TERMCRIT_ITER, m_numIteration, 0.0);
00034         m_snakeCurve = new CvPoint[m_numSegments];
00035 }
00036 
00038 Snake::~Snake(void)
00039 {
00040         delete[] m_snakeCurve;
00041 }
00042 
00044 void Snake::initSnakeCurve()
00045 {
00046         m_snakeCurve = new CvPoint[m_numSegments];
00047         for(int i = 0; i < m_numSegments; i++)
00048         {
00049                 m_snakeCurve[i].x = cvRound( 0.8 * m_inImg->width * cos(i * 6.28 / m_numSegments) / 2.0
00050                         + m_inImg->width / 2 );
00051 
00052                 m_snakeCurve[i].y = cvRound( 0.8 * m_inImg->height * sin(i * 6.28 / m_numSegments) / 2.0
00053                         + m_inImg->height / 2 );
00054         }
00055 }
00056 
00058 void Snake::initSnakeCurve(CvPoint* pt)
00059 {
00060         m_snakeCurve = pt;
00061         if(m_inImg->nChannels == 1)
00062                 cvCvtColor(m_inImg, m_snakeImg, CV_GRAY2RGB);
00063         else if (m_inImg->nChannels == 3)
00064                 cvCopy( m_inImg, m_snakeImg, NULL);
00065 
00066         for (int actSegment = 0; actSegment < m_numSegments; actSegment++)
00067         {
00068                 if(actSegment < m_numSegments-1)
00069                         cvLine(m_snakeImg, pt[actSegment], pt[actSegment+1],
00070                         CV_RGB(255, 0, 0), 2, 1);
00071                 else
00072                         cvLine(m_snakeImg, pt[actSegment], pt[0],
00073                         CV_RGB(0, 255, 0), 2, 1);
00074         }
00075 }
00076 
00078 IplImage* Snake::iterateSnakeCurve(int numIterations, bool showIterations)
00079 {
00080         m_snakeImg = cvCreateImage( cvSize(m_inImg->width, m_inImg->height), 
00081                 IPL_DEPTH_8U, 3);
00082 
00083         char wndName[] = "SNAKE";
00084 
00085         for (int i = 0; i < numIterations; i++)
00086         {
00087                 cvSnakeImage(m_inImg, m_snakeCurve, m_numSegments, 
00088                         &m_alpha, &m_beta, &m_gamma, 1, m_winSize, m_crit, 1);
00089 
00090                 if (showIterations)
00091                 {
00092                         // clear the line with loading the inital image
00093                         if(m_inImg->nChannels == 1)
00094                                 cvCvtColor(m_inImg, m_snakeImg, CV_GRAY2RGB);
00095                         else if (m_inImg->nChannels == 3)
00096                                 cvCopy( m_inImg, m_snakeImg, NULL);
00097                         else
00098                                 qDebug("SNAKE - iterate: Undefined number of channels in the input image!");
00099 
00100                         for (int actSegment = 0; actSegment < m_numSegments; actSegment++)
00101                         {
00102                                 if(actSegment < m_numSegments-1)
00103                                         cvLine(m_snakeImg, m_snakeCurve[actSegment], m_snakeCurve[actSegment+1],
00104                                         CV_RGB(255, 0, 0), 2, 1);
00105                                 else
00106                                         cvLine(m_snakeImg, m_snakeCurve[actSegment], m_snakeCurve[0],
00107                                         CV_RGB(0, 255, 0), 2, 1);
00108                         }
00109                         if (m_parent->iterate() == true)
00110                         {
00111                                 cvNamedWindow( wndName, 1 );
00112                                 cvResizeWindow( wndName, m_inImg->width,m_inImg->height );
00113                                 cvShowImage( wndName, m_snakeImg );
00114 
00115                                 if (m_parent->step() == true)
00116                                         cvWaitKey(0);
00117                                 else
00118                                         cvWaitKey(200);
00119                         }
00120                 }
00121         }
00122         cvDestroyWindow(wndName);
00123         return m_snakeImg;
00124 }

Generated on Thu Jul 6 23:16:43 2006 for Snake Ballooning by  doxygen 1.4.7