使用opencv里面的神经网络

来源:互联网 发布:微软数据库工程师 编辑:程序博客网 时间:2024/06/06 20:36

参考资料

http://blog.csdn.net/xiaowei_cqu/article/details/9027617

这几天做人脸姿态检测的时候需要用的姿态的分类,由于之前了解过bp神经网络,所以就使用神经网络对姿态进行分类,

问题:

知道左眼和右眼的坐标、鼻子的坐标,如何经过训练来得知脸部姿态旋转了多少度?

解决方法:

使用bp神经网络进行训练;

准备训练样本(这个还没有准备充分,下节接着讨论)。

源程序:

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/ml/ml.hpp>#include <iostream>#include <string>using namespace std;using namespace cv;int main(){//Setup the BPNetworkCvANN_MLP bp;// Set up BPNetwork's parametersCvANN_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 datafloat labels[3][1] = { { -30}, {0 }, { 30 } };Mat labelsMat(3, 1, CV_32FC1, labels);float trainingData[3][3] = { { 244, 152, 213 }, { 244, 152, 198 }, { 244, 152, 165 } };Mat trainingDataMat(3, 3, CV_32FC1, trainingData);Mat layerSizes = (Mat_<int>(1, 5) << 3, 2, 2, 2, 1);bp.create(layerSizes, CvANN_MLP::SIGMOID_SYM);//CvANN_MLP::SIGMOID_SYM//CvANN_MLP::GAUSSIAN//CvANN_MLP::IDENTITYbp.train(trainingDataMat, labelsMat, Mat(), Mat(), params);// Data for visual representationint 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 SVMfor (int i = 0; i < image.rows; ++i)for (int j = 0; j < image.cols; ++j){Mat sampleMat = (Mat_<float>(1, 3) << i, j, 0);Mat responseMat;bp.predict(sampleMat, responseMat);float* p = responseMat.ptr<float>(0);//float response = 0.0f;//for (int k = 0; k<3; k++){////cout<<p[k]<<" ";//response += p[k];//}if (p[0] >0)image.at<Vec3b>(j, i) = green;elseimage.at<Vec3b>(j, i) = blue;}// Show the training dataint 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 userwaitKey(0);}


0 0