In this demo, we consider the case of binary variables with attractive potentials. The original binary image Smile.png is degraded with noise, as showed at image Smile_noise.png. Using the DGM we restore the original image from its noised version and evaluate the results by comparing the restored image with the original one.
Input | | Output |
|
|
|
|
Smile.png | Smile_noise.png | | Restored Image |
This example copies the idea from the GraphCuts UGM Demo
#include "DGM.h"
int main(int argc, char *argv[])
{
if (argc != 3) {
print_help(argv[0]);
return 0;
}
const unsigned int nStates = 2;
Mat img = imread(argv[1], 0); if (img.empty()) printf("Can't open %s\n", argv[1]);
Mat noise = imread(argv[2], 0); if (noise.empty()) printf("Can't open %s\n", argv[2]);
vec_mat_t p(nStates);
noise.convertTo(p[0], CV_32FC1, -1.0 / 255, 1.0);
noise.convertTo(p[1], CV_32FC1, 1.0 / 255);
Mat nodePot;
merge(p, nodePot);
graphExt.setGraph(nodePot);
graphExt.addDefaultEdgesModel(10000, 3);
vec_byte_t optimalDecoding = decoder.decode(100);
noise = Mat(noise.size(), CV_8UC1, optimalDecoding.data()) * 255;
medianBlur(noise, noise, 3);
float error = 0;
for (int y = 0; y < img.rows; y++)
for (int x = 0; x < img.cols; x++)
if (noise.at<byte>(y,x) != img.at<byte>(y,x)) error++;
printf("Accuracy = %.2f%%\n", 100 - 100 * error / (img.cols * img.rows));
imshow("image", noise);
waitKey();
return 0;
}