2012-08-31 14:08:52 +08:00
|
|
|
#include "opencv2/objdetect/objdetect.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/ocl/ocl.hpp"
|
2013-04-08 02:45:38 +08:00
|
|
|
|
|
|
|
#include "opencv2/highgui/highgui_c.h"
|
|
|
|
|
2012-08-31 14:08:52 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-12-02 15:15:46 +08:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1700)
|
|
|
|
# include <thread>
|
2013-11-22 18:47:58 +08:00
|
|
|
#endif
|
2013-04-12 18:12:12 +08:00
|
|
|
|
2012-08-31 14:08:52 +08:00
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
2013-12-18 04:15:02 +08:00
|
|
|
|
2013-11-19 00:48:00 +08:00
|
|
|
#define LOOP_NUM 1
|
2013-12-18 04:15:02 +08:00
|
|
|
#define MAX_THREADS 10
|
|
|
|
|
2013-05-24 15:52:33 +08:00
|
|
|
|
2013-12-02 15:15:46 +08:00
|
|
|
///////////////////////////single-threading faces detecting///////////////////////////////
|
2013-11-22 18:47:58 +08:00
|
|
|
|
2013-05-24 15:52:33 +08:00
|
|
|
const static Scalar colors[] = { CV_RGB(0,0,255),
|
2013-06-19 16:36:23 +08:00
|
|
|
CV_RGB(0,128,255),
|
|
|
|
CV_RGB(0,255,255),
|
|
|
|
CV_RGB(0,255,0),
|
|
|
|
CV_RGB(255,128,0),
|
|
|
|
CV_RGB(255,255,0),
|
|
|
|
CV_RGB(255,0,0),
|
|
|
|
CV_RGB(255,0,255)
|
|
|
|
} ;
|
|
|
|
|
2012-08-31 14:08:52 +08:00
|
|
|
|
2013-12-18 04:15:02 +08:00
|
|
|
int64 work_begin[MAX_THREADS] = {0};
|
2013-12-18 21:02:39 +08:00
|
|
|
int64 work_total[MAX_THREADS] = {0};
|
2013-12-02 15:15:46 +08:00
|
|
|
string inputName, outputName, cascadeName;
|
2013-05-24 15:52:33 +08:00
|
|
|
|
2013-12-18 04:15:02 +08:00
|
|
|
static void workBegin(int i = 0)
|
2013-06-04 22:31:51 +08:00
|
|
|
{
|
2013-12-18 04:15:02 +08:00
|
|
|
work_begin[i] = getTickCount();
|
2013-05-24 15:52:33 +08:00
|
|
|
}
|
2013-10-22 00:47:55 +08:00
|
|
|
|
2013-12-18 04:15:02 +08:00
|
|
|
static void workEnd(int i = 0)
|
2012-08-31 14:08:52 +08:00
|
|
|
{
|
2013-12-18 21:02:39 +08:00
|
|
|
work_total[i] += (getTickCount() - work_begin[i]);
|
2012-08-31 14:08:52 +08:00
|
|
|
}
|
2013-04-12 18:12:12 +08:00
|
|
|
|
2013-12-18 21:02:39 +08:00
|
|
|
static double getTotalTime(int i = 0)
|
2013-06-19 16:36:23 +08:00
|
|
|
{
|
2013-12-18 21:02:39 +08:00
|
|
|
return work_total[i] /getTickFrequency() * 1000.;
|
2013-05-24 15:52:33 +08:00
|
|
|
}
|
|
|
|
|
2012-08-31 14:08:52 +08:00
|
|
|
|
2013-10-22 00:47:55 +08:00
|
|
|
static void detect( Mat& img, vector<Rect>& faces,
|
2013-07-22 17:53:56 +08:00
|
|
|
ocl::OclCascadeClassifier& cascade,
|
2013-11-19 02:48:04 +08:00
|
|
|
double scale);
|
2013-06-19 16:36:23 +08:00
|
|
|
|
2012-08-31 14:08:52 +08:00
|
|
|
|
2013-10-22 00:47:55 +08:00
|
|
|
static void detectCPU( Mat& img, vector<Rect>& faces,
|
2013-06-19 16:36:23 +08:00
|
|
|
CascadeClassifier& cascade,
|
2013-11-19 02:48:04 +08:00
|
|
|
double scale);
|
2013-05-24 15:52:33 +08:00
|
|
|
|
2013-10-22 00:47:55 +08:00
|
|
|
static void Draw(Mat& img, vector<Rect>& faces, double scale);
|
2013-05-24 15:52:33 +08:00
|
|
|
|
2013-06-19 16:36:23 +08:00
|
|
|
|
2013-05-24 15:52:33 +08:00
|
|
|
// This function test if gpu_rst matches cpu_rst.
|
|
|
|
// If the two vectors are not equal, it will return the difference in vector size
|
|
|
|
// Else if will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels)
|
2013-06-19 16:36:23 +08:00
|
|
|
double checkRectSimilarity(Size sz, vector<Rect>& cpu_rst, vector<Rect>& gpu_rst);
|
|
|
|
|
2013-12-02 15:15:46 +08:00
|
|
|
static int facedetect_one_thread(bool useCPU, double scale )
|
2012-08-31 14:08:52 +08:00
|
|
|
{
|
2013-05-24 15:52:33 +08:00
|
|
|
CvCapture* capture = 0;
|
2013-11-19 00:48:00 +08:00
|
|
|
Mat frame, frameCopy0, frameCopy, image;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-07-22 17:53:56 +08:00
|
|
|
ocl::OclCascadeClassifier cascade;
|
2013-05-24 15:52:33 +08:00
|
|
|
CascadeClassifier cpu_cascade;
|
|
|
|
|
|
|
|
if( !cascade.load( cascadeName ) || !cpu_cascade.load(cascadeName) )
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-12-06 16:12:24 +08:00
|
|
|
cout << "ERROR: Could not load classifier cascade: " << cascadeName << endl;
|
2013-10-22 00:47:55 +08:00
|
|
|
return EXIT_FAILURE;
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
|
2013-05-24 15:52:33 +08:00
|
|
|
if( inputName.empty() )
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-05-24 15:52:33 +08:00
|
|
|
capture = cvCaptureFromCAM(0);
|
2013-06-04 22:31:51 +08:00
|
|
|
if(!capture)
|
2013-05-24 15:52:33 +08:00
|
|
|
cout << "Capture from CAM 0 didn't work" << endl;
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
2013-10-22 00:47:55 +08:00
|
|
|
else
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-10-22 00:47:55 +08:00
|
|
|
image = imread( inputName, CV_LOAD_IMAGE_COLOR );
|
2012-10-17 07:18:30 +08:00
|
|
|
if( image.empty() )
|
|
|
|
{
|
|
|
|
capture = cvCaptureFromAVI( inputName.c_str() );
|
2013-06-04 22:31:51 +08:00
|
|
|
if(!capture)
|
2013-05-24 15:52:33 +08:00
|
|
|
cout << "Capture from AVI didn't work" << endl;
|
2013-10-22 00:47:55 +08:00
|
|
|
return EXIT_FAILURE;
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-19 16:36:23 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
if( capture )
|
|
|
|
{
|
|
|
|
cout << "In capture ..." << endl;
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
IplImage* iplImg = cvQueryFrame( capture );
|
2013-03-30 04:47:22 +08:00
|
|
|
frame = cv::cvarrToMat(iplImg);
|
2013-05-24 15:52:33 +08:00
|
|
|
vector<Rect> faces;
|
2012-10-17 07:18:30 +08:00
|
|
|
if( frame.empty() )
|
|
|
|
break;
|
|
|
|
if( iplImg->origin == IPL_ORIGIN_TL )
|
2013-11-19 00:48:00 +08:00
|
|
|
frame.copyTo( frameCopy0 );
|
2012-10-17 07:18:30 +08:00
|
|
|
else
|
2013-11-19 00:48:00 +08:00
|
|
|
flip( frame, frameCopy0, 0 );
|
|
|
|
if( scale == 1)
|
|
|
|
frameCopy0.copyTo(frameCopy);
|
|
|
|
else
|
|
|
|
resize(frameCopy0, frameCopy, Size(), 1./scale, 1./scale, INTER_LINEAR);
|
2013-10-22 00:47:55 +08:00
|
|
|
|
2013-06-19 16:36:23 +08:00
|
|
|
if(useCPU)
|
2013-11-19 03:26:19 +08:00
|
|
|
detectCPU(frameCopy, faces, cpu_cascade, 1);
|
2013-06-19 16:36:23 +08:00
|
|
|
else
|
2013-11-19 03:26:19 +08:00
|
|
|
detect(frameCopy, faces, cascade, 1);
|
2013-10-22 00:47:55 +08:00
|
|
|
|
2013-11-19 00:48:00 +08:00
|
|
|
Draw(frameCopy, faces, 1);
|
2012-10-17 07:18:30 +08:00
|
|
|
if( waitKey( 10 ) >= 0 )
|
2013-10-22 00:47:55 +08:00
|
|
|
break;
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
cvReleaseCapture( &capture );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 04:15:02 +08:00
|
|
|
cout << "In image read " << image.size() << endl;
|
2013-05-24 15:52:33 +08:00
|
|
|
vector<Rect> faces;
|
|
|
|
vector<Rect> ref_rst;
|
|
|
|
double accuracy = 0.;
|
2013-11-19 03:26:19 +08:00
|
|
|
detectCPU(image, ref_rst, cpu_cascade, scale);
|
2013-11-19 11:15:04 +08:00
|
|
|
|
2013-12-18 04:15:02 +08:00
|
|
|
cout << "loops: ";
|
2013-06-19 16:36:23 +08:00
|
|
|
for(int i = 0; i <= LOOP_NUM; i ++)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-12-18 04:15:02 +08:00
|
|
|
cout << i << ", ";
|
2013-06-19 16:36:23 +08:00
|
|
|
if(useCPU)
|
2013-11-19 03:26:19 +08:00
|
|
|
detectCPU(image, faces, cpu_cascade, scale);
|
2013-06-19 16:36:23 +08:00
|
|
|
else
|
|
|
|
{
|
2013-11-19 03:26:19 +08:00
|
|
|
detect(image, faces, cascade, scale);
|
2013-06-19 16:36:23 +08:00
|
|
|
if(i == 0)
|
|
|
|
{
|
2013-05-24 15:52:33 +08:00
|
|
|
accuracy = checkRectSimilarity(image.size(), ref_rst, faces);
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
2013-05-24 15:52:33 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
2013-12-18 04:15:02 +08:00
|
|
|
cout << "done!" << endl;
|
|
|
|
if (useCPU)
|
|
|
|
cout << "average CPU time (noCamera) : ";
|
|
|
|
else
|
|
|
|
cout << "average GPU time (noCamera) : ";
|
2013-12-18 21:02:39 +08:00
|
|
|
cout << getTotalTime() / LOOP_NUM << " ms" << endl;
|
2013-12-18 04:15:02 +08:00
|
|
|
cout << "accuracy value: " << accuracy <<endl;
|
|
|
|
|
2013-05-24 15:52:33 +08:00
|
|
|
Draw(image, faces, scale);
|
|
|
|
waitKey(0);
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cvDestroyWindow("result");
|
2013-12-02 15:15:46 +08:00
|
|
|
std::cout<< "single-threaded sample has finished" <<std::endl;
|
2012-10-17 07:18:30 +08:00
|
|
|
return 0;
|
2012-08-31 14:08:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-02 15:15:46 +08:00
|
|
|
///////////////////////////////////////detectfaces with multithreading////////////////////////////////////////////
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1700)
|
|
|
|
|
2013-12-18 21:02:39 +08:00
|
|
|
static void detectFaces(std::string fileName, int threadNum)
|
2013-11-22 18:47:58 +08:00
|
|
|
{
|
2013-12-02 15:15:46 +08:00
|
|
|
ocl::OclCascadeClassifier cascade;
|
2013-12-06 16:12:24 +08:00
|
|
|
if(!cascade.load(cascadeName))
|
|
|
|
{
|
|
|
|
std::cout << "ERROR: Could not load classifier cascade: " << cascadeName << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-02 15:15:46 +08:00
|
|
|
Mat img = imread(fileName, CV_LOAD_IMAGE_COLOR);
|
|
|
|
if (img.empty())
|
2013-11-22 18:47:58 +08:00
|
|
|
{
|
2013-12-18 21:02:39 +08:00
|
|
|
std::cout << '[' << threadNum << "] " << "can't open file " + fileName <<std::endl;
|
2013-12-02 15:15:46 +08:00
|
|
|
return;
|
2013-11-22 18:47:58 +08:00
|
|
|
}
|
2013-12-02 15:15:46 +08:00
|
|
|
|
|
|
|
ocl::oclMat d_img;
|
|
|
|
d_img.upload(img);
|
|
|
|
|
|
|
|
std::vector<Rect> oclfaces;
|
2013-12-18 21:02:39 +08:00
|
|
|
std::thread::id tid = std::this_thread::get_id();
|
|
|
|
std::cout << '[' << threadNum << "] "
|
|
|
|
<< "ThreadID = " << tid
|
|
|
|
<< ", CommandQueue = " << *(void**)ocl::getClCommandQueuePtr()
|
|
|
|
<< endl;
|
|
|
|
for(int i = 0; i <= LOOP_NUM; i++)
|
|
|
|
{
|
|
|
|
if(i>0) workBegin(threadNum);
|
2013-12-23 22:50:17 +08:00
|
|
|
cascade.detectMultiScale(d_img, oclfaces, 1.1, 3, 0|CASCADE_SCALE_IMAGE, Size(30, 30), Size(0, 0));
|
2013-12-18 21:02:39 +08:00
|
|
|
if(i>0) workEnd(threadNum);
|
|
|
|
}
|
|
|
|
std::cout << '[' << threadNum << "] " << "Average time = " << getTotalTime(threadNum) / LOOP_NUM << " ms" << endl;
|
2013-12-02 15:15:46 +08:00
|
|
|
|
|
|
|
for(unsigned int i = 0; i<oclfaces.size(); i++)
|
2013-12-06 18:49:36 +08:00
|
|
|
rectangle(img, Point(oclfaces[i].x, oclfaces[i].y), Point(oclfaces[i].x + oclfaces[i].width, oclfaces[i].y + oclfaces[i].height), colors[i%8], 3);
|
2013-12-02 15:15:46 +08:00
|
|
|
|
2013-12-06 18:49:36 +08:00
|
|
|
std::string::size_type pos = outputName.rfind('.');
|
2013-12-18 21:02:39 +08:00
|
|
|
std::string strTid = std::to_string(_threadid);
|
|
|
|
if( !outputName.empty() )
|
2013-12-06 18:49:36 +08:00
|
|
|
{
|
2013-12-18 21:02:39 +08:00
|
|
|
if(pos == std::string::npos)
|
|
|
|
{
|
|
|
|
std::cout << "Invalid output file name: " << outputName << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string outputNameTid = outputName.substr(0, pos) + "_" + strTid + outputName.substr(pos);
|
|
|
|
imwrite(outputNameTid, img);
|
|
|
|
}
|
2013-12-06 18:49:36 +08:00
|
|
|
}
|
2013-12-18 21:02:39 +08:00
|
|
|
imshow(strTid, img);
|
2013-12-06 18:49:36 +08:00
|
|
|
waitKey(0);
|
2013-12-02 15:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void facedetect_multithreading(int nthreads)
|
|
|
|
{
|
|
|
|
int thread_number = MAX_THREADS < nthreads ? MAX_THREADS : nthreads;
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
for(int i = 0; i<thread_number; i++)
|
2013-12-18 21:02:39 +08:00
|
|
|
threads.push_back(std::thread(detectFaces, inputName, i));
|
2013-12-02 15:15:46 +08:00
|
|
|
for(int i = 0; i<thread_number; i++)
|
|
|
|
threads[i].join();
|
2013-11-22 18:47:58 +08:00
|
|
|
}
|
2013-12-02 15:15:46 +08:00
|
|
|
#endif
|
2013-11-22 18:47:58 +08:00
|
|
|
|
|
|
|
int main( int argc, const char** argv )
|
|
|
|
{
|
2013-12-02 15:15:46 +08:00
|
|
|
|
|
|
|
const char* keys =
|
2013-12-09 20:31:31 +08:00
|
|
|
"{ h help | false | print help message }"
|
|
|
|
"{ i input | | specify input image }"
|
|
|
|
"{ t template | haarcascade_frontalface_alt.xml |"
|
2013-12-02 15:15:46 +08:00
|
|
|
" specify template file path }"
|
2013-12-09 20:31:31 +08:00
|
|
|
"{ c scale | 1.0 | scale image }"
|
|
|
|
"{ s use_cpu | false | use cpu or gpu to process the image }"
|
2013-12-23 22:50:17 +08:00
|
|
|
"{ o output | | specify output image save path(only works when input is images) }"
|
2013-12-09 20:31:31 +08:00
|
|
|
"{ n thread_num | 1 | set number of threads >= 1 }";
|
2013-12-02 15:15:46 +08:00
|
|
|
|
|
|
|
CommandLineParser cmd(argc, argv, keys);
|
2013-12-09 20:31:31 +08:00
|
|
|
if (cmd.has("help"))
|
2013-12-02 15:15:46 +08:00
|
|
|
{
|
|
|
|
cout << "Usage : facedetect [options]" << endl;
|
|
|
|
cout << "Available options:" << endl;
|
2013-12-09 20:31:31 +08:00
|
|
|
cmd.printMessage();
|
2013-12-02 15:15:46 +08:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
bool useCPU = cmd.get<bool>("s");
|
|
|
|
inputName = cmd.get<string>("i");
|
|
|
|
outputName = cmd.get<string>("o");
|
|
|
|
cascadeName = cmd.get<string>("t");
|
|
|
|
double scale = cmd.get<double>("c");
|
|
|
|
int n = cmd.get<int>("n");
|
|
|
|
|
|
|
|
if(n > 1)
|
|
|
|
{
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1700)
|
|
|
|
std::cout<<"multi-threaded sample is running" <<std::endl;
|
|
|
|
facedetect_multithreading(n);
|
|
|
|
std::cout<<"multi-threaded sample has finished" <<std::endl;
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
std::cout << "std::thread is not supported, running a single-threaded version" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (n<0)
|
|
|
|
std::cout<<"incorrect number of threads:" << n << ", running a single-threaded version" <<std::endl;
|
|
|
|
else
|
|
|
|
std::cout<<"single-threaded sample is running" <<std::endl;
|
|
|
|
return facedetect_one_thread(useCPU, scale);
|
|
|
|
|
2013-11-22 18:47:58 +08:00
|
|
|
}
|
|
|
|
|
2013-06-04 22:31:51 +08:00
|
|
|
void detect( Mat& img, vector<Rect>& faces,
|
2013-07-22 17:53:56 +08:00
|
|
|
ocl::OclCascadeClassifier& cascade,
|
2013-11-19 02:48:04 +08:00
|
|
|
double scale)
|
2012-08-31 14:08:52 +08:00
|
|
|
{
|
2013-06-19 16:36:23 +08:00
|
|
|
ocl::oclMat image(img);
|
|
|
|
ocl::oclMat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
|
2013-11-19 00:48:00 +08:00
|
|
|
workBegin();
|
2013-06-25 18:08:23 +08:00
|
|
|
ocl::cvtColor( image, gray, COLOR_BGR2GRAY );
|
2013-06-19 16:36:23 +08:00
|
|
|
ocl::resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
|
|
|
|
ocl::equalizeHist( smallImg, smallImg );
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-05-24 15:52:33 +08:00
|
|
|
cascade.detectMultiScale( smallImg, faces, 1.1,
|
2013-06-19 16:36:23 +08:00
|
|
|
3, 0
|
2013-07-22 17:53:56 +08:00
|
|
|
|CASCADE_SCALE_IMAGE
|
2013-06-19 16:36:23 +08:00
|
|
|
, Size(30,30), Size(0, 0) );
|
2013-11-19 00:48:00 +08:00
|
|
|
workEnd();
|
2013-05-24 15:52:33 +08:00
|
|
|
}
|
|
|
|
|
2013-06-04 22:31:51 +08:00
|
|
|
void detectCPU( Mat& img, vector<Rect>& faces,
|
2013-06-19 16:36:23 +08:00
|
|
|
CascadeClassifier& cascade,
|
2013-11-19 02:48:04 +08:00
|
|
|
double scale)
|
2013-05-24 15:52:33 +08:00
|
|
|
{
|
2013-11-19 00:48:00 +08:00
|
|
|
workBegin();
|
2013-05-24 15:52:33 +08:00
|
|
|
Mat cpu_gray, cpu_smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
|
2013-07-22 17:53:56 +08:00
|
|
|
cvtColor(img, cpu_gray, COLOR_BGR2GRAY);
|
2013-05-24 15:52:33 +08:00
|
|
|
resize(cpu_gray, cpu_smallImg, cpu_smallImg.size(), 0, 0, INTER_LINEAR);
|
|
|
|
equalizeHist(cpu_smallImg, cpu_smallImg);
|
|
|
|
cascade.detectMultiScale(cpu_smallImg, faces, 1.1,
|
2013-07-22 17:53:56 +08:00
|
|
|
3, 0 | CASCADE_SCALE_IMAGE,
|
2013-06-19 16:36:23 +08:00
|
|
|
Size(30, 30), Size(0, 0));
|
2013-11-19 00:48:00 +08:00
|
|
|
workEnd();
|
2013-05-24 15:52:33 +08:00
|
|
|
}
|
|
|
|
|
2013-06-19 16:36:23 +08:00
|
|
|
|
2013-05-24 15:52:33 +08:00
|
|
|
void Draw(Mat& img, vector<Rect>& faces, double scale)
|
|
|
|
{
|
|
|
|
int i = 0;
|
2012-10-17 07:18:30 +08:00
|
|
|
for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
|
|
|
|
{
|
|
|
|
Point center;
|
|
|
|
Scalar color = colors[i%8];
|
|
|
|
int radius;
|
|
|
|
center.x = cvRound((r->x + r->width*0.5)*scale);
|
|
|
|
center.y = cvRound((r->y + r->height*0.5)*scale);
|
|
|
|
radius = cvRound((r->width + r->height)*0.25*scale);
|
|
|
|
circle( img, center, radius, color, 3, 8, 0 );
|
|
|
|
}
|
2013-12-23 22:50:17 +08:00
|
|
|
//if( !outputName.empty() ) imwrite( outputName, img );
|
2013-12-18 21:02:39 +08:00
|
|
|
if( abs(scale-1.0)>.001 )
|
2013-06-28 15:08:39 +08:00
|
|
|
{
|
2013-06-28 16:23:01 +08:00
|
|
|
resize(img, img, Size((int)(img.cols/scale), (int)(img.rows/scale)));
|
2013-06-28 15:08:39 +08:00
|
|
|
}
|
|
|
|
imshow( "result", img );
|
2013-08-21 20:44:09 +08:00
|
|
|
|
2012-08-31 14:08:52 +08:00
|
|
|
}
|
2013-05-24 15:52:33 +08:00
|
|
|
|
2013-06-19 16:36:23 +08:00
|
|
|
|
|
|
|
double checkRectSimilarity(Size sz, vector<Rect>& ob1, vector<Rect>& ob2)
|
2013-05-24 15:52:33 +08:00
|
|
|
{
|
|
|
|
double final_test_result = 0.0;
|
|
|
|
size_t sz1 = ob1.size();
|
|
|
|
size_t sz2 = ob2.size();
|
|
|
|
|
|
|
|
if(sz1 != sz2)
|
2013-06-19 16:36:23 +08:00
|
|
|
{
|
2013-05-24 15:52:33 +08:00
|
|
|
return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
|
2013-06-19 16:36:23 +08:00
|
|
|
}
|
2013-05-24 15:52:33 +08:00
|
|
|
else
|
|
|
|
{
|
2013-06-19 16:36:23 +08:00
|
|
|
if(sz1==0 && sz2==0)
|
|
|
|
return 0;
|
|
|
|
Mat cpu_result(sz, CV_8UC1);
|
2013-05-24 15:52:33 +08:00
|
|
|
cpu_result.setTo(0);
|
|
|
|
|
|
|
|
for(vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); r++)
|
2013-06-04 22:31:51 +08:00
|
|
|
{
|
2013-06-19 16:36:23 +08:00
|
|
|
Mat cpu_result_roi(cpu_result, *r);
|
2013-05-24 15:52:33 +08:00
|
|
|
cpu_result_roi.setTo(1);
|
|
|
|
cpu_result.copyTo(cpu_result);
|
|
|
|
}
|
2013-06-19 16:36:23 +08:00
|
|
|
int cpu_area = countNonZero(cpu_result > 0);
|
2013-05-24 15:52:33 +08:00
|
|
|
|
|
|
|
|
2013-06-19 16:36:23 +08:00
|
|
|
Mat gpu_result(sz, CV_8UC1);
|
2013-05-24 15:52:33 +08:00
|
|
|
gpu_result.setTo(0);
|
|
|
|
for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); r2++)
|
|
|
|
{
|
|
|
|
cv::Mat gpu_result_roi(gpu_result, *r2);
|
|
|
|
gpu_result_roi.setTo(1);
|
|
|
|
gpu_result.copyTo(gpu_result);
|
|
|
|
}
|
|
|
|
|
2013-06-19 16:36:23 +08:00
|
|
|
Mat result_;
|
2013-05-24 15:52:33 +08:00
|
|
|
multiply(cpu_result, gpu_result, result_);
|
2013-06-19 16:36:23 +08:00
|
|
|
int result = countNonZero(result_ > 0);
|
|
|
|
if(cpu_area!=0 && result!=0)
|
|
|
|
final_test_result = 1.0 - (double)result/(double)cpu_area;
|
|
|
|
else if(cpu_area==0 && result!=0)
|
|
|
|
final_test_result = -1;
|
2013-05-24 15:52:33 +08:00
|
|
|
}
|
|
|
|
return final_test_result;
|
|
|
|
}
|