Post by **Creator** » Mon May 08, 2017, 22:07

As far as I understand, you can use the DGM library for your code. All the examples for the DGM library are given for 2D cases, but it is not difficult to extend the model for a 3D case.

First of all, you have to build a graphical model, using the CGraph class (STAGE 1). The graphical model has nodes and edges and you have to fill them with node and edge potentials.

Since you have 3 classes, the length of your node potentials is 3: that's it, 3 potentials (or probabilities) to be the class 0, 1 and 2 ,respectively. And your edge potentials are square matrices 3x3.

In order to train node potentials with the Random Forest (RF) classifier, you may use CTrainNodeCvRF class. Its method CTrainNodeCvRF::getNodePotential() will return the array of 3 numbers – potentials.

In case, if you use external RF classifier, you might get from it the predicted class, one number (

*i.e.* 0, 1 or 2). In order to convert it to potential I suggest you to use the following idea:

Code: Select all

`// inside STAGE 3`

const float lambda = 0.33; // it must me in the interval [0; 0.5) and you have to play with it in order to achieve best results

for (i : all nodes) {

int my_prediction = my_RF_classifier.get (i); // the predicted class

Mat node_potential (3, 1, CV_32FC1);

node_potential.setTo(lambda);

node_potential.at<float>(my_prediction, 0) = 1.0f – lambda;

graph.setNode(I, node_potential);

}

The edge potentials should be set to your transition matrices.

Note, that in case when you use your own RF classifier for nodes, and transition matrices for edges, you do not need STAGE 2 from the example.

I hope that helps.

As far as I understand, you can use the DGM library for your code. All the examples for the DGM library are given for 2D cases, but it is not difficult to extend the model for a 3D case.

First of all, you have to build a graphical model, using the CGraph class (STAGE 1). The graphical model has nodes and edges and you have to fill them with node and edge potentials.

Since you have 3 classes, the length of your node potentials is 3: that's it, 3 potentials (or probabilities) to be the class 0, 1 and 2 ,respectively. And your edge potentials are square matrices 3x3.

In order to train node potentials with the Random Forest (RF) classifier, you may use CTrainNodeCvRF class. Its method CTrainNodeCvRF::getNodePotential() will return the array of 3 numbers – potentials.

In case, if you use external RF classifier, you might get from it the predicted class, one number ([i]i.e.[/i] 0, 1 or 2). In order to convert it to potential I suggest you to use the following idea:

[code]

// inside STAGE 3

const float lambda = 0.33; // it must me in the interval [0; 0.5) and you have to play with it in order to achieve best results

for (i : all nodes) {

int my_prediction = my_RF_classifier.get (i); // the predicted class

Mat node_potential (3, 1, CV_32FC1);

node_potential.setTo(lambda);

node_potential.at<float>(my_prediction, 0) = 1.0f – lambda;

graph.setNode(I, node_potential);

}

[/code]

The edge potentials should be set to your transition matrices.

Note, that in case when you use your own RF classifier for nodes, and transition matrices for edges, you do not need STAGE 2 from the example.

I hope that helps.