Merge pull request #1924 from ilya-lavrenov:svm
This commit is contained in:
commit
098d4e6174
@ -20,6 +20,10 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
|
|||||||
ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/gpu/include")
|
ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/gpu/include")
|
||||||
endif()
|
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)
|
if(CMAKE_COMPILER_IS_GNUCXX AND NOT ENABLE_NOISY_WARNINGS)
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
|
||||||
endif()
|
endif()
|
||||||
@ -47,6 +51,10 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND)
|
|||||||
target_link_libraries(${the_target} opencv_gpu)
|
target_link_libraries(${the_target} opencv_gpu)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(HAVE_opencv_ocl)
|
||||||
|
target_link_libraries(${the_target} opencv_ocl)
|
||||||
|
endif()
|
||||||
|
|
||||||
set_target_properties(${the_target} PROPERTIES
|
set_target_properties(${the_target} PROPERTIES
|
||||||
OUTPUT_NAME "cpp-${sample_kind}-${name}"
|
OUTPUT_NAME "cpp-${sample_kind}-${name}"
|
||||||
PROJECT_LABEL "(${sample_KIND}) ${name}")
|
PROJECT_LABEL "(${sample_KIND}) ${name}")
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
|
#include "opencv2/opencv_modules.hpp"
|
||||||
#include "opencv2/highgui/highgui.hpp"
|
#include "opencv2/highgui/highgui.hpp"
|
||||||
#include "opencv2/imgproc/imgproc.hpp"
|
#include "opencv2/imgproc/imgproc.hpp"
|
||||||
#include "opencv2/features2d/features2d.hpp"
|
#include "opencv2/features2d/features2d.hpp"
|
||||||
#include "opencv2/nonfree/nonfree.hpp"
|
#include "opencv2/nonfree/nonfree.hpp"
|
||||||
#include "opencv2/ml/ml.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 <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -2373,9 +2378,15 @@ static void setSVMTrainAutoParams( CvParamGrid& c_grid, CvParamGrid& gamma_grid,
|
|||||||
degree_grid.step = 0;
|
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,
|
static void trainSVMClassifier( CvSVM& svm, const SVMTrainParamsExt& svmParamsExt, const string& objClassName, VocData& vocData,
|
||||||
Ptr<BOWImgDescriptorExtractor>& bowExtractor, const Ptr<FeatureDetector>& fdetector,
|
Ptr<BOWImgDescriptorExtractor>& bowExtractor, const Ptr<FeatureDetector>& fdetector,
|
||||||
const string& resPath )
|
const string& resPath )
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
/* first check if a previously trained svm for the current class has been saved to file */
|
/* first check if a previously trained svm for the current class has been saved to file */
|
||||||
string svmFilename = resPath + svmsDir + "/" + objClassName + ".xml.gz";
|
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,
|
static void computeConfidences( CvSVM& svm, const string& objClassName, VocData& vocData,
|
||||||
Ptr<BOWImgDescriptorExtractor>& bowExtractor, const Ptr<FeatureDetector>& fdetector,
|
Ptr<BOWImgDescriptorExtractor>& bowExtractor, const Ptr<FeatureDetector>& fdetector,
|
||||||
const string& resPath )
|
const string& resPath )
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
cout << "*** CALCULATING CONFIDENCES FOR CLASS " << objClassName << " ***" << endl;
|
cout << "*** CALCULATING CONFIDENCES FOR CLASS " << objClassName << " ***" << endl;
|
||||||
cout << "CALCULATING BOW VECTORS FOR TEST SET OF " << 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 )
|
for( size_t classIdx = 0; classIdx < objClasses.size(); ++classIdx )
|
||||||
{
|
{
|
||||||
// Train a classifier on train dataset
|
// Train a classifier on train dataset
|
||||||
|
#if defined HAVE_OPENCV_OCL && _OCL_SVM_
|
||||||
|
cv::ocl::CvSVM_OCL svm;
|
||||||
|
#else
|
||||||
CvSVM svm;
|
CvSVM svm;
|
||||||
|
#endif
|
||||||
trainSVMClassifier( svm, svmTrainParamsExt, objClasses[classIdx], vocData,
|
trainSVMClassifier( svm, svmTrainParamsExt, objClasses[classIdx], vocData,
|
||||||
bowExtractor, featureDetector, resPath );
|
bowExtractor, featureDetector, resPath );
|
||||||
|
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
|
#include "opencv2/opencv_modules.hpp"
|
||||||
#include "opencv2/core/core.hpp"
|
#include "opencv2/core/core.hpp"
|
||||||
#include "opencv2/ml/ml.hpp"
|
#include "opencv2/ml/ml.hpp"
|
||||||
#include "opencv2/highgui/highgui.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>
|
#include <stdio.h>
|
||||||
|
|
||||||
@ -133,7 +139,14 @@ static void find_decision_boundary_KNN( int K )
|
|||||||
prepare_train_data( trainSamples, trainClasses );
|
prepare_train_data( trainSamples, trainClasses );
|
||||||
|
|
||||||
// learn classifier
|
// 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 );
|
CvKNearest knnClassifier( trainSamples, trainClasses, Mat(), false, K );
|
||||||
|
#endif
|
||||||
|
|
||||||
Mat testSample( 1, 2, CV_32FC1 );
|
Mat testSample( 1, 2, CV_32FC1 );
|
||||||
for( int y = 0; y < img.rows; y += testStep )
|
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>(0) = (float)x;
|
||||||
testSample.at<float>(1) = (float)y;
|
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 );
|
int response = (int)knnClassifier.find_nearest( testSample, K );
|
||||||
circle( imgDst, Point(x,y), 1, classColors[response] );
|
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 );
|
prepare_train_data( trainSamples, trainClasses );
|
||||||
|
|
||||||
// learn classifier
|
// 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 );
|
CvSVM svmClassifier( trainSamples, trainClasses, Mat(), Mat(), params );
|
||||||
|
#endif
|
||||||
|
|
||||||
Mat testSample( 1, 2, CV_32FC1 );
|
Mat testSample( 1, 2, CV_32FC1 );
|
||||||
for( int y = 0; y < img.rows; y += testStep )
|
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++ )
|
for( int i = 0; i < svmClassifier.get_support_vector_count(); i++ )
|
||||||
{
|
{
|
||||||
const float* supportVector = svmClassifier.get_support_vector(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 );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user