Set stricter warning rules for gcc
This commit is contained in:
@@ -42,119 +42,119 @@
|
||||
#include "opencv2/contrib/contrib.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
|
||||
void help(char **argv)
|
||||
static void help(char **argv)
|
||||
{
|
||||
std::cout << "\nThis program demonstrates the contributed flesh detector CvAdaptiveSkinDetector which can be found in contrib.cpp\n"
|
||||
<< "Usage: " << std::endl <<
|
||||
argv[0] << " fileMask firstFrame lastFrame" << std::endl << std::endl <<
|
||||
"Example: " << std::endl <<
|
||||
argv[0] << " C:\\VideoSequences\\sample1\\right_view\\temp_%05d.jpg 0 1000" << std::endl <<
|
||||
" iterates through temp_00000.jpg to temp_01000.jpg" << std::endl << std::endl <<
|
||||
"If no parameter specified, this application will try to capture from the default Webcam." << std::endl <<
|
||||
"Please note: Background should not contain large surfaces with skin tone." <<
|
||||
"\n\n ESC will stop\n"
|
||||
"Using OpenCV version %s\n" << CV_VERSION << "\n"
|
||||
<< std::endl;
|
||||
std::cout << "\nThis program demonstrates the contributed flesh detector CvAdaptiveSkinDetector which can be found in contrib.cpp\n"
|
||||
<< "Usage: " << std::endl <<
|
||||
argv[0] << " fileMask firstFrame lastFrame" << std::endl << std::endl <<
|
||||
"Example: " << std::endl <<
|
||||
argv[0] << " C:\\VideoSequences\\sample1\\right_view\\temp_%05d.jpg 0 1000" << std::endl <<
|
||||
" iterates through temp_00000.jpg to temp_01000.jpg" << std::endl << std::endl <<
|
||||
"If no parameter specified, this application will try to capture from the default Webcam." << std::endl <<
|
||||
"Please note: Background should not contain large surfaces with skin tone." <<
|
||||
"\n\n ESC will stop\n"
|
||||
"Using OpenCV version %s\n" << CV_VERSION << "\n"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
class ASDFrameHolder
|
||||
{
|
||||
private:
|
||||
IplImage *image;
|
||||
double timeStamp;
|
||||
IplImage *image;
|
||||
double timeStamp;
|
||||
|
||||
public:
|
||||
ASDFrameHolder();
|
||||
virtual ~ASDFrameHolder();
|
||||
virtual void assignFrame(IplImage *sourceImage, double frameTime);
|
||||
inline IplImage *getImage();
|
||||
inline double getTimeStamp();
|
||||
virtual void setImage(IplImage *sourceImage);
|
||||
ASDFrameHolder();
|
||||
virtual ~ASDFrameHolder();
|
||||
virtual void assignFrame(IplImage *sourceImage, double frameTime);
|
||||
inline IplImage *getImage();
|
||||
inline double getTimeStamp();
|
||||
virtual void setImage(IplImage *sourceImage);
|
||||
};
|
||||
|
||||
class ASDFrameSequencer
|
||||
{
|
||||
public:
|
||||
virtual ~ASDFrameSequencer();
|
||||
virtual IplImage *getNextImage();
|
||||
virtual void close();
|
||||
virtual bool isOpen();
|
||||
virtual void getFrameCaption(char *caption);
|
||||
virtual ~ASDFrameSequencer();
|
||||
virtual IplImage *getNextImage();
|
||||
virtual void close();
|
||||
virtual bool isOpen();
|
||||
virtual void getFrameCaption(char *caption);
|
||||
};
|
||||
|
||||
class ASDCVFrameSequencer : public ASDFrameSequencer
|
||||
{
|
||||
protected:
|
||||
CvCapture *capture;
|
||||
CvCapture *capture;
|
||||
|
||||
public:
|
||||
virtual IplImage *getNextImage();
|
||||
virtual void close();
|
||||
virtual bool isOpen();
|
||||
virtual IplImage *getNextImage();
|
||||
virtual void close();
|
||||
virtual bool isOpen();
|
||||
};
|
||||
|
||||
class ASDFrameSequencerWebCam : public ASDCVFrameSequencer
|
||||
{
|
||||
public:
|
||||
virtual bool open(int cameraIndex);
|
||||
virtual bool open(int cameraIndex);
|
||||
};
|
||||
|
||||
class ASDFrameSequencerVideoFile : public ASDCVFrameSequencer
|
||||
{
|
||||
public:
|
||||
virtual bool open(const char *fileName);
|
||||
virtual bool open(const char *fileName);
|
||||
};
|
||||
|
||||
class ASDFrameSequencerImageFile : public ASDFrameSequencer {
|
||||
private:
|
||||
char sFileNameMask[2048];
|
||||
int nCurrentIndex, nStartIndex, nEndIndex;
|
||||
char sFileNameMask[2048];
|
||||
int nCurrentIndex, nStartIndex, nEndIndex;
|
||||
|
||||
public:
|
||||
virtual void open(const char *fileNameMask, int startIndex, int endIndex);
|
||||
virtual void getFrameCaption(char *caption);
|
||||
virtual IplImage *getNextImage();
|
||||
virtual void close();
|
||||
virtual bool isOpen();
|
||||
virtual void open(const char *fileNameMask, int startIndex, int endIndex);
|
||||
virtual void getFrameCaption(char *caption);
|
||||
virtual IplImage *getNextImage();
|
||||
virtual void close();
|
||||
virtual bool isOpen();
|
||||
};
|
||||
|
||||
//-------------------- ASDFrameHolder -----------------------//
|
||||
ASDFrameHolder::ASDFrameHolder( )
|
||||
{
|
||||
image = NULL;
|
||||
timeStamp = 0;
|
||||
image = NULL;
|
||||
timeStamp = 0;
|
||||
};
|
||||
|
||||
ASDFrameHolder::~ASDFrameHolder( )
|
||||
{
|
||||
cvReleaseImage(&image);
|
||||
cvReleaseImage(&image);
|
||||
};
|
||||
|
||||
void ASDFrameHolder::assignFrame(IplImage *sourceImage, double frameTime)
|
||||
{
|
||||
if (image != NULL)
|
||||
{
|
||||
cvReleaseImage(&image);
|
||||
image = NULL;
|
||||
}
|
||||
if (image != NULL)
|
||||
{
|
||||
cvReleaseImage(&image);
|
||||
image = NULL;
|
||||
}
|
||||
|
||||
image = cvCloneImage(sourceImage);
|
||||
timeStamp = frameTime;
|
||||
image = cvCloneImage(sourceImage);
|
||||
timeStamp = frameTime;
|
||||
};
|
||||
|
||||
IplImage *ASDFrameHolder::getImage()
|
||||
{
|
||||
return image;
|
||||
return image;
|
||||
};
|
||||
|
||||
double ASDFrameHolder::getTimeStamp()
|
||||
{
|
||||
return timeStamp;
|
||||
return timeStamp;
|
||||
};
|
||||
|
||||
void ASDFrameHolder::setImage(IplImage *sourceImage)
|
||||
{
|
||||
image = sourceImage;
|
||||
image = sourceImage;
|
||||
};
|
||||
|
||||
|
||||
@@ -162,12 +162,12 @@ void ASDFrameHolder::setImage(IplImage *sourceImage)
|
||||
|
||||
ASDFrameSequencer::~ASDFrameSequencer()
|
||||
{
|
||||
close();
|
||||
close();
|
||||
};
|
||||
|
||||
IplImage *ASDFrameSequencer::getNextImage()
|
||||
{
|
||||
return NULL;
|
||||
return NULL;
|
||||
};
|
||||
|
||||
void ASDFrameSequencer::close()
|
||||
@@ -177,40 +177,40 @@ void ASDFrameSequencer::close()
|
||||
|
||||
bool ASDFrameSequencer::isOpen()
|
||||
{
|
||||
return false;
|
||||
return false;
|
||||
};
|
||||
|
||||
void ASDFrameSequencer::getFrameCaption(char* /*caption*/) {
|
||||
return;
|
||||
return;
|
||||
};
|
||||
|
||||
IplImage* ASDCVFrameSequencer::getNextImage()
|
||||
{
|
||||
IplImage *image;
|
||||
IplImage *image;
|
||||
|
||||
image = cvQueryFrame(capture);
|
||||
image = cvQueryFrame(capture);
|
||||
|
||||
if (image != NULL)
|
||||
{
|
||||
return cvCloneImage(image);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (image != NULL)
|
||||
{
|
||||
return cvCloneImage(image);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
};
|
||||
|
||||
void ASDCVFrameSequencer::close()
|
||||
{
|
||||
if (capture != NULL)
|
||||
{
|
||||
cvReleaseCapture(&capture);
|
||||
}
|
||||
if (capture != NULL)
|
||||
{
|
||||
cvReleaseCapture(&capture);
|
||||
}
|
||||
};
|
||||
|
||||
bool ASDCVFrameSequencer::isOpen()
|
||||
{
|
||||
return (capture != NULL);
|
||||
return (capture != NULL);
|
||||
};
|
||||
|
||||
|
||||
@@ -218,18 +218,18 @@ bool ASDCVFrameSequencer::isOpen()
|
||||
|
||||
bool ASDFrameSequencerWebCam::open(int cameraIndex)
|
||||
{
|
||||
close();
|
||||
close();
|
||||
|
||||
capture = cvCaptureFromCAM(cameraIndex);
|
||||
capture = cvCaptureFromCAM(cameraIndex);
|
||||
|
||||
if (!capture)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!capture)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -237,17 +237,17 @@ bool ASDFrameSequencerWebCam::open(int cameraIndex)
|
||||
|
||||
bool ASDFrameSequencerVideoFile::open(const char *fileName)
|
||||
{
|
||||
close();
|
||||
close();
|
||||
|
||||
capture = cvCaptureFromFile(fileName);
|
||||
if (!capture)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
capture = cvCaptureFromFile(fileName);
|
||||
if (!capture)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -255,159 +255,159 @@ bool ASDFrameSequencerVideoFile::open(const char *fileName)
|
||||
|
||||
void ASDFrameSequencerImageFile::open(const char *fileNameMask, int startIndex, int endIndex)
|
||||
{
|
||||
nCurrentIndex = startIndex-1;
|
||||
nStartIndex = startIndex;
|
||||
nEndIndex = endIndex;
|
||||
nCurrentIndex = startIndex-1;
|
||||
nStartIndex = startIndex;
|
||||
nEndIndex = endIndex;
|
||||
|
||||
std::sprintf(sFileNameMask, "%s", fileNameMask);
|
||||
std::sprintf(sFileNameMask, "%s", fileNameMask);
|
||||
};
|
||||
|
||||
void ASDFrameSequencerImageFile::getFrameCaption(char *caption) {
|
||||
std::sprintf(caption, sFileNameMask, nCurrentIndex);
|
||||
std::sprintf(caption, sFileNameMask, nCurrentIndex);
|
||||
};
|
||||
|
||||
IplImage* ASDFrameSequencerImageFile::getNextImage()
|
||||
{
|
||||
char fileName[2048];
|
||||
char fileName[2048];
|
||||
|
||||
nCurrentIndex++;
|
||||
nCurrentIndex++;
|
||||
|
||||
if (nCurrentIndex > nEndIndex)
|
||||
return NULL;
|
||||
if (nCurrentIndex > nEndIndex)
|
||||
return NULL;
|
||||
|
||||
std::sprintf(fileName, sFileNameMask, nCurrentIndex);
|
||||
std::sprintf(fileName, sFileNameMask, nCurrentIndex);
|
||||
|
||||
IplImage* img = cvLoadImage(fileName);
|
||||
IplImage* img = cvLoadImage(fileName);
|
||||
|
||||
return img;
|
||||
return img;
|
||||
};
|
||||
|
||||
void ASDFrameSequencerImageFile::close()
|
||||
{
|
||||
nCurrentIndex = nEndIndex+1;
|
||||
nCurrentIndex = nEndIndex+1;
|
||||
};
|
||||
|
||||
bool ASDFrameSequencerImageFile::isOpen()
|
||||
{
|
||||
return (nCurrentIndex <= nEndIndex);
|
||||
return (nCurrentIndex <= nEndIndex);
|
||||
};
|
||||
|
||||
void putTextWithShadow(IplImage *img, const char *str, CvPoint point, CvFont *font, CvScalar color = CV_RGB(255, 255, 128))
|
||||
static void putTextWithShadow(IplImage *img, const char *str, CvPoint point, CvFont *font, CvScalar color = CV_RGB(255, 255, 128))
|
||||
{
|
||||
cvPutText(img, str, cvPoint(point.x-1,point.y-1), font, CV_RGB(0, 0, 0));
|
||||
cvPutText(img, str, point, font, color);
|
||||
cvPutText(img, str, cvPoint(point.x-1,point.y-1), font, CV_RGB(0, 0, 0));
|
||||
cvPutText(img, str, point, font, color);
|
||||
};
|
||||
|
||||
#define ASD_RGB_SET_PIXEL(pointer, r, g, b) { (*pointer) = (unsigned char)b; (*(pointer+1)) = (unsigned char)g; (*(pointer+2)) = (unsigned char)r; }
|
||||
#define ASD_RGB_SET_PIXEL(pointer, r, g, b) { (*pointer) = (unsigned char)b; (*(pointer+1)) = (unsigned char)g; (*(pointer+2)) = (unsigned char)r; }
|
||||
|
||||
#define ASD_RGB_GET_PIXEL(pointer, r, g, b) {b = (unsigned char)(*(pointer)); g = (unsigned char)(*(pointer+1)); r = (unsigned char)(*(pointer+2));}
|
||||
|
||||
void displayBuffer(IplImage *rgbDestImage, IplImage *buffer, int rValue, int gValue, int bValue)
|
||||
static void displayBuffer(IplImage *rgbDestImage, IplImage *buffer, int rValue, int gValue, int bValue)
|
||||
{
|
||||
int x, y, nWidth, nHeight;
|
||||
double destX, destY, dx, dy;
|
||||
uchar c;
|
||||
unsigned char *pSrc;
|
||||
int x, y, nWidth, nHeight;
|
||||
double destX, destY, dx, dy;
|
||||
uchar c;
|
||||
unsigned char *pSrc;
|
||||
|
||||
nWidth = buffer->width;
|
||||
nHeight = buffer->height;
|
||||
nWidth = buffer->width;
|
||||
nHeight = buffer->height;
|
||||
|
||||
dx = double(rgbDestImage->width)/double(nWidth);
|
||||
dy = double(rgbDestImage->height)/double(nHeight);
|
||||
dx = double(rgbDestImage->width)/double(nWidth);
|
||||
dy = double(rgbDestImage->height)/double(nHeight);
|
||||
|
||||
destX = 0;
|
||||
for (x = 0; x < nWidth; x++)
|
||||
{
|
||||
destY = 0;
|
||||
for (y = 0; y < nHeight; y++)
|
||||
{
|
||||
c = ((uchar*)(buffer->imageData + buffer->widthStep*y))[x];
|
||||
destX = 0;
|
||||
for (x = 0; x < nWidth; x++)
|
||||
{
|
||||
destY = 0;
|
||||
for (y = 0; y < nHeight; y++)
|
||||
{
|
||||
c = ((uchar*)(buffer->imageData + buffer->widthStep*y))[x];
|
||||
|
||||
if (c)
|
||||
{
|
||||
pSrc = (unsigned char *)rgbDestImage->imageData + rgbDestImage->widthStep*int(destY) + (int(destX)*rgbDestImage->nChannels);
|
||||
ASD_RGB_SET_PIXEL(pSrc, rValue, gValue, bValue);
|
||||
}
|
||||
destY += dy;
|
||||
}
|
||||
destY = 0;
|
||||
destX += dx;
|
||||
}
|
||||
if (c)
|
||||
{
|
||||
pSrc = (unsigned char *)rgbDestImage->imageData + rgbDestImage->widthStep*int(destY) + (int(destX)*rgbDestImage->nChannels);
|
||||
ASD_RGB_SET_PIXEL(pSrc, rValue, gValue, bValue);
|
||||
}
|
||||
destY += dy;
|
||||
}
|
||||
destY = 0;
|
||||
destX += dx;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv )
|
||||
{
|
||||
IplImage *img, *filterMask = NULL;
|
||||
CvAdaptiveSkinDetector filter(1, CvAdaptiveSkinDetector::MORPHING_METHOD_ERODE_DILATE);
|
||||
ASDFrameSequencer *sequencer;
|
||||
CvFont base_font;
|
||||
char caption[2048], s[256], windowName[256];
|
||||
long int clockTotal = 0, numFrames = 0;
|
||||
std::clock_t clock;
|
||||
IplImage *img, *filterMask = NULL;
|
||||
CvAdaptiveSkinDetector filter(1, CvAdaptiveSkinDetector::MORPHING_METHOD_ERODE_DILATE);
|
||||
ASDFrameSequencer *sequencer;
|
||||
CvFont base_font;
|
||||
char caption[2048], s[256], windowName[256];
|
||||
long int clockTotal = 0, numFrames = 0;
|
||||
std::clock_t clock;
|
||||
|
||||
if (argc < 4)
|
||||
{
|
||||
help(argv);
|
||||
sequencer = new ASDFrameSequencerWebCam();
|
||||
(dynamic_cast<ASDFrameSequencerWebCam*>(sequencer))->open(-1);
|
||||
if (argc < 4)
|
||||
{
|
||||
help(argv);
|
||||
sequencer = new ASDFrameSequencerWebCam();
|
||||
(dynamic_cast<ASDFrameSequencerWebCam*>(sequencer))->open(-1);
|
||||
|
||||
if (! sequencer->isOpen())
|
||||
{
|
||||
std::cout << std::endl << "Error: Cannot initialize the default Webcam" << std::endl << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sequencer = new ASDFrameSequencerImageFile();
|
||||
(dynamic_cast<ASDFrameSequencerImageFile*>(sequencer))->open(argv[1], std::atoi(argv[2]), std::atoi(argv[3]) ); // A sequence of images captured from video source, is stored here
|
||||
if (! sequencer->isOpen())
|
||||
{
|
||||
std::cout << std::endl << "Error: Cannot initialize the default Webcam" << std::endl << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sequencer = new ASDFrameSequencerImageFile();
|
||||
(dynamic_cast<ASDFrameSequencerImageFile*>(sequencer))->open(argv[1], std::atoi(argv[2]), std::atoi(argv[3]) ); // A sequence of images captured from video source, is stored here
|
||||
|
||||
}
|
||||
std::sprintf(windowName, "%s", "Adaptive Skin Detection Algorithm for Video Sequences");
|
||||
}
|
||||
std::sprintf(windowName, "%s", "Adaptive Skin Detection Algorithm for Video Sequences");
|
||||
|
||||
cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE);
|
||||
cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5);
|
||||
cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE);
|
||||
cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5);
|
||||
|
||||
// Usage:
|
||||
// c:\>CvASDSample "C:\VideoSequences\sample1\right_view\temp_%05d.jpg" 0 1000
|
||||
// Usage:
|
||||
// c:\>CvASDSample "C:\VideoSequences\sample1\right_view\temp_%05d.jpg" 0 1000
|
||||
|
||||
std::cout << "Press ESC to stop." << std::endl << std::endl;
|
||||
while ((img = sequencer->getNextImage()) != 0)
|
||||
{
|
||||
numFrames++;
|
||||
std::cout << "Press ESC to stop." << std::endl << std::endl;
|
||||
while ((img = sequencer->getNextImage()) != 0)
|
||||
{
|
||||
numFrames++;
|
||||
|
||||
if (filterMask == NULL)
|
||||
{
|
||||
filterMask = cvCreateImage( cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
|
||||
}
|
||||
clock = std::clock();
|
||||
filter.process(img, filterMask); // DETECT SKIN
|
||||
clockTotal += (std::clock() - clock);
|
||||
if (filterMask == NULL)
|
||||
{
|
||||
filterMask = cvCreateImage( cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
|
||||
}
|
||||
clock = std::clock();
|
||||
filter.process(img, filterMask); // DETECT SKIN
|
||||
clockTotal += (std::clock() - clock);
|
||||
|
||||
displayBuffer(img, filterMask, 0, 255, 0);
|
||||
displayBuffer(img, filterMask, 0, 255, 0);
|
||||
|
||||
sequencer->getFrameCaption(caption);
|
||||
std::sprintf(s, "%s - %d x %d", caption, img->width, img->height);
|
||||
putTextWithShadow(img, s, cvPoint(10, img->height-35), &base_font);
|
||||
sequencer->getFrameCaption(caption);
|
||||
std::sprintf(s, "%s - %d x %d", caption, img->width, img->height);
|
||||
putTextWithShadow(img, s, cvPoint(10, img->height-35), &base_font);
|
||||
|
||||
std::sprintf(s, "Average processing time per frame: %5.2fms", (double(clockTotal*1000/CLOCKS_PER_SEC))/numFrames);
|
||||
putTextWithShadow(img, s, cvPoint(10, img->height-15), &base_font);
|
||||
std::sprintf(s, "Average processing time per frame: %5.2fms", (double(clockTotal*1000/CLOCKS_PER_SEC))/numFrames);
|
||||
putTextWithShadow(img, s, cvPoint(10, img->height-15), &base_font);
|
||||
|
||||
cvShowImage (windowName, img);
|
||||
cvReleaseImage(&img);
|
||||
cvShowImage (windowName, img);
|
||||
cvReleaseImage(&img);
|
||||
|
||||
if (cvWaitKey(1) == 27)
|
||||
break;
|
||||
}
|
||||
if (cvWaitKey(1) == 27)
|
||||
break;
|
||||
}
|
||||
|
||||
sequencer->close();
|
||||
delete sequencer;
|
||||
sequencer->close();
|
||||
delete sequencer;
|
||||
|
||||
cvReleaseImage(&filterMask);
|
||||
cvReleaseImage(&filterMask);
|
||||
|
||||
cvDestroyWindow(windowName);
|
||||
cvDestroyWindow(windowName);
|
||||
|
||||
std::cout << "Finished, " << numFrames << " frames processed." << std::endl;
|
||||
std::cout << "Finished, " << numFrames << " frames processed." << std::endl;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -1,11 +1,11 @@
|
||||
// Background average sample code done with averages and done with codebooks
|
||||
// (adapted from the OpenCV book sample)
|
||||
//
|
||||
//
|
||||
// NOTE: To get the keyboard to work, you *have* to have one of the video windows be active
|
||||
// and NOT the consule window.
|
||||
//
|
||||
// Gary Bradski Oct 3, 2008.
|
||||
//
|
||||
//
|
||||
/* *************** License:**************************
|
||||
Oct. 3, 2008
|
||||
Right to use this code in any way you want without warrenty, support or any guarentee of it working.
|
||||
@@ -14,11 +14,11 @@
|
||||
Learning OpenCV: Computer Vision with the OpenCV Library
|
||||
by Gary Bradski and Adrian Kaehler
|
||||
Published by O'Reilly Media, October 3, 2008
|
||||
|
||||
AVAILABLE AT:
|
||||
|
||||
AVAILABLE AT:
|
||||
http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134
|
||||
Or: http://oreilly.com/catalog/9780596516130/
|
||||
ISBN-10: 0596516134 or: ISBN-13: 978-0596516130
|
||||
ISBN-10: 0596516134 or: ISBN-13: 978-0596516130
|
||||
************************************************** */
|
||||
#include "opencv2/core/core.hpp"
|
||||
#include "opencv2/video/background_segm.hpp"
|
||||
@@ -38,19 +38,19 @@ CvBGCodeBookModel* model = 0;
|
||||
const int NCHANNELS = 3;
|
||||
bool ch[NCHANNELS]={true,true,true}; // This sets what channels should be adjusted for background bounds
|
||||
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
printf("\nLearn background and find foreground using simple average and average difference learning method:\n"
|
||||
"Originally from the book: Learning OpenCV by O'Reilly press\n"
|
||||
"Originally from the book: Learning OpenCV by O'Reilly press\n"
|
||||
"\nUSAGE:\n"
|
||||
" bgfg_codebook [--nframes(-nf)=300] [--movie_filename(-mf)=tree.avi] [--camera(-c), use camera or not]\n"
|
||||
"***Keep the focus on the video windows, NOT the consol***\n\n"
|
||||
"INTERACTIVE PARAMETERS:\n"
|
||||
"\tESC,q,Q - quit the program\n"
|
||||
"\th - print this help\n"
|
||||
"\tp - pause toggle\n"
|
||||
"\ts - single step\n"
|
||||
"\tr - run mode (single step off)\n"
|
||||
"\th - print this help\n"
|
||||
"\tp - pause toggle\n"
|
||||
"\ts - single step\n"
|
||||
"\tr - run mode (single step off)\n"
|
||||
"=== AVG PARAMS ===\n"
|
||||
"\t- - bump high threshold UP by 0.25\n"
|
||||
"\t= - bump high threshold DOWN by 0.25\n"
|
||||
@@ -58,10 +58,10 @@ void help()
|
||||
"\t] - bump low threshold DOWN by 0.25\n"
|
||||
"=== CODEBOOK PARAMS ===\n"
|
||||
"\ty,u,v- only adjust channel 0(y) or 1(u) or 2(v) respectively\n"
|
||||
"\ta - adjust all 3 channels at once\n"
|
||||
"\tb - adjust both 2 and 3 at once\n"
|
||||
"\ti,o - bump upper threshold up,down by 1\n"
|
||||
"\tk,l - bump lower threshold up,down by 1\n"
|
||||
"\ta - adjust all 3 channels at once\n"
|
||||
"\tb - adjust both 2 and 3 at once\n"
|
||||
"\ti,o - bump upper threshold up,down by 1\n"
|
||||
"\tk,l - bump lower threshold up,down by 1\n"
|
||||
"\tSPACE - reset the model\n"
|
||||
);
|
||||
}
|
||||
@@ -91,7 +91,7 @@ int main(int argc, const char** argv)
|
||||
int c, n, nframes = 0;
|
||||
|
||||
model = cvCreateBGCodeBookModel();
|
||||
|
||||
|
||||
//Set color thresholds to default values
|
||||
model->modMin[0] = 3;
|
||||
model->modMin[1] = model->modMin[2] = 3;
|
||||
@@ -127,12 +127,12 @@ int main(int argc, const char** argv)
|
||||
{
|
||||
rawImage = cvQueryFrame( capture );
|
||||
++nframes;
|
||||
if(!rawImage)
|
||||
if(!rawImage)
|
||||
break;
|
||||
}
|
||||
if( singlestep )
|
||||
pause = true;
|
||||
|
||||
|
||||
//First time:
|
||||
if( nframes == 1 && rawImage )
|
||||
{
|
||||
@@ -141,13 +141,13 @@ int main(int argc, const char** argv)
|
||||
ImaskCodeBook = cvCreateImage( cvGetSize(rawImage), IPL_DEPTH_8U, 1 );
|
||||
ImaskCodeBookCC = cvCreateImage( cvGetSize(rawImage), IPL_DEPTH_8U, 1 );
|
||||
cvSet(ImaskCodeBook,cvScalar(255));
|
||||
|
||||
|
||||
cvNamedWindow( "Raw", 1 );
|
||||
cvNamedWindow( "ForegroundCodeBook",1);
|
||||
cvNamedWindow( "CodeBook_ConnectComp",1);
|
||||
}
|
||||
|
||||
// If we've got an rawImage and are good to go:
|
||||
// If we've got an rawImage and are good to go:
|
||||
if( rawImage )
|
||||
{
|
||||
cvCvtColor( rawImage, yuvImage, CV_BGR2YCrCb );//YUV For codebook method
|
||||
@@ -157,14 +157,14 @@ int main(int argc, const char** argv)
|
||||
|
||||
if( nframes-1 == nframesToLearnBG )
|
||||
cvBGCodeBookClearStale( model, model->t/2 );
|
||||
|
||||
|
||||
//Find the foreground if any
|
||||
if( nframes-1 >= nframesToLearnBG )
|
||||
{
|
||||
// Find foreground by codebook method
|
||||
cvBGCodeBookDiff( model, yuvImage, ImaskCodeBook );
|
||||
// This part just to visualize bounding boxes and centers if desired
|
||||
cvCopy(ImaskCodeBook,ImaskCodeBookCC);
|
||||
cvCopy(ImaskCodeBook,ImaskCodeBookCC);
|
||||
cvSegmentFGMask( ImaskCodeBookCC );
|
||||
}
|
||||
//Display
|
||||
@@ -205,7 +205,7 @@ int main(int argc, const char** argv)
|
||||
case 'u': case '1':
|
||||
case 'v': case '2':
|
||||
case 'a': case '3':
|
||||
case 'b':
|
||||
case 'b':
|
||||
ch[0] = c == 'y' || c == '0' || c == 'a' || c == '3';
|
||||
ch[1] = c == 'u' || c == '1' || c == 'a' || c == '3' || c == 'b';
|
||||
ch[2] = c == 'v' || c == '2' || c == 'a' || c == '3' || c == 'b';
|
||||
@@ -230,8 +230,8 @@ int main(int argc, const char** argv)
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cvReleaseCapture( &capture );
|
||||
cvDestroyWindow( "Raw" );
|
||||
cvDestroyWindow( "ForegroundCodeBook");
|
||||
|
@@ -1,22 +1,23 @@
|
||||
#include <opencv2/imgproc/imgproc_c.h>
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "opencv2/imgproc/imgproc_c.h"
|
||||
#include "opencv2/highgui/highgui_c.h"
|
||||
#include <stdio.h>
|
||||
void help()
|
||||
|
||||
static void help(void)
|
||||
{
|
||||
printf("\nThis program creates an image to demonstrate the use of the \"c\" contour\n"
|
||||
"functions: cvFindContours() and cvApproxPoly() along with the storage\n"
|
||||
"functions cvCreateMemStorage() and cvDrawContours().\n"
|
||||
"It also shows the use of a trackbar to control contour retrieval.\n"
|
||||
"\n"
|
||||
printf("\nThis program creates an image to demonstrate the use of the \"c\" contour\n"
|
||||
"functions: cvFindContours() and cvApproxPoly() along with the storage\n"
|
||||
"functions cvCreateMemStorage() and cvDrawContours().\n"
|
||||
"It also shows the use of a trackbar to control contour retrieval.\n"
|
||||
"\n"
|
||||
"Usage :\n"
|
||||
"./contours\n");
|
||||
"./contours\n");
|
||||
}
|
||||
|
||||
#define w 500
|
||||
int levels = 3;
|
||||
CvSeq* contours = 0;
|
||||
|
||||
void on_trackbar(int pos)
|
||||
static void on_trackbar(int pos)
|
||||
{
|
||||
IplImage* cnt_img = cvCreateImage( cvSize(w,w), 8, 3 );
|
||||
CvSeq* _contours = contours;
|
||||
@@ -36,7 +37,7 @@ static void findCComp( IplImage* img )
|
||||
cvZero(mask);
|
||||
cvRectangle( mask, cvPoint(0, 0), cvPoint(mask->width-1, mask->height-1),
|
||||
cvScalarAll(1), 1, 8, 0 );
|
||||
|
||||
|
||||
for( y = 0; y < img->height; y++ )
|
||||
for( x = 0; x < img->width; x++ )
|
||||
{
|
||||
@@ -49,7 +50,7 @@ static void findCComp( IplImage* img )
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int i, j;
|
||||
CvMemStorage* storage = cvCreateMemStorage(0);
|
||||
@@ -100,11 +101,11 @@ int main()
|
||||
|
||||
cvFindContours( img32s, storage, &contours, sizeof(CvContour),
|
||||
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
|
||||
|
||||
|
||||
//cvFindContours( img, storage, &contours, sizeof(CvContour),
|
||||
// CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
|
||||
|
||||
|
||||
|
||||
|
||||
{
|
||||
const char* attrs[] = {"recursive", "1", 0};
|
||||
cvSave("contours.xml", contours, 0, 0, cvAttrList(attrs, 0));
|
||||
@@ -119,7 +120,7 @@ int main()
|
||||
|
||||
{
|
||||
CvRNG rng = cvRNG(-1);
|
||||
|
||||
|
||||
CvSeq* tcontours = contours;
|
||||
cvCvtColor( img, img3, CV_GRAY2BGR );
|
||||
while( tcontours->h_next )
|
||||
@@ -142,9 +143,9 @@ int main()
|
||||
cvDrawContours(img3, tcontours->v_next, color, color, 1, -1, 8, cvPoint(0,0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
cvShowImage( "colored", img3 );
|
||||
on_trackbar(0);
|
||||
cvWaitKey(0);
|
||||
@@ -153,7 +154,7 @@ int main()
|
||||
cvReleaseImage( &img32f );
|
||||
cvReleaseImage( &img32s );
|
||||
cvReleaseImage( &img3 );
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -4,7 +4,7 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void help()
|
||||
static void help(void)
|
||||
{
|
||||
printf("\n This sample demonstrates cascade's convertation \n"
|
||||
"Usage:\n"
|
||||
|
@@ -3,7 +3,7 @@
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
void help()
|
||||
static void help( void )
|
||||
{
|
||||
printf("\nThis program demostrates iterative construction of\n"
|
||||
"delaunay triangulation and voronoi tesselation.\n"
|
||||
@@ -14,7 +14,7 @@ void help()
|
||||
"hitting any key.\n");
|
||||
}
|
||||
|
||||
CvSubdiv2D* init_delaunay( CvMemStorage* storage,
|
||||
static CvSubdiv2D* init_delaunay( CvMemStorage* storage,
|
||||
CvRect rect )
|
||||
{
|
||||
CvSubdiv2D* subdiv;
|
||||
@@ -29,13 +29,13 @@ CvSubdiv2D* init_delaunay( CvMemStorage* storage,
|
||||
}
|
||||
|
||||
|
||||
void draw_subdiv_point( IplImage* img, CvPoint2D32f fp, CvScalar color )
|
||||
static void draw_subdiv_point( IplImage* img, CvPoint2D32f fp, CvScalar color )
|
||||
{
|
||||
cvCircle( img, cvPoint(cvRound(fp.x), cvRound(fp.y)), 3, color, CV_FILLED, 8, 0 );
|
||||
}
|
||||
|
||||
|
||||
void draw_subdiv_edge( IplImage* img, CvSubdiv2DEdge edge, CvScalar color )
|
||||
static void draw_subdiv_edge( IplImage* img, CvSubdiv2DEdge edge, CvScalar color )
|
||||
{
|
||||
CvSubdiv2DPoint* org_pt;
|
||||
CvSubdiv2DPoint* dst_pt;
|
||||
@@ -59,7 +59,7 @@ void draw_subdiv_edge( IplImage* img, CvSubdiv2DEdge edge, CvScalar color )
|
||||
}
|
||||
|
||||
|
||||
void draw_subdiv( IplImage* img, CvSubdiv2D* subdiv,
|
||||
static void draw_subdiv( IplImage* img, CvSubdiv2D* subdiv,
|
||||
CvScalar delaunay_color, CvScalar voronoi_color )
|
||||
{
|
||||
CvSeqReader reader;
|
||||
@@ -83,7 +83,7 @@ void draw_subdiv( IplImage* img, CvSubdiv2D* subdiv,
|
||||
}
|
||||
|
||||
|
||||
void locate_point( CvSubdiv2D* subdiv, CvPoint2D32f fp, IplImage* img,
|
||||
static void locate_point( CvSubdiv2D* subdiv, CvPoint2D32f fp, IplImage* img,
|
||||
CvScalar active_color )
|
||||
{
|
||||
CvSubdiv2DEdge e;
|
||||
@@ -107,7 +107,7 @@ void locate_point( CvSubdiv2D* subdiv, CvPoint2D32f fp, IplImage* img,
|
||||
}
|
||||
|
||||
|
||||
void draw_subdiv_facet( IplImage* img, CvSubdiv2DEdge edge )
|
||||
static void draw_subdiv_facet( IplImage* img, CvSubdiv2DEdge edge )
|
||||
{
|
||||
CvSubdiv2DEdge t = edge;
|
||||
int i, count = 0;
|
||||
@@ -142,7 +142,7 @@ void draw_subdiv_facet( IplImage* img, CvSubdiv2DEdge edge )
|
||||
free( buf );
|
||||
}
|
||||
|
||||
void paint_voronoi( CvSubdiv2D* subdiv, IplImage* img )
|
||||
static void paint_voronoi( CvSubdiv2D* subdiv, IplImage* img )
|
||||
{
|
||||
CvSeqReader reader;
|
||||
int i, total = subdiv->edges->total;
|
||||
@@ -171,7 +171,7 @@ void paint_voronoi( CvSubdiv2D* subdiv, IplImage* img )
|
||||
}
|
||||
|
||||
|
||||
void run(void)
|
||||
static void run(void)
|
||||
{
|
||||
char win[] = "source";
|
||||
int i;
|
||||
|
@@ -8,7 +8,7 @@
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
cout << "\nThis program demonstrates the cascade recognizer. Now you can use Haar or LBP features.\n"
|
||||
"This classifier can recognize many ~rigid objects, it's most known use is for faces.\n"
|
||||
|
@@ -2,16 +2,18 @@
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "opencv2/imgproc/imgproc_c.h"
|
||||
#include <stdio.h>
|
||||
void help()
|
||||
|
||||
static void help(void)
|
||||
{
|
||||
printf(
|
||||
printf(
|
||||
"\n This program demonstrate dense \"Farneback\n optical flow\n"
|
||||
"It read from camera 0, and shows how to use and display dense Franeback optical flow\n"
|
||||
"It read from camera 0, and shows how to use and display dense Franeback optical flow\n"
|
||||
"Usage: \n"
|
||||
"./fback_c \n");
|
||||
|
||||
}
|
||||
void drawOptFlowMap(const CvMat* flow, CvMat* cflowmap, int step,
|
||||
|
||||
static void drawOptFlowMap(const CvMat* flow, CvMat* cflowmap, int step,
|
||||
double scale, CvScalar color)
|
||||
{
|
||||
int x, y;
|
||||
@@ -25,7 +27,7 @@ void drawOptFlowMap(const CvMat* flow, CvMat* cflowmap, int step,
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
CvCapture* capture = cvCreateCameraCapture(0);
|
||||
CvMat* prevgray = 0, *gray = 0, *flow = 0, *cflow = 0;
|
||||
@@ -34,9 +36,9 @@ int main()
|
||||
|
||||
if( !capture )
|
||||
return -1;
|
||||
|
||||
|
||||
cvNamedWindow("flow", 1);
|
||||
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int firstFrame = gray == 0;
|
||||
@@ -51,7 +53,7 @@ int main()
|
||||
cflow = cvCreateMat(gray->rows, gray->cols, CV_8UC3);
|
||||
}
|
||||
cvCvtColor(frame, gray, CV_BGR2GRAY);
|
||||
|
||||
|
||||
if( !firstFrame )
|
||||
{
|
||||
cvCalcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
|
||||
@@ -62,7 +64,7 @@ int main()
|
||||
if(cvWaitKey(30)>=0)
|
||||
break;
|
||||
{
|
||||
CvMat* temp;
|
||||
CvMat* temp;
|
||||
CV_SWAP(prevgray, gray, temp);
|
||||
}
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace std;
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
printf(
|
||||
"This program demonstrated the use of the SURF Detector and Descriptor using\n"
|
||||
@@ -35,87 +35,8 @@ void help()
|
||||
|
||||
IplImage* image = 0;
|
||||
|
||||
double
|
||||
compareSURFDescriptors( const float* d1, const float* d2, double best, int length )
|
||||
{
|
||||
double total_cost = 0;
|
||||
assert( length % 4 == 0 );
|
||||
for( int i = 0; i < length; i += 4 )
|
||||
{
|
||||
double t0 = d1[i ] - d2[i ];
|
||||
double t1 = d1[i+1] - d2[i+1];
|
||||
double t2 = d1[i+2] - d2[i+2];
|
||||
double t3 = d1[i+3] - d2[i+3];
|
||||
total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3;
|
||||
if( total_cost > best )
|
||||
break;
|
||||
}
|
||||
return total_cost;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
naiveNearestNeighbor( const float* vec, int laplacian,
|
||||
const CvSeq* model_keypoints,
|
||||
const CvSeq* model_descriptors )
|
||||
{
|
||||
int length = (int)(model_descriptors->elem_size/sizeof(float));
|
||||
int i, neighbor = -1;
|
||||
double d, dist1 = 1e6, dist2 = 1e6;
|
||||
CvSeqReader reader, kreader;
|
||||
cvStartReadSeq( model_keypoints, &kreader, 0 );
|
||||
cvStartReadSeq( model_descriptors, &reader, 0 );
|
||||
|
||||
for( i = 0; i < model_descriptors->total; i++ )
|
||||
{
|
||||
const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
|
||||
const float* mvec = (const float*)reader.ptr;
|
||||
CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
|
||||
CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
|
||||
if( laplacian != kp->laplacian )
|
||||
continue;
|
||||
d = compareSURFDescriptors( vec, mvec, dist2, length );
|
||||
if( d < dist1 )
|
||||
{
|
||||
dist2 = dist1;
|
||||
dist1 = d;
|
||||
neighbor = i;
|
||||
}
|
||||
else if ( d < dist2 )
|
||||
dist2 = d;
|
||||
}
|
||||
if ( dist1 < 0.6*dist2 )
|
||||
return neighbor;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
|
||||
const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, vector<int>& ptpairs )
|
||||
{
|
||||
int i;
|
||||
CvSeqReader reader, kreader;
|
||||
cvStartReadSeq( objectKeypoints, &kreader );
|
||||
cvStartReadSeq( objectDescriptors, &reader );
|
||||
ptpairs.clear();
|
||||
|
||||
for( i = 0; i < objectDescriptors->total; i++ )
|
||||
{
|
||||
const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
|
||||
const float* descriptor = (const float*)reader.ptr;
|
||||
CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
|
||||
CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
|
||||
int nearest_neighbor = naiveNearestNeighbor( descriptor, kp->laplacian, imageKeypoints, imageDescriptors );
|
||||
if( nearest_neighbor >= 0 )
|
||||
{
|
||||
ptpairs.push_back(i);
|
||||
ptpairs.push_back(nearest_neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
#ifdef USE_FLANN
|
||||
static void
|
||||
flannFindPairs( const CvSeq*, const CvSeq* objectDescriptors,
|
||||
const CvSeq*, const CvSeq* imageDescriptors, vector<int>& ptpairs )
|
||||
{
|
||||
@@ -162,10 +83,89 @@ flannFindPairs( const CvSeq*, const CvSeq* objectDescriptors,
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
static double
|
||||
compareSURFDescriptors( const float* d1, const float* d2, double best, int length )
|
||||
{
|
||||
double total_cost = 0;
|
||||
assert( length % 4 == 0 );
|
||||
for( int i = 0; i < length; i += 4 )
|
||||
{
|
||||
double t0 = d1[i ] - d2[i ];
|
||||
double t1 = d1[i+1] - d2[i+1];
|
||||
double t2 = d1[i+2] - d2[i+2];
|
||||
double t3 = d1[i+3] - d2[i+3];
|
||||
total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3;
|
||||
if( total_cost > best )
|
||||
break;
|
||||
}
|
||||
return total_cost;
|
||||
}
|
||||
|
||||
static int
|
||||
naiveNearestNeighbor( const float* vec, int laplacian,
|
||||
const CvSeq* model_keypoints,
|
||||
const CvSeq* model_descriptors )
|
||||
{
|
||||
int length = (int)(model_descriptors->elem_size/sizeof(float));
|
||||
int i, neighbor = -1;
|
||||
double d, dist1 = 1e6, dist2 = 1e6;
|
||||
CvSeqReader reader, kreader;
|
||||
cvStartReadSeq( model_keypoints, &kreader, 0 );
|
||||
cvStartReadSeq( model_descriptors, &reader, 0 );
|
||||
|
||||
for( i = 0; i < model_descriptors->total; i++ )
|
||||
{
|
||||
const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
|
||||
const float* mvec = (const float*)reader.ptr;
|
||||
CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
|
||||
CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
|
||||
if( laplacian != kp->laplacian )
|
||||
continue;
|
||||
d = compareSURFDescriptors( vec, mvec, dist2, length );
|
||||
if( d < dist1 )
|
||||
{
|
||||
dist2 = dist1;
|
||||
dist1 = d;
|
||||
neighbor = i;
|
||||
}
|
||||
else if ( d < dist2 )
|
||||
dist2 = d;
|
||||
}
|
||||
if ( dist1 < 0.6*dist2 )
|
||||
return neighbor;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void
|
||||
findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
|
||||
const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, vector<int>& ptpairs )
|
||||
{
|
||||
int i;
|
||||
CvSeqReader reader, kreader;
|
||||
cvStartReadSeq( objectKeypoints, &kreader );
|
||||
cvStartReadSeq( objectDescriptors, &reader );
|
||||
ptpairs.clear();
|
||||
|
||||
for( i = 0; i < objectDescriptors->total; i++ )
|
||||
{
|
||||
const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
|
||||
const float* descriptor = (const float*)reader.ptr;
|
||||
CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
|
||||
CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
|
||||
int nearest_neighbor = naiveNearestNeighbor( descriptor, kp->laplacian, imageKeypoints, imageDescriptors );
|
||||
if( nearest_neighbor >= 0 )
|
||||
{
|
||||
ptpairs.push_back(i);
|
||||
ptpairs.push_back(nearest_neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* a rough implementation for object location */
|
||||
int
|
||||
static int
|
||||
locatePlanarObject( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
|
||||
const CvSeq* imageKeypoints, const CvSeq* imageDescriptors,
|
||||
const CvPoint src_corners[4], CvPoint dst_corners[4] )
|
||||
|
@@ -11,7 +11,7 @@
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
cout << "This program shows the use of the Calonder point descriptor classifier"
|
||||
"SURF is used to detect interest points, Calonder is used to describe/match these points\n"
|
||||
@@ -28,7 +28,7 @@ void help()
|
||||
/*
|
||||
* Generates random perspective transform of image
|
||||
*/
|
||||
void warpPerspectiveRand( const Mat& src, Mat& dst, Mat& H, RNG& rng )
|
||||
static void warpPerspectiveRand( const Mat& src, Mat& dst, Mat& H, RNG& rng )
|
||||
{
|
||||
H.create(3, 3, CV_32FC1);
|
||||
H.at<float>(0,0) = rng.uniform( 0.8f, 1.2f);
|
||||
@@ -51,7 +51,7 @@ void warpPerspectiveRand( const Mat& src, Mat& dst, Mat& H, RNG& rng )
|
||||
*
|
||||
* To train Calonder classifier RTreeClassifier class need to be used.
|
||||
*/
|
||||
void trainCalonderClassifier( const string& classifierFilename, const string& imgFilename )
|
||||
static void trainCalonderClassifier( const string& classifierFilename, const string& imgFilename )
|
||||
{
|
||||
// Reads train images
|
||||
ifstream is( imgFilename.c_str(), ifstream::in );
|
||||
@@ -104,7 +104,7 @@ void trainCalonderClassifier( const string& classifierFilename, const string& im
|
||||
* but it is convenient to use CalonderDescriptorExtractor class which is wrapper of
|
||||
* RTreeClassifier.
|
||||
*/
|
||||
void testCalonderClassifier( const string& classifierFilename, const string& imgFilename )
|
||||
static void testCalonderClassifier( const string& classifierFilename, const string& imgFilename )
|
||||
{
|
||||
Mat img1 = imread( imgFilename, CV_LOAD_IMAGE_GRAYSCALE ), img2, H12;
|
||||
if( img1.empty() )
|
||||
|
@@ -11,7 +11,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace cv;
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
printf( "This program shows the use of the \"fern\" plannar PlanarObjectDetector point\n"
|
||||
"descriptor classifier\n"
|
||||
|
@@ -2,8 +2,8 @@
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef HAVE_CVCONFIG_H
|
||||
#include <cvconfig.h>
|
||||
#ifdef HAVE_CVCONFIG_H
|
||||
#include <cvconfig.h>
|
||||
#endif
|
||||
#ifdef HAVE_TBB
|
||||
#include "tbb/task_scheduler_init.h"
|
||||
@@ -11,44 +11,44 @@
|
||||
|
||||
using namespace cv;
|
||||
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
printf( "This program demonstrated the use of the latentSVM detector.\n"
|
||||
"It reads in a trained object model and then uses that to detect the object in an image\n"
|
||||
"Call:\n"
|
||||
printf( "This program demonstrated the use of the latentSVM detector.\n"
|
||||
"It reads in a trained object model and then uses that to detect the object in an image\n"
|
||||
"Call:\n"
|
||||
"./latentsvmdetect [<image_filename> <model_filename> [<threads_number>]]\n"
|
||||
" The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"
|
||||
" Press any key to quit.\n");
|
||||
" The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"
|
||||
" Press any key to quit.\n");
|
||||
}
|
||||
|
||||
const char* model_filename = "cat.xml";
|
||||
const char* image_filename = "cat.jpg";
|
||||
int tbbNumThreads = -1;
|
||||
|
||||
void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)
|
||||
static void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)
|
||||
{
|
||||
CvMemStorage* storage = cvCreateMemStorage(0);
|
||||
CvSeq* detections = 0;
|
||||
int i = 0;
|
||||
int64 start = 0, finish = 0;
|
||||
int64 start = 0, finish = 0;
|
||||
#ifdef HAVE_TBB
|
||||
tbb::task_scheduler_init init(tbb::task_scheduler_init::deferred);
|
||||
if (numThreads > 0)
|
||||
{
|
||||
init.initialize(numThreads);
|
||||
if (numThreads > 0)
|
||||
{
|
||||
init.initialize(numThreads);
|
||||
printf("Number of threads %i\n", numThreads);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Number of threads is not correct for TBB version");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Number of threads is not correct for TBB version");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
start = cvGetTickCount();
|
||||
start = cvGetTickCount();
|
||||
detections = cvLatentSvmDetectObjects(image, detector, storage, 0.5f, numThreads);
|
||||
finish = cvGetTickCount();
|
||||
printf("detection time = %.3f\n", (float)(finish - start) / (float)(cvGetTickFrequency() * 1000000.0));
|
||||
finish = cvGetTickCount();
|
||||
printf("detection time = %.3f\n", (float)(finish - start) / (float)(cvGetTickFrequency() * 1000000.0));
|
||||
|
||||
#ifdef HAVE_TBB
|
||||
init.terminate();
|
||||
@@ -56,10 +56,10 @@ void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, in
|
||||
for( i = 0; i < detections->total; i++ )
|
||||
{
|
||||
CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, i );
|
||||
CvRect bounding_box = detection.rect;
|
||||
CvRect bounding_box = detection.rect;
|
||||
cvRectangle( image, cvPoint(bounding_box.x, bounding_box.y),
|
||||
cvPoint(bounding_box.x + bounding_box.width,
|
||||
bounding_box.y + bounding_box.height),
|
||||
cvPoint(bounding_box.x + bounding_box.width,
|
||||
bounding_box.y + bounding_box.height),
|
||||
CV_RGB(255,0,0), 3 );
|
||||
}
|
||||
cvReleaseMemStorage( &storage );
|
||||
@@ -67,31 +67,31 @@ void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, in
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
help();
|
||||
if (argc > 2)
|
||||
{
|
||||
image_filename = argv[1];
|
||||
model_filename = argv[2];
|
||||
help();
|
||||
if (argc > 2)
|
||||
{
|
||||
image_filename = argv[1];
|
||||
model_filename = argv[2];
|
||||
if (argc > 3)
|
||||
{
|
||||
tbbNumThreads = atoi(argv[3]);
|
||||
}
|
||||
}
|
||||
IplImage* image = cvLoadImage(image_filename);
|
||||
if (!image)
|
||||
{
|
||||
printf( "Unable to load the image\n"
|
||||
}
|
||||
IplImage* image = cvLoadImage(image_filename);
|
||||
if (!image)
|
||||
{
|
||||
printf( "Unable to load the image\n"
|
||||
"Pass it as the first parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_filename);
|
||||
if (!detector)
|
||||
{
|
||||
printf( "Unable to load the model\n"
|
||||
if (!detector)
|
||||
{
|
||||
printf( "Unable to load the model\n"
|
||||
"Pass it as the second parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
|
||||
cvReleaseImage( &image );
|
||||
return -1;
|
||||
}
|
||||
cvReleaseImage( &image );
|
||||
return -1;
|
||||
}
|
||||
detect_and_draw_objects( image, detector, tbbNumThreads );
|
||||
cvNamedWindow( "test", 0 );
|
||||
cvShowImage( "test", image );
|
||||
@@ -99,6 +99,6 @@ int main(int argc, char* argv[])
|
||||
cvReleaseLatentSvmDetector( &detector );
|
||||
cvReleaseImage( &image );
|
||||
cvDestroyAllWindows();
|
||||
|
||||
return 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@ int open_close_pos = 0;
|
||||
int erode_dilate_pos = 0;
|
||||
|
||||
// callback function for open/close trackbar
|
||||
void OpenClose(int pos)
|
||||
static void OpenClose(int pos)
|
||||
{
|
||||
int n = open_close_pos - max_iters;
|
||||
int an = n > 0 ? n : -n;
|
||||
@@ -35,7 +35,7 @@ void OpenClose(int pos)
|
||||
}
|
||||
|
||||
// callback function for erode/dilate trackbar
|
||||
void ErodeDilate(int pos)
|
||||
static void ErodeDilate(int pos)
|
||||
{
|
||||
int n = erode_dilate_pos - max_iters;
|
||||
int an = n > 0 ? n : -n;
|
||||
@@ -52,7 +52,7 @@ void ErodeDilate(int pos)
|
||||
cvShowImage("Erode/Dilate",dst);
|
||||
}
|
||||
|
||||
void help()
|
||||
static void help(void)
|
||||
{
|
||||
printf( "This program demonstrated the use of the morphology operator, especially open, close, erode, dilate operations\n"
|
||||
"Morphology operators are built on max (close) and min (open) operators as measured by pixels covered by small structuring elements.\n"
|
||||
|
@@ -5,7 +5,7 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
void help()
|
||||
static void help(void)
|
||||
{
|
||||
printf(
|
||||
"\nThis program demonstrated the use of motion templates -- basically using the gradients\n"
|
||||
@@ -39,7 +39,7 @@ CvMemStorage* storage = 0; // temporary storage
|
||||
// img - input video frame
|
||||
// dst - resultant motion picture
|
||||
// args - optional parameters
|
||||
void update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
|
||||
static void update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
|
||||
{
|
||||
double timestamp = (double)clock()/CLOCKS_PER_SEC; // get current time in seconds
|
||||
CvSize size = cvSize(img->width,img->height); // get current frame size
|
||||
|
@@ -12,15 +12,15 @@
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
cout << "\nThis program demonstrates the Maximal Extremal Region interest point detector.\n"
|
||||
cout << "\nThis program demonstrates the Maximal Extremal Region interest point detector.\n"
|
||||
"It finds the most stable (in size) dark and white regions as a threshold is increased.\n"
|
||||
"\nCall:\n"
|
||||
"./mser_sample <path_and_image_filename, Default is 'puzzle.png'>\n\n";
|
||||
}
|
||||
|
||||
static const Vec3b bcolors[] =
|
||||
static const Vec3b bcolors[] =
|
||||
{
|
||||
Vec3b(0,0,255),
|
||||
Vec3b(0,128,255),
|
||||
@@ -35,10 +35,10 @@ static const Vec3b bcolors[] =
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
string path;
|
||||
Mat img0, img, yuv, gray, ellipses;
|
||||
help();
|
||||
|
||||
string path;
|
||||
Mat img0, img, yuv, gray, ellipses;
|
||||
help();
|
||||
|
||||
img0 = imread( argc != 2 ? "puzzle.png" : argv[1], 1 );
|
||||
if( img0.empty() )
|
||||
{
|
||||
@@ -48,39 +48,39 @@ int main( int argc, char** argv )
|
||||
cout << "Unable to load image " << argv[1] << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
cvtColor(img0, yuv, COLOR_BGR2YCrCb);
|
||||
|
||||
cvtColor(img0, yuv, COLOR_BGR2YCrCb);
|
||||
cvtColor(img0, gray, COLOR_BGR2GRAY);
|
||||
cvtColor(gray, img, COLOR_GRAY2BGR);
|
||||
img.copyTo(ellipses);
|
||||
|
||||
|
||||
vector<vector<Point> > contours;
|
||||
double t = (double)getTickCount();
|
||||
double t = (double)getTickCount();
|
||||
MSER()(yuv, contours);
|
||||
t = (double)getTickCount() - t;
|
||||
printf( "MSER extracted %d contours in %g ms.\n", (int)contours.size(),
|
||||
t = (double)getTickCount() - t;
|
||||
printf( "MSER extracted %d contours in %g ms.\n", (int)contours.size(),
|
||||
t*1000./getTickFrequency() );
|
||||
|
||||
// draw mser's with different colors
|
||||
for( int i = (int)contours.size()-1; i >= 0; i-- )
|
||||
{
|
||||
const vector<Point>& r = contours[i];
|
||||
for ( int j = 0; j < (int)r.size(); j++ )
|
||||
{
|
||||
Point pt = r[j];
|
||||
|
||||
// draw mser's with different colors
|
||||
for( int i = (int)contours.size()-1; i >= 0; i-- )
|
||||
{
|
||||
const vector<Point>& r = contours[i];
|
||||
for ( int j = 0; j < (int)r.size(); j++ )
|
||||
{
|
||||
Point pt = r[j];
|
||||
img.at<Vec3b>(pt) = bcolors[i%9];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// find ellipse (it seems cvfitellipse2 have error or sth?)
|
||||
RotatedRect box = fitEllipse( r );
|
||||
|
||||
|
||||
box.angle=(float)CV_PI/2-box.angle;
|
||||
ellipse( ellipses, box, Scalar(196,255,255), 2 );
|
||||
}
|
||||
|
||||
imshow( "original", img0 );
|
||||
imshow( "response", img );
|
||||
imshow( "ellipses", ellipses );
|
||||
|
||||
waitKey(0);
|
||||
}
|
||||
|
||||
imshow( "original", img0 );
|
||||
imshow( "response", img );
|
||||
imshow( "ellipses", ellipses );
|
||||
|
||||
waitKey(0);
|
||||
}
|
||||
|
@@ -2,9 +2,9 @@
|
||||
#include "opencv2/ml/ml.hpp"
|
||||
#include <stdio.h>
|
||||
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
printf("\nThis program demonstrated the use of OpenCV's decision tree function for learning and predicting data\n"
|
||||
printf("\nThis program demonstrated the use of OpenCV's decision tree function for learning and predicting data\n"
|
||||
"Usage :\n"
|
||||
"./mushroom <path to agaricus-lepiota.data>\n"
|
||||
"\n"
|
||||
@@ -21,7 +21,7 @@ void help()
|
||||
"// the values are encoded by characters.\n\n");
|
||||
}
|
||||
|
||||
int mushroom_read_database( const char* filename, CvMat** data, CvMat** missing, CvMat** responses )
|
||||
static int mushroom_read_database( const char* filename, CvMat** data, CvMat** missing, CvMat** responses )
|
||||
{
|
||||
const int M = 1024;
|
||||
FILE* f = fopen( filename, "rt" );
|
||||
@@ -95,7 +95,7 @@ int mushroom_read_database( const char* filename, CvMat** data, CvMat** missing,
|
||||
}
|
||||
|
||||
|
||||
CvDTree* mushroom_create_dtree( const CvMat* data, const CvMat* missing,
|
||||
static CvDTree* mushroom_create_dtree( const CvMat* data, const CvMat* missing,
|
||||
const CvMat* responses, float p_weight )
|
||||
{
|
||||
CvDTree* dtree;
|
||||
@@ -107,7 +107,7 @@ CvDTree* mushroom_create_dtree( const CvMat* data, const CvMat* missing,
|
||||
cvSet( var_type, cvScalarAll(CV_VAR_CATEGORICAL) ); // all the variables are categorical
|
||||
|
||||
dtree = new CvDTree;
|
||||
|
||||
|
||||
dtree->train( data, CV_ROW_SAMPLE, responses, 0, 0, var_type, missing,
|
||||
CvDTreeParams( 8, // max depth
|
||||
10, // min sample count
|
||||
@@ -179,7 +179,7 @@ static const char* var_desc[] =
|
||||
};
|
||||
|
||||
|
||||
void print_variable_importance( CvDTree* dtree, const char** var_desc )
|
||||
static void print_variable_importance( CvDTree* dtree, const char** var_desc )
|
||||
{
|
||||
const CvMat* var_importance = dtree->get_var_importance();
|
||||
int i;
|
||||
@@ -215,7 +215,7 @@ void print_variable_importance( CvDTree* dtree, const char** var_desc )
|
||||
}
|
||||
}
|
||||
|
||||
void interactive_classification( CvDTree* dtree, const char** var_desc )
|
||||
static void interactive_classification( CvDTree* dtree, const char** var_desc )
|
||||
{
|
||||
char input[1000];
|
||||
const CvDTreeNode* root;
|
||||
@@ -230,14 +230,14 @@ void interactive_classification( CvDTree* dtree, const char** var_desc )
|
||||
for(;;)
|
||||
{
|
||||
const CvDTreeNode* node;
|
||||
|
||||
|
||||
printf( "Start/Proceed with interactive mushroom classification (y/n): " );
|
||||
int values_read = scanf( "%1s", input );
|
||||
CV_Assert(values_read == 1);
|
||||
|
||||
if( input[0] != 'y' && input[0] != 'Y' )
|
||||
break;
|
||||
printf( "Enter 1-letter answers, '?' for missing/unknown value...\n" );
|
||||
printf( "Enter 1-letter answers, '?' for missing/unknown value...\n" );
|
||||
|
||||
// custom version of predict
|
||||
node = root;
|
||||
@@ -245,7 +245,7 @@ void interactive_classification( CvDTree* dtree, const char** var_desc )
|
||||
{
|
||||
CvDTreeSplit* split = node->split;
|
||||
int dir = 0;
|
||||
|
||||
|
||||
if( !node->left || node->Tn <= dtree->get_pruned_tree_idx() || !node->split )
|
||||
break;
|
||||
|
||||
@@ -279,7 +279,7 @@ void interactive_classification( CvDTree* dtree, const char** var_desc )
|
||||
else
|
||||
printf( "Error: unrecognized value\n" );
|
||||
}
|
||||
|
||||
|
||||
if( !dir )
|
||||
{
|
||||
printf( "Impossible to classify the sample\n");
|
||||
|
@@ -18,7 +18,7 @@
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
printf("\nThis program demonstrates the one way interest point descriptor found in features2d.hpp\n"
|
||||
"Correspondences are drawn\n");
|
||||
|
@@ -6,7 +6,7 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void help()
|
||||
static void help( void )
|
||||
{
|
||||
printf("\nThis program illustrates Linear-Polar and Log-Polar image transforms\n"
|
||||
"Usage :\n"
|
||||
|
@@ -4,7 +4,7 @@
|
||||
#include "opencv2/legacy/legacy.hpp"
|
||||
#include <stdio.h>
|
||||
|
||||
void help()
|
||||
static void help(void)
|
||||
{
|
||||
printf("\nThis program demonstrated color pyramid segmentation cvcvPyrSegmentation() which is controlled\n"
|
||||
"by two trhesholds which can be manipulated by a trackbar. It can take an image file name or defaults to 'fruits.jpg'\n"
|
||||
@@ -32,7 +32,7 @@ CvMemStorage *storage;
|
||||
|
||||
CvPoint pt1, pt2;
|
||||
|
||||
void ON_SEGMENT(int a)
|
||||
static void ON_SEGMENT(int a)
|
||||
{
|
||||
cvPyrSegmentation(image0, image1, storage, &comp,
|
||||
level, threshold1+1, threshold2+1);
|
||||
|
@@ -3,23 +3,23 @@
|
||||
#include <stdio.h>
|
||||
#include <map>
|
||||
|
||||
void help()
|
||||
static void help()
|
||||
{
|
||||
printf(
|
||||
"\nThis sample demonstrates how to use different decision trees and forests including boosting and random trees:\n"
|
||||
"CvDTree dtree;\n"
|
||||
"CvBoost boost;\n"
|
||||
"CvRTrees rtrees;\n"
|
||||
"CvERTrees ertrees;\n"
|
||||
"CvGBTrees gbtrees;\n"
|
||||
"Call:\n\t./tree_engine [-r <response_column>] [-c] <csv filename>\n"
|
||||
printf(
|
||||
"\nThis sample demonstrates how to use different decision trees and forests including boosting and random trees:\n"
|
||||
"CvDTree dtree;\n"
|
||||
"CvBoost boost;\n"
|
||||
"CvRTrees rtrees;\n"
|
||||
"CvERTrees ertrees;\n"
|
||||
"CvGBTrees gbtrees;\n"
|
||||
"Call:\n\t./tree_engine [-r <response_column>] [-c] <csv filename>\n"
|
||||
"where -r <response_column> specified the 0-based index of the response (0 by default)\n"
|
||||
"-c specifies that the response is categorical (it's ordered by default) and\n"
|
||||
"<csv filename> is the name of training data file in comma-separated value format\n\n");
|
||||
}
|
||||
|
||||
|
||||
int count_classes(CvMLData& data)
|
||||
static int count_classes(CvMLData& data)
|
||||
{
|
||||
cv::Mat r(data.get_responses());
|
||||
std::map<int, int> rmap;
|
||||
@@ -30,26 +30,26 @@ int count_classes(CvMLData& data)
|
||||
int ival = cvRound(val);
|
||||
if( ival != val )
|
||||
return -1;
|
||||
rmap[ival] = 1;
|
||||
rmap[ival] = 1;
|
||||
}
|
||||
return (int)rmap.size();
|
||||
}
|
||||
|
||||
void print_result(float train_err, float test_err, const CvMat* _var_imp)
|
||||
static void print_result(float train_err, float test_err, const CvMat* _var_imp)
|
||||
{
|
||||
printf( "train error %f\n", train_err );
|
||||
printf( "test error %f\n\n", test_err );
|
||||
|
||||
|
||||
if (_var_imp)
|
||||
{
|
||||
cv::Mat var_imp(_var_imp), sorted_idx;
|
||||
cv::sortIdx(var_imp, sorted_idx, CV_SORT_EVERY_ROW + CV_SORT_DESCENDING);
|
||||
|
||||
|
||||
printf( "variable importance:\n" );
|
||||
int i, n = (int)var_imp.total();
|
||||
int type = var_imp.type();
|
||||
CV_Assert(type == CV_32F || type == CV_64F);
|
||||
|
||||
|
||||
for( i = 0; i < n; i++)
|
||||
{
|
||||
int k = sorted_idx.at<int>(i);
|
||||
@@ -69,7 +69,7 @@ int main(int argc, char** argv)
|
||||
const char* filename = 0;
|
||||
int response_idx = 0;
|
||||
bool categorical_response = false;
|
||||
|
||||
|
||||
for(int i = 1; i < argc; i++)
|
||||
{
|
||||
if(strcmp(argv[i], "-r") == 0)
|
||||
@@ -85,26 +85,26 @@ int main(int argc, char** argv)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
printf("\nReading in %s...\n\n",filename);
|
||||
CvDTree dtree;
|
||||
CvBoost boost;
|
||||
CvRTrees rtrees;
|
||||
CvERTrees ertrees;
|
||||
CvGBTrees gbtrees;
|
||||
CvGBTrees gbtrees;
|
||||
|
||||
CvMLData data;
|
||||
|
||||
|
||||
|
||||
CvTrainTestSplit spl( 0.5f );
|
||||
|
||||
|
||||
if ( data.read_csv( filename ) == 0)
|
||||
{
|
||||
data.set_response_idx( response_idx );
|
||||
if(categorical_response)
|
||||
data.change_var_type( response_idx, CV_VAR_CATEGORICAL );
|
||||
data.set_train_test_split( &spl );
|
||||
|
||||
|
||||
printf("======DTREE=====\n");
|
||||
dtree.train( &data, CvDTreeParams( 10, 2, 0, false, 16, 0, false, false, 0 ));
|
||||
print_result( dtree.calc_error( &data, CV_TRAIN_ERROR), dtree.calc_error( &data, CV_TEST_ERROR ), dtree.get_var_importance() );
|
||||
@@ -125,10 +125,10 @@ int main(int argc, char** argv)
|
||||
print_result( ertrees.calc_error( &data, CV_TRAIN_ERROR), ertrees.calc_error( &data, CV_TEST_ERROR ), ertrees.get_var_importance() );
|
||||
|
||||
printf("======GBTREES=====\n");
|
||||
if (categorical_response)
|
||||
gbtrees.train( &data, CvGBTreesParams(CvGBTrees::DEVIANCE_LOSS, 100, 0.1f, 0.8f, 5, false));
|
||||
else
|
||||
gbtrees.train( &data, CvGBTreesParams(CvGBTrees::SQUARED_LOSS, 100, 0.1f, 0.8f, 5, false));
|
||||
if (categorical_response)
|
||||
gbtrees.train( &data, CvGBTreesParams(CvGBTrees::DEVIANCE_LOSS, 100, 0.1f, 0.8f, 5, false));
|
||||
else
|
||||
gbtrees.train( &data, CvGBTreesParams(CvGBTrees::SQUARED_LOSS, 100, 0.1f, 0.8f, 5, false));
|
||||
print_result( gbtrees.calc_error( &data, CV_TRAIN_ERROR), gbtrees.calc_error( &data, CV_TEST_ERROR ), 0 ); //doesn't compute importance
|
||||
}
|
||||
else
|
||||
|
Reference in New Issue
Block a user