Merge remote-tracking branch 'origin/2.4' into merge-2.4
Conflicts: modules/core/include/opencv2/core/operations.hpp modules/core/include/opencv2/core/version.hpp modules/core/src/gpumat.cpp modules/cudaimgproc/src/color.cpp modules/features2d/src/orb.cpp modules/imgproc/src/samplers.cpp modules/ocl/include/opencv2/ocl/matrix_operations.hpp modules/ocl/include/opencv2/ocl/ocl.hpp samples/ocl/facedetect.cpp
This commit is contained in:
@@ -29,6 +29,10 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
|
||||
ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/cudafilters/include")
|
||||
endif()
|
||||
|
||||
if(HAVE_opencv_ocl)
|
||||
ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/ocl/include")
|
||||
endif()
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX AND NOT ENABLE_NOISY_WARNINGS)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
|
||||
endif()
|
||||
@@ -56,6 +60,10 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
|
||||
target_link_libraries(${the_target} opencv_cudaarithm opencv_cudafilters)
|
||||
endif()
|
||||
|
||||
if(HAVE_opencv_ocl)
|
||||
target_link_libraries(${the_target} opencv_ocl)
|
||||
endif()
|
||||
|
||||
set_target_properties(${the_target} PROPERTIES
|
||||
OUTPUT_NAME "cpp-${sample_kind}-${name}"
|
||||
PROJECT_LABEL "(${sample_KIND}) ${name}")
|
||||
|
@@ -1,8 +1,13 @@
|
||||
#include "opencv2/opencv_modules.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "opencv2/features2d/features2d.hpp"
|
||||
#include "opencv2/nonfree/nonfree.hpp"
|
||||
#include "opencv2/ml/ml.hpp"
|
||||
#ifdef HAVE_OPENCV_OCL
|
||||
#define _OCL_SVM_ 1 //select whether using ocl::svm method or not, default is using
|
||||
#include "opencv2/ocl/ocl.hpp"
|
||||
#endif
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -2373,9 +2378,15 @@ static void setSVMTrainAutoParams( CvParamGrid& c_grid, CvParamGrid& gamma_grid,
|
||||
degree_grid.step = 0;
|
||||
}
|
||||
|
||||
#if defined HAVE_OPENCV_OCL && _OCL_SVM_
|
||||
static void trainSVMClassifier( cv::ocl::CvSVM_OCL& svm, const SVMTrainParamsExt& svmParamsExt, const string& objClassName, VocData& vocData,
|
||||
Ptr<BOWImgDescriptorExtractor>& bowExtractor, const Ptr<FeatureDetector>& fdetector,
|
||||
const string& resPath )
|
||||
#else
|
||||
static void trainSVMClassifier( CvSVM& svm, const SVMTrainParamsExt& svmParamsExt, const string& objClassName, VocData& vocData,
|
||||
Ptr<BOWImgDescriptorExtractor>& bowExtractor, const Ptr<FeatureDetector>& fdetector,
|
||||
const string& resPath )
|
||||
#endif
|
||||
{
|
||||
/* first check if a previously trained svm for the current class has been saved to file */
|
||||
string svmFilename = resPath + svmsDir + "/" + objClassName + ".xml.gz";
|
||||
@@ -2448,9 +2459,15 @@ static void trainSVMClassifier( CvSVM& svm, const SVMTrainParamsExt& svmParamsEx
|
||||
}
|
||||
}
|
||||
|
||||
#if defined HAVE_OPENCV_OCL && _OCL_SVM_
|
||||
static void computeConfidences( cv::ocl::CvSVM_OCL& svm, const string& objClassName, VocData& vocData,
|
||||
Ptr<BOWImgDescriptorExtractor>& bowExtractor, const Ptr<FeatureDetector>& fdetector,
|
||||
const string& resPath )
|
||||
#else
|
||||
static void computeConfidences( CvSVM& svm, const string& objClassName, VocData& vocData,
|
||||
Ptr<BOWImgDescriptorExtractor>& bowExtractor, const Ptr<FeatureDetector>& fdetector,
|
||||
const string& resPath )
|
||||
#endif
|
||||
{
|
||||
cout << "*** CALCULATING CONFIDENCES FOR CLASS " << objClassName << " ***" << endl;
|
||||
cout << "CALCULATING BOW VECTORS FOR TEST SET OF " << objClassName << "..." << endl;
|
||||
@@ -2589,7 +2606,11 @@ int main(int argc, char** argv)
|
||||
for( size_t classIdx = 0; classIdx < objClasses.size(); ++classIdx )
|
||||
{
|
||||
// Train a classifier on train dataset
|
||||
#if defined HAVE_OPENCV_OCL && _OCL_SVM_
|
||||
cv::ocl::CvSVM_OCL svm;
|
||||
#else
|
||||
CvSVM svm;
|
||||
#endif
|
||||
trainSVMClassifier( svm, svmTrainParamsExt, objClasses[classIdx], vocData,
|
||||
bowExtractor, featureDetector, resPath );
|
||||
|
||||
|
@@ -1,6 +1,12 @@
|
||||
#include "opencv2/opencv_modules.hpp"
|
||||
#include "opencv2/core/core.hpp"
|
||||
#include "opencv2/ml/ml.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#ifdef HAVE_OPENCV_OCL
|
||||
#define _OCL_KNN_ 1 // select whether using ocl::KNN method or not, default is using
|
||||
#define _OCL_SVM_ 1 // select whether using ocl::svm method or not, default is using
|
||||
#include "opencv2/ocl/ocl.hpp"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -133,7 +139,14 @@ static void find_decision_boundary_KNN( int K )
|
||||
prepare_train_data( trainSamples, trainClasses );
|
||||
|
||||
// learn classifier
|
||||
#if defined HAVE_OPENCV_OCL && _OCL_KNN_
|
||||
cv::ocl::KNearestNeighbour knnClassifier;
|
||||
Mat temp, result;
|
||||
knnClassifier.train(trainSamples, trainClasses, temp, false, K);
|
||||
cv::ocl::oclMat testSample_ocl, reslut_ocl;
|
||||
#else
|
||||
CvKNearest knnClassifier( trainSamples, trainClasses, Mat(), false, K );
|
||||
#endif
|
||||
|
||||
Mat testSample( 1, 2, CV_32FC1 );
|
||||
for( int y = 0; y < img.rows; y += testStep )
|
||||
@@ -142,9 +155,19 @@ static void find_decision_boundary_KNN( int K )
|
||||
{
|
||||
testSample.at<float>(0) = (float)x;
|
||||
testSample.at<float>(1) = (float)y;
|
||||
#if defined HAVE_OPENCV_OCL && _OCL_KNN_
|
||||
testSample_ocl.upload(testSample);
|
||||
|
||||
knnClassifier.find_nearest(testSample_ocl, K, reslut_ocl);
|
||||
|
||||
reslut_ocl.download(result);
|
||||
int response = saturate_cast<int>(result.at<float>(0));
|
||||
circle(imgDst, Point(x, y), 1, classColors[response]);
|
||||
#else
|
||||
|
||||
int response = (int)knnClassifier.find_nearest( testSample, K );
|
||||
circle( imgDst, Point(x,y), 1, classColors[response] );
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,7 +182,11 @@ static void find_decision_boundary_SVM( CvSVMParams params )
|
||||
prepare_train_data( trainSamples, trainClasses );
|
||||
|
||||
// learn classifier
|
||||
#if defined HAVE_OPENCV_OCL && _OCL_SVM_
|
||||
cv::ocl::CvSVM_OCL svmClassifier(trainSamples, trainClasses, Mat(), Mat(), params);
|
||||
#else
|
||||
CvSVM svmClassifier( trainSamples, trainClasses, Mat(), Mat(), params );
|
||||
#endif
|
||||
|
||||
Mat testSample( 1, 2, CV_32FC1 );
|
||||
for( int y = 0; y < img.rows; y += testStep )
|
||||
@@ -178,7 +205,7 @@ static void find_decision_boundary_SVM( CvSVMParams params )
|
||||
for( int i = 0; i < svmClassifier.get_support_vector_count(); i++ )
|
||||
{
|
||||
const float* supportVector = svmClassifier.get_support_vector(i);
|
||||
circle( imgDst, Point(supportVector[0],supportVector[1]), 5, Scalar(255,255,255), -1 );
|
||||
circle( imgDst, Point(saturate_cast<int>(supportVector[0]),saturate_cast<int>(supportVector[1])), 5, CV_RGB(255,255,255), -1 );
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -8,11 +8,16 @@
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1700)
|
||||
# include <thread>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
#define LOOP_NUM 1
|
||||
|
||||
///////////////////////////single-threading faces detecting///////////////////////////////
|
||||
|
||||
const static Scalar colors[] = { CV_RGB(0,0,255),
|
||||
CV_RGB(0,128,255),
|
||||
CV_RGB(0,255,255),
|
||||
@@ -26,7 +31,7 @@ const static Scalar colors[] = { CV_RGB(0,0,255),
|
||||
|
||||
int64 work_begin = 0;
|
||||
int64 work_end = 0;
|
||||
string outputName;
|
||||
string inputName, outputName, cascadeName;
|
||||
|
||||
static void workBegin()
|
||||
{
|
||||
@@ -61,41 +66,17 @@ static void Draw(Mat& img, vector<Rect>& faces, double scale);
|
||||
// Else if will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels)
|
||||
double checkRectSimilarity(Size sz, vector<Rect>& cpu_rst, vector<Rect>& gpu_rst);
|
||||
|
||||
int main( int argc, const char** argv )
|
||||
static int facedetect_one_thread(bool useCPU, double scale )
|
||||
{
|
||||
const char* keys =
|
||||
"{ h help | false | print help message }"
|
||||
"{ i input | | specify input image }"
|
||||
"{ t template | haarcascade_frontalface_alt.xml |"
|
||||
" specify template file path }"
|
||||
"{ c scale | 1.0 | scale image }"
|
||||
"{ s use_cpu | false | use cpu or gpu to process the image }"
|
||||
"{ o output | facedetect_output.jpg |"
|
||||
" specify output image save path(only works when input is images) }";
|
||||
|
||||
CommandLineParser cmd(argc, argv, keys);
|
||||
if (cmd.get<bool>("help"))
|
||||
{
|
||||
cout << "Usage : facedetect [options]" << endl;
|
||||
cout << "Available options:" << endl;
|
||||
cmd.printMessage();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
CvCapture* capture = 0;
|
||||
Mat frame, frameCopy0, frameCopy, image;
|
||||
|
||||
bool useCPU = cmd.get<bool>("s");
|
||||
string inputName = cmd.get<string>("i");
|
||||
outputName = cmd.get<string>("o");
|
||||
string cascadeName = cmd.get<string>("t");
|
||||
double scale = cmd.get<double>("c");
|
||||
ocl::OclCascadeClassifier cascade;
|
||||
CascadeClassifier cpu_cascade;
|
||||
|
||||
if( !cascade.load( cascadeName ) || !cpu_cascade.load(cascadeName) )
|
||||
{
|
||||
cout << "ERROR: Could not load classifier cascade" << endl;
|
||||
cout << "ERROR: Could not load classifier cascade: " << cascadeName << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -186,9 +167,114 @@ int main( int argc, const char** argv )
|
||||
}
|
||||
|
||||
cvDestroyWindow("result");
|
||||
std::cout<< "single-threaded sample has finished" <<std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
///////////////////////////////////////detectfaces with multithreading////////////////////////////////////////////
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1700)
|
||||
|
||||
#define MAX_THREADS 10
|
||||
|
||||
static void detectFaces(std::string fileName)
|
||||
{
|
||||
ocl::OclCascadeClassifier cascade;
|
||||
if(!cascade.load(cascadeName))
|
||||
{
|
||||
std::cout << "ERROR: Could not load classifier cascade: " << cascadeName << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Mat img = imread(fileName, CV_LOAD_IMAGE_COLOR);
|
||||
if (img.empty())
|
||||
{
|
||||
std::cout << "cann't open file " + fileName <<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
ocl::oclMat d_img;
|
||||
d_img.upload(img);
|
||||
|
||||
std::vector<Rect> oclfaces;
|
||||
cascade.detectMultiScale(d_img, oclfaces, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30), Size(0, 0));
|
||||
|
||||
for(unsigned int i = 0; i<oclfaces.size(); i++)
|
||||
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);
|
||||
|
||||
std::string::size_type pos = outputName.rfind('.');
|
||||
std::string outputNameTid = outputName + '-' + std::to_string(_threadid);
|
||||
if(pos == std::string::npos)
|
||||
{
|
||||
std::cout << "Invalid output file name: " << outputName << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
outputNameTid = outputName.substr(0, pos) + "_" + std::to_string(_threadid) + outputName.substr(pos);
|
||||
imwrite(outputNameTid, img);
|
||||
}
|
||||
imshow(outputNameTid, img);
|
||||
waitKey(0);
|
||||
}
|
||||
|
||||
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++)
|
||||
threads.push_back(std::thread(detectFaces, inputName));
|
||||
for(int i = 0; i<thread_number; i++)
|
||||
threads[i].join();
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, const char** argv )
|
||||
{
|
||||
|
||||
const char* keys =
|
||||
"{ h help | false | print help message }"
|
||||
"{ i input | | specify input image }"
|
||||
"{ t template | haarcascade_frontalface_alt.xml |"
|
||||
" specify template file path }"
|
||||
"{ c scale | 1.0 | scale image }"
|
||||
"{ s use_cpu | false | use cpu or gpu to process the image }"
|
||||
"{ o output | facedetect_output.jpg |"
|
||||
" specify output image save path(only works when input is images) }"
|
||||
"{ n thread_num | 1 | set number of threads >= 1 }";
|
||||
|
||||
CommandLineParser cmd(argc, argv, keys);
|
||||
if (cmd.has("help"))
|
||||
{
|
||||
cout << "Usage : facedetect [options]" << endl;
|
||||
cout << "Available options:" << endl;
|
||||
cmd.printMessage();
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
void detect( Mat& img, vector<Rect>& faces,
|
||||
ocl::OclCascadeClassifier& cascade,
|
||||
double scale)
|
||||
|
Reference in New Issue
Block a user