2010-10-09 13:36:06 +02:00
|
|
|
#include "opencv2/objdetect/objdetect.hpp"
|
2010-11-26 18:59:40 +01:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2010-10-09 13:36:06 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-06-15 15:04:17 +02:00
|
|
|
#ifdef HAVE_CVCONFIG_H
|
|
|
|
#include <cvconfig.h>
|
2011-02-08 08:34:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TBB
|
|
|
|
#include "tbb/task_scheduler_init.h"
|
|
|
|
#endif
|
|
|
|
|
2010-10-09 13:36:06 +02:00
|
|
|
using namespace cv;
|
|
|
|
|
2012-06-15 15:04:17 +02:00
|
|
|
static void help()
|
2010-12-03 00:42:46 +01:00
|
|
|
{
|
2012-06-15 15:04:17 +02:00
|
|
|
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"
|
2011-06-09 14:01:47 +02:00
|
|
|
"./latentsvmdetect [<image_filename> <model_filename> [<threads_number>]]\n"
|
2012-06-15 15:04:17 +02:00
|
|
|
" The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"
|
|
|
|
" Press any key to quit.\n");
|
2010-12-03 00:42:46 +01:00
|
|
|
}
|
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
const char* model_filename = "cat.xml";
|
|
|
|
const char* image_filename = "cat.jpg";
|
|
|
|
int tbbNumThreads = -1;
|
2010-10-09 13:36:06 +02:00
|
|
|
|
2012-06-15 15:04:17 +02:00
|
|
|
static void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)
|
2010-10-09 13:36:06 +02:00
|
|
|
{
|
|
|
|
CvMemStorage* storage = cvCreateMemStorage(0);
|
|
|
|
CvSeq* detections = 0;
|
|
|
|
int i = 0;
|
2012-06-15 15:04:17 +02:00
|
|
|
int64 start = 0, finish = 0;
|
2011-02-08 08:34:25 +01:00
|
|
|
#ifdef HAVE_TBB
|
|
|
|
tbb::task_scheduler_init init(tbb::task_scheduler_init::deferred);
|
2012-06-15 15:04:17 +02:00
|
|
|
if (numThreads > 0)
|
|
|
|
{
|
|
|
|
init.initialize(numThreads);
|
2011-02-08 08:34:25 +01:00
|
|
|
printf("Number of threads %i\n", numThreads);
|
2012-06-15 15:04:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Number of threads is not correct for TBB version");
|
|
|
|
return;
|
|
|
|
}
|
2011-02-08 08:34:25 +01:00
|
|
|
#endif
|
2011-06-09 14:01:47 +02:00
|
|
|
|
2012-06-15 15:04:17 +02:00
|
|
|
start = cvGetTickCount();
|
2011-02-08 08:34:25 +01:00
|
|
|
detections = cvLatentSvmDetectObjects(image, detector, storage, 0.5f, numThreads);
|
2012-06-15 15:04:17 +02:00
|
|
|
finish = cvGetTickCount();
|
|
|
|
printf("detection time = %.3f\n", (float)(finish - start) / (float)(cvGetTickFrequency() * 1000000.0));
|
2010-10-09 13:36:06 +02:00
|
|
|
|
2011-02-08 08:34:25 +01:00
|
|
|
#ifdef HAVE_TBB
|
|
|
|
init.terminate();
|
|
|
|
#endif
|
2010-10-09 13:36:06 +02:00
|
|
|
for( i = 0; i < detections->total; i++ )
|
|
|
|
{
|
|
|
|
CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, i );
|
2012-06-15 15:04:17 +02:00
|
|
|
CvRect bounding_box = detection.rect;
|
2010-10-09 13:36:06 +02:00
|
|
|
cvRectangle( image, cvPoint(bounding_box.x, bounding_box.y),
|
2012-06-15 15:04:17 +02:00
|
|
|
cvPoint(bounding_box.x + bounding_box.width,
|
|
|
|
bounding_box.y + bounding_box.height),
|
2010-10-09 13:36:06 +02:00
|
|
|
CV_RGB(255,0,0), 3 );
|
|
|
|
}
|
|
|
|
cvReleaseMemStorage( &storage );
|
|
|
|
}
|
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
int main(int argc, char* argv[])
|
2010-10-09 13:36:06 +02:00
|
|
|
{
|
2012-06-15 15:04:17 +02:00
|
|
|
help();
|
|
|
|
if (argc > 2)
|
|
|
|
{
|
|
|
|
image_filename = argv[1];
|
|
|
|
model_filename = argv[2];
|
2011-06-09 14:01:47 +02:00
|
|
|
if (argc > 3)
|
|
|
|
{
|
|
|
|
tbbNumThreads = atoi(argv[3]);
|
|
|
|
}
|
2012-06-15 15:04:17 +02:00
|
|
|
}
|
|
|
|
IplImage* image = cvLoadImage(image_filename);
|
|
|
|
if (!image)
|
|
|
|
{
|
|
|
|
printf( "Unable to load the image\n"
|
2010-10-16 09:10:46 +02:00
|
|
|
"Pass it as the first parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
|
2012-06-15 15:04:17 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-06-09 14:01:47 +02:00
|
|
|
CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_filename);
|
2012-06-15 15:04:17 +02:00
|
|
|
if (!detector)
|
|
|
|
{
|
|
|
|
printf( "Unable to load the model\n"
|
2010-10-16 09:10:46 +02:00
|
|
|
"Pass it as the second parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );
|
2012-06-15 15:04:17 +02:00
|
|
|
cvReleaseImage( &image );
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-08 08:34:25 +01:00
|
|
|
detect_and_draw_objects( image, detector, tbbNumThreads );
|
2010-10-09 13:36:06 +02:00
|
|
|
cvNamedWindow( "test", 0 );
|
|
|
|
cvShowImage( "test", image );
|
|
|
|
cvWaitKey(0);
|
|
|
|
cvReleaseLatentSvmDetector( &detector );
|
|
|
|
cvReleaseImage( &image );
|
|
|
|
cvDestroyAllWindows();
|
2012-06-15 15:04:17 +02:00
|
|
|
|
|
|
|
return 0;
|
2010-12-03 00:42:46 +01:00
|
|
|
}
|