使用SVM進行非線性迴歸(Do non-linear regression with OpenCV’s SVM tool)

來源:互聯網
上載者:User

OpenCV整合的東西越來越多了,不用費勁去配置很多環境,這點還是挺方便的,原來一直用SVM進行分類,最近了研究一下使用SVM進行迴歸,發現還是很好用的。

下面就用OpenCV的SVM工具對Sinc函數的樣本進行迴歸,代碼比較簡單,效果還不錯。

本文為原創,轉載請註明,本文地址:http://blog.csdn.net/houston11235/article/details/9023229。

從Sinc函數獲得的樣本點如所示,這是沒有雜訊時候的情形(Data without noise)

加入雜訊之後的樣本如(Add noise to data)

使用SVM進行迴歸之後的結果如(Result)

最後附上原始碼,其中的參數設定參考了以下連結:http://dlib.net/svr_ex.cpp.html

// draw some samples from sinc function and do a non-linear regression#include <opencv2/opencv.hpp>#include <iostream>using namespace std;using namespace cv;// the sinc functionfloat sinc(float x){return static_cast<float>( x==0 ? 1.0 : sin(x) / x );}int main(int argc, char* argv[]){RNG rng;int ndata = 10;Mat traindata(ndata, 1, CV_32FC1); // train dataMat label(ndata, 1, CV_32FC1); // train responsefor (int i = 0; i < ndata; ++i){traindata.at<float>(i, 0) = static_cast<float>(i);float noise = static_cast<float>(rng.gaussian(0.1));//noise = 0.0;    // uncomment to eliminate the noiselabel.at<float>(i, 0) = static_cast<float>(sinc(i) + noise);}// show the train dataint width = 500;int height = 500;Mat canvas(height, width, CV_8UC3, Scalar(0,0,0));double minV;double maxV;Point minId;Point maxId;minMaxLoc(traindata, &minV, &maxV, &minId, &maxId);float X_shift = static_cast<float>(minV);float X_ratio = static_cast<float>(width) / static_cast<float>(maxV - minV);minMaxLoc(label, &minV, &maxV, &minId, &maxId);float Y_shift = static_cast<float>(minV);float Y_ratio = static_cast<float>(height) / static_cast<float>(maxV - minV);for (int idx = 0; idx < traindata.rows; ++idx){float x = (traindata.at<float>(idx, 0) - X_shift) * X_ratio;float y = static_cast<float>(height) - (label.at<float>(idx, 0) - Y_shift) * Y_ratio;circle(canvas, Point2f(x, y), 3, Scalar(0,0,255), -1);}imshow("train", canvas);//imwrite("train_noise.png", canvas);CvSVMParams param;param.svm_type = CvSVM::EPS_SVR;param.kernel_type = CvSVM::RBF;param.C = 5;param.p = 1e-3;param.gamma = 0.1;CvSVM regresser;regresser.train(traindata, label, Mat(), Mat(), param);// predict the responses of the samples and show themfor (float i = 0; i < 10; i+=0.23f){Mat sample(1,1, CV_32FC1);sample.at<float>(0, 0) = static_cast<float>(i);float response = regresser.predict(sample);//cout<<response<<endl;float x = (sample.at<float>(0, 0) - X_shift) * X_ratio;float y = static_cast<float>(height) - (response - Y_shift) * Y_ratio;circle(canvas, Point2f(x, y), 3, Scalar(0,255,0), -1);}imshow("predict", canvas);//imwrite("regress.png", canvas);waitKey(0);return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.