em_bug.cpp

mr.pppoe -, 2012-01-27 05:18 pm

Download (952 Bytes)

 
1
#include <opencv2/opencv.hpp>
2
#include <iostream>
3
4
using namespace cv;
5
using namespace std;
6
7
int main(int argc, char **argv)
8
{
9
    
10
    CvEM em; 
11
12
    Mat samples = Mat(3,2,CV_32F);
13
    samples.at<float>(0,0) = 1;
14
    samples.at<float>(1,0) = 2;
15
    samples.at<float>(3,0) = 3;
16
17
    CvEMParams params;
18
    params.nclusters = 2;
19
20
    Mat labels;
21
22
    em.train(samples, Mat(), params, &labels); 
23
24
    //< Write Out
25
    {
26
        FileStorage fs = FileStorage("./dump.em", FileStorage::WRITE);
27
        em.write(fs.fs, "EM");
28
    }
29
30
    //< Read In
31
    {
32
        FileStorage fs = FileStorage("./dump.em", FileStorage::READ);
33
        FileNode fileNode = fs["EM"];
34
35
        //< you need the const_cast to get this file compiled, this is not a trivial issue, 
36
        //< since all the read() functions require non-const pointer arguments
37
        em.read(const_cast<CvFileStorage*>(fileNode.fs), const_cast<CvFileNode*>(fileNode.node));
38
    }
39
40
    return 0;
41
}