OpenCV之CvANN_MLP和CvSVM测试

来源:互联网 发布:scala编程实战 pdf 编辑:程序博客网 时间:2024/04/30 15:48

CvANN介绍参考http://blog.csdn.net/xiaowei_cqu/article/details/9027617

但是博主demo没看懂,可能是没贴对。

另整理demo如下:

void testANN()
{
//Setup the BPNetwork
CvANN_MLP bp; 
// Set up BPNetwork's parameters
CvANN_MLP_TrainParams params;
//params.train_method=CvANN_MLP_TrainParams::BACKPROP;
//params.bp_dw_scale=0.1;
//params.bp_moment_scale=0.1;
params.train_method=CvANN_MLP_TrainParams::RPROP;
params.rp_dw0 = 0.1; 
params.rp_dw_plus = 1.2; 
params.rp_dw_minus = 0.5;
params.rp_dw_min = FLT_EPSILON; 
params.rp_dw_max = 50.;


// Set up training data
// step 1:  
float labels[4] = {1.0, -1.0, -1.0, -1.0};  
Mat labelsMat(4, 1, CV_32FC1, labels);  
 
float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };  
Mat trainingDataMat(4, 2, CV_32FC1, trainingData);  


Mat layerSizes=(Mat_<int>(1,4) << 2,3,3,1);
bp.create(layerSizes,CvANN_MLP::SIGMOID_SYM);//CvANN_MLP::SIGMOID_SYM
                                          //CvANN_MLP::GAUSSIAN
                                          //CvANN_MLP::IDENTITY


bp.train(trainingDataMat, labelsMat, Mat(),Mat(), params);




// Data for visual representation
int width = 512, height = 512;
Mat image = Mat::zeros(height, width, CV_8UC3);
Vec3b green(0,255,0), blue (255,0,0);
// Show the decision regions given by the SVM
for (int i = 0; i < image.rows; ++i)
for (int j = 0; j < image.cols; ++j)
{
Mat sampleMat = (Mat_<float>(1,2) << i,j);
Mat responseMat;
bp.predict(sampleMat,responseMat);
float* p=responseMat.ptr<float>(0);
float response=p[0];

if ( response < -0)
{
image.at<Vec3b>(j, i) = green;
}
else if ( response > 0) 
{
image.at<Vec3b>(j, i) = blue;
}
}


// Show the training data
int thickness = -1;
int lineType = 8;
circle( image, Point(501,  10), 5, Scalar(  0,   0,   0), thickness, lineType);
circle( image, Point(255,  10), 5, Scalar(255, 255, 255), thickness, lineType);
circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType);
circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness, lineType);


imwrite("result.png", image);        // save the image 


imshow("BP Simple Example", image); // show it to the user
waitKey(0);


}






void testSVM()
{


// step 1:
float labels[4] = {1.0, -1.0, -1.0, -1.0};
Mat labelsMat(3, 1, CV_32FC1, labels);

float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };
Mat trainingDataMat(3, 2, CV_32FC1, trainingData);


// step 2:
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

// step 3:
CvSVM SVM;
SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params);

// step 4:
// Data for visual representation
int width = 512, height = 512;
Mat image = Mat::zeros(height, width, CV_8UC3);
Vec3b green(0, 255, 0), blue(255, 0, 0);
for (int i=0; i<image.rows; i++)
{
for (int j=0; j<image.cols; j++)
{
Mat sampleMat = (Mat_<float>(1,2) << i,j);
float response = SVM.predict(sampleMat);


if ( response < -0)
{
image.at<Vec3b>(j, i) = green;
}
else if ( response > 0) 
{
image.at<Vec3b>(j, i) = blue;
}
}
}


// step 5:
int c = SVM.get_support_vector_count();


for (int i=0; i<c; i++)
{
const float* v = SVM.get_support_vector(i);
}


// Show the training data
int thickness = -1;
int lineType = 8;
circle( image, Point(501,  10), 5, Scalar(  0,   0,   0), thickness, lineType);
circle( image, Point(255,  10), 5, Scalar(255, 255, 255), thickness, lineType);
circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType);
circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness, lineType);


imshow("SVM Simple Example", image); // show it to the user
waitKey(0);
}


直接加入到main函数对比下就可以了。。

原创粉丝点击