本文整理汇总了C#中CvMat.Set方法的典型用法代码示例。如果您正苦于以下问题:C# CvMat.Set方法的具体用法?C# CvMat.Set怎么用?C# CvMat.Set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CvMat
的用法示例。
在下文中一共展示了CvMat.Set方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildBoostClassifier
/// <summary>
///
/// </summary>
/// <param name="dataFilename"></param>
/// <param name="filenameToSave"></param>
/// <param name="filenameToLoad"></param>
private void BuildBoostClassifier(string dataFilename, string filenameToSave, string filenameToLoad)
{
const int ClassCount = 26;
CvMat data = null;
CvMat responses = null;
CvMat varType = null;
CvMat tempSample = null;
CvMat weakResponses = null;
int nsamplesAall = 0, ntrainSamples = 0;
int varCount;
double trainHr = 0, testHr = 0;
CvBoost boost = new CvBoost();
try
{
ReadNumClassData(dataFilename, 16, out data, out responses);
}
catch
{
Console.WriteLine("Could not read the database {0}", dataFilename);
return;
}
Console.WriteLine("The database {0} is loaded.", dataFilename);
nsamplesAall = data.Rows;
ntrainSamples = (int)(nsamplesAall * 0.5);
varCount = data.Cols;
// Create or load Boosted Tree classifier
if (filenameToLoad != null)
{
// load classifier from the specified file
boost.Load(filenameToLoad);
ntrainSamples = 0;
if (boost.GetWeakPredictors() == null)
{
Console.WriteLine("Could not read the classifier {0}", filenameToLoad);
return;
}
Console.WriteLine("The classifier {0} is loaded.", filenameToLoad);
}
else
{
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
// As currently boosted tree classifier in MLL can only be trained
// for 2-class problems, we transform the training database by
// "unrolling" each training sample as many times as the number of
// classes (26) that we have.
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
using (CvMat newData = new CvMat(ntrainSamples * ClassCount, varCount + 1, MatrixType.F32C1))
using (CvMat newResponses = new CvMat(ntrainSamples * ClassCount, 1, MatrixType.S32C1))
{
// 1. unroll the database type mask
Console.WriteLine("Unrolling the database...");
for (int i = 0; i < ntrainSamples; i++)
{
unsafe
{
float* dataRow = (float*)(data.DataByte + data.Step * i);
for (int j = 0; j < ClassCount; j++)
{
float* newDataRow = (float*)(newData.DataByte + newData.Step * (i * ClassCount + j));
for (int k = 0; k < varCount; k++)
{
newDataRow[k] = dataRow[k];
}
newDataRow[varCount] = (float)j;
newResponses.DataInt32[i * ClassCount + j] = (responses.DataSingle[i] == j + 'A') ? 1 : 0;
}
}
}
// 2. create type mask
varType = new CvMat(varCount + 2, 1, MatrixType.U8C1);
varType.Set(CvScalar.ScalarAll(CvStatModel.CV_VAR_ORDERED));
// the last indicator variable, as well
// as the new (binary) response are categorical
varType.SetReal1D(varCount, CvStatModel.CV_VAR_CATEGORICAL);
varType.SetReal1D(varCount + 1, CvStatModel.CV_VAR_CATEGORICAL);
// 3. train classifier
Console.Write("Training the classifier (may take a few minutes)...");
boost.Train(
newData, DTreeDataLayout.RowSample, newResponses, null, null, varType, null,
new CvBoostParams(CvBoost.REAL, 100, 0.95, 5, false, null)
);
}
Console.WriteLine();
//.........这里部分代码省略.........
示例2: BuildRtreesClassifier
/// <summary>
/// RTrees
/// </summary>
/// <param name="dataFilename"></param>
/// <param name="filenameToSave"></param>
/// <param name="filenameToLoad"></param>
private void BuildRtreesClassifier(string dataFilename, string filenameToSave, string filenameToLoad)
{
CvMat data = null;
CvMat responses = null;
CvMat varType = null;
CvMat sampleIdx = null;
int nsamplesAll = 0, ntrainSamples = 0;
double trainHr = 0, testHr = 0;
CvRTrees forest = new CvRTrees();
try
{
ReadNumClassData(dataFilename, 16, out data, out responses);
}
catch
{
Console.WriteLine("Could not read the database {0}", dataFilename);
return;
}
Console.WriteLine("The database {0} is loaded.", dataFilename);
nsamplesAll = data.Rows;
ntrainSamples = (int)(nsamplesAll * 0.8);
// Create or load Random Trees classifier
if (filenameToLoad != null)
{
// load classifier from the specified file
forest.Load(filenameToLoad);
ntrainSamples = 0;
if (forest.GetTreeCount() == 0)
{
Console.WriteLine("Could not read the classifier {0}", filenameToLoad);
return;
}
Console.WriteLine("The classifier {0} is loaded.", filenameToLoad);
}
else
{
// create classifier by using <data> and <responses>
Console.Write("Training the classifier ...");
// 1. create type mask
varType = new CvMat(data.Cols + 1, 1, MatrixType.U8C1);
varType.Set(CvScalar.ScalarAll(CvStatModel.CV_VAR_ORDERED));
varType.SetReal1D(data.Cols, CvStatModel.CV_VAR_CATEGORICAL);
// 2. create sample_idx
sampleIdx = new CvMat(1, nsamplesAll, MatrixType.U8C1);
{
CvMat mat;
Cv.GetCols(sampleIdx, out mat, 0, ntrainSamples);
mat.Set(CvScalar.RealScalar(1));
Cv.GetCols(sampleIdx, out mat, ntrainSamples, nsamplesAll);
mat.SetZero();
}
// 3. train classifier
forest.Train(
data, DTreeDataLayout.RowSample, responses, null, sampleIdx, varType, null,
new CvRTParams(10, 10, 0, false, 15, null, true, 4, new CvTermCriteria(100, 0.01f))
);
Console.WriteLine();
}
// compute prediction error on train and test data
for (int i = 0; i < nsamplesAll; i++)
{
double r;
CvMat sample;
Cv.GetRow(data, out sample, i);
r = forest.Predict(sample);
r = Math.Abs((double)r - responses.DataArraySingle[i]) <= float.Epsilon ? 1 : 0;
if (i < ntrainSamples)
trainHr += r;
else
testHr += r;
}
testHr /= (double)(nsamplesAll - ntrainSamples);
trainHr /= (double)ntrainSamples;
Console.WriteLine("Recognition rate: train = {0:F1}%, test = {1:F1}%", trainHr * 100.0, testHr * 100.0);
Console.WriteLine("Number of trees: {0}", forest.GetTreeCount());
// Print variable importance
Mat varImportance0 = forest.GetVarImportance();
CvMat varImportance = varImportance0.ToCvMat();
if (varImportance != null)
//.........这里部分代码省略.........