您的位置:首页 > 其它

EmguCv3中使用决策树

2015-09-30 00:27 357 查看
由于我训练的范围太小,图片也要放大到像素级才能看得清。

public Form1()
{
InitializeComponent();
DTrees tree = new DTrees();
float[,] fdata = new float[5, 2] { { 1, 1 }, { 2, 2 }, { 1, 0 }, { 0, 1 }, { 0, 0 } };
Image<Gray, float> data = new Image<Gray, float>(5, 2);
float[] fresponses = new float[5] { 1, 1, 0, 0, 0 };
Image<Gray, float> responses = new Image<Gray, float>(5, 1);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 2; j++)
data[j, i] = new Gray(fdata[i, j]);
}
for (int i = 0; i < 5; i++)
{
responses[0, i] = new Gray(fresponses[i]);
}
tree.Use1SERule = true;
tree.UseSurrogates = false;
tree.TruncatePrunedTree = true;
tree.MaxDepth = 8;
tree.MinSampleCount = 1;
tree.RegressionAccuracy = 0;
tree.MaxCategories = 15;
tree.CVFolds = 0;

try
{
tree.Train(data, DataLayoutType.ColSample, responses);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Image<Bgr, byte> aaa = new Image<Bgr, byte>(4, 4);
for (int i = 0; i < aaa.Height; i++)
for (int j = 0; j < aaa.Width; j++)
{
//int k = i * 512 + j;
Image<Gray, float> bbb = new Image<Gray, float>(5, 1);
bbb[0, 0] = new Gray(i);
bbb[0, 1] = new Gray(j);
bbb[0, 2] = bbb[0, 3] = bbb[0, 4] = new Gray(0);
//Mat res = new Mat();
float tt = tree.Predict(bbb);
if (tt == 1)
aaa[i, j] = new Bgr(0, 0, 255);
else if (tt == 0)
aaa[i, j] = new Bgr(0, 255, 0);
}
//CvInvoke.Circle(aaa, new Point(100, 250), 5, new MCvScalar(0, 0, 0), -1);
//CvInvoke.Circle(aaa, new Point(75, 11), 5, new MCvScalar(0, 0, 0), -1);
//CvInvoke.Circle(aaa, new Point(500, 400), 5, new MCvScalar(255, 255, 255), -1);
//CvInvoke.Circle(aaa, new Point(350, 20), 5, new MCvScalar(255, 255, 255), -1);
//CvInvoke.Circle(aaa, new Point(190, 100), 5, new MCvScalar(255, 255, 255), -1);
aaa[1, 1] = new Bgr(0, 0, 0);
aaa[2, 2] = new Bgr(0, 0, 0);
aaa[0, 0] = new Bgr(255, 255, 255);
aaa[1, 0] = new Bgr(255, 255, 255);
aaa[0, 1] = new Bgr(255, 255, 255);
imageBox1.Image = aaa;
}


效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: