Opencv 机器学习 快速入手小程序

OpenCV机器学习库中主要实现算法如下:
1)一般贝叶斯分类器(Normal Bayes Classifier):CvNormalBayesClassifier
2)K近邻分类(K-nearest Neighbor Classifier): CvKNearest
3)支持向量机(Support Vector Machine):CvSVM
4)期望最大化(Expection Maximization): EM
5)决策树(Decision Tree):CvDTree
6)随机森林(Random Treess Classifier):CvForestTree
7)超随机树分类器(Extremely randomized trees Classifier): CvERTrees
8)Boost分类器(Boosted tree Classifier): CvBoost
9)梯度下降Boost分类器(Gradient Boosted Trees):CvGBTrees
10)神经网络(Artificial Neural Networks): CvANN_MLP

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace cv;
using namespace std;

/************************************************************************/
/* K-Nearest Neighbour Classifier(K-邻近算法);                       */
/************************************************************************/
void KNN()
{
    float labels[10] = {0,0,0,0,0,1,1,1,1,1};
    Mat labelsMat(10, 1, CV_32FC1, labels);
    cout<<labelsMat<<endl;
    float trainingData[10][2];
    srand(time(0)); 
    for(int i=0;i<5;i++)
    {
        trainingData[i][0] = rand()%100+1;
        trainingData[i][1] = rand()%100+1;
        trainingData[i+5][0] = rand()%100+100;
        trainingData[i+5][1] = rand()%100+100;
    }
    Mat trainingDataMat(10, 2, CV_32FC1, trainingData);
    cout<<trainingDataMat<<endl;
    KNearest knn;
    knn.train(trainingDataMat,labelsMat,Mat(), false, 2 );

    //train( const cv::Mat& trainData, const cv::Mat& responses,
    //  const cv::Mat& sampleIdx=cv::Mat(), bool isRegression=false,
    //  int maxK=32, bool updateBase=false );
    // Data for visual representation
    int width =200, height =200;
    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){
            const Mat sampleMat = (Mat_<float>(1,2) << i,j);
            Mat response(1, 2, CV_32FC1);
            float result = knn.find_nearest(sampleMat,2,&response);

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

    // Show the training data
    for(int i=0;i<5;i++){
        circle( image, Point(trainingData[i][0],  trainingData[i][1]), 
            5, Scalar(  0,   0,   0), -1, 8);
        circle( image, Point(trainingData[i+5][0],  trainingData[i+5][1]), 
            5, Scalar(255, 255, 255), -1, 8);
    }
    imshow("KNN Simple Example", image); // show it to the user
    waitKey();

}

void  Kmeans()
{
    const int MAX_CLUSTERS = 5;
    Scalar colorTab[] =     //因为最多只有5类,所以最多也就给5个颜色
    {
        Scalar(0, 0, 255),
        Scalar(0,255,0),
        Scalar(255,100,100),
        Scalar(255,0,255),
        Scalar(0,255,255)
    };

    Mat img(500, 500, CV_8UC3);
    RNG rng(12345); //随机数产生器

    for(;;)
    {
        int k, clusterCount = rng.uniform(2, MAX_CLUSTERS+1);
        int i, sampleCount = rng.uniform(1, 1001);
        Mat points(sampleCount, 1, CV_32FC2), labels;   //产生的样本数,实际上为2通道的列向量,元素类型为Point2f

        clusterCount = MIN(clusterCount, sampleCount);
        Mat centers(clusterCount, 1, points.type());    //用来存储聚类后的中心点

        /* generate random sample from multigaussian distribution */
        for( 
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值