Skip to content

Commit

Permalink
Merging
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibaud EHRET committed Jan 12, 2020
2 parents bbf88f8 + b5cab82 commit a700b6c
Show file tree
Hide file tree
Showing 3 changed files with 632 additions and 516 deletions.
267 changes: 139 additions & 128 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ void initializeParameters_1(
prms.Nb = Nb;

if(d < 0)
prms.d = (7.*7.)/(255.*prms.k*prms.k);
prms.d = (7.*7.*255.)/(prms.k*prms.k);
else
prms.d = (d*d)/(255.*prms.k*prms.k);
prms.d = (d*d*255.)/(prms.k*prms.k);

if(p < 0)
prms.p = 6;
// prms.p = 6;
prms.p = prms.k/4*3;
else
prms.p = p;

Expand All @@ -109,7 +110,7 @@ void initializeParameters_1(
prms.N = N;

if(tau < 0)
prms.tau = (sigma > 30) ? 4500/(255.*255.) : 3000/(255.*255.);
prms.tau = (sigma > 30) ? 4500 : 3000;
else
prms.tau = tau;

Expand All @@ -119,19 +120,19 @@ void initializeParameters_1(
prms.lambda3D = lambda3D;

if(T_2D == NONE)
{
if(prms.k == 8)
prms.T_2D = BIOR;
else
prms.T_2D = DCT;
}
{
if(prms.k == 8)
prms.T_2D = BIOR;
else
prms.T_2D = DCT;
}
else
{
if(prms.k == 8)
prms.T_2D = T_2D;
else
prms.T_2D = DCT;
}
{
if(prms.k == 8)
prms.T_2D = T_2D;
else
prms.T_2D = DCT;
}

if(T_3D == NONE)
prms.T_3D = HAAR;
Expand Down Expand Up @@ -188,12 +189,13 @@ void initializeParameters_2(
prms.Nb = Nb;

if(d < 0)
prms.d = (3.*3.)/(255.*prms.k*prms.k);
prms.d = (3.*3.*255.)/(prms.k*prms.k);
else
prms.d = (d*d)/(255.*prms.k*prms.k);
prms.d = (d*d*255.)/(prms.k*prms.k);

if(p < 0)
prms.p = 4;
// prms.p = 4;
prms.p = prms.k/2;
else
prms.p = p;

Expand All @@ -203,26 +205,50 @@ void initializeParameters_2(
prms.N = N;

if(tau < 0)
prms.tau = (sigma > 30) ? 3000/(255.*255.) : 1500/(255.*255.);
prms.tau = (sigma > 30) ? 3000 : 1500;
else
prms.tau = tau;

if(T_2D == NONE)
prms.T_2D = DCT;
else
{
if(prms.k == 8)
prms.T_2D = T_2D;
else
prms.T_2D = DCT;
}
{
if(prms.k == 8)
prms.T_2D = T_2D;
else
prms.T_2D = DCT;
}

if(T_3D == NONE)
prms.T_3D = HAAR;
else
prms.T_3D = T_3D;
}

void printParameters(const Parameters& prms, const string suffix)
{
const char *s = suffix.c_str();
if (!prms.k)
{
printf("%s step skipped\n", s);
return;
}

printf("Parameters %s step:\n", s);
printf("\tpatch size k x k x kt = %dx%dx%d\n", prms.k, prms.k, prms.kt);
printf("\tprocessing step p = %d\n", prms.p);
printf("\tpred. search Nf = %d\n", prms.Nf);
printf("\tpred. search Ns = %d\n", prms.Ns);
printf("\tpred. search Npr = %d\n", prms.Npr);
printf("\tpred. search Nb = %d\n", prms.Nb);
printf("\tpred. search d = %f\n", prms.d);
printf("\tpred. search N = %d\n", prms.N);
printf("\tpred. search tau = %f\n", prms.tau);
printf("\ttransform T_2D = %d\n", prms.T_2D);
printf("\ttransform T_3D = %d\n", prms.T_3D);
return;
}


/**
* @file main.cpp
Expand All @@ -236,12 +262,12 @@ int main(int argc, char **argv)
//! Check if there is the right call for the algorithm
using std::string;
const string input_path = clo_option("-i" , "" , "< Input sequence");
const string inbsc_path = clo_option("-b" , "" , "< Input basic sequence (it will replace the basic estimation step)");
const string noisy_path = clo_option("-nisy" , "" , "> Noisy sequence");
const string inbsc_path = clo_option("-b" , "" , "< Input basic sequence (replacing first pass)");
const string noisy_path = clo_option("-nisy" , "" , "> Noisy sequence (only when -add is true)");
const string final_path = clo_option("-deno" , "deno_%03d.tiff" , "> Denoised sequence");
const string basic_path = clo_option("-bsic" , "" , "> Basic denoised sequence");
const string diff_path = clo_option("-diff" , "" , "> Difference sequence");
const string meas_path = clo_option("-meas" , "measure.txt" , "> Text file containing the measures (only reliable when -add is set to true)");
const string basic_path = clo_option("-bsic" , "bsic_%03d.tiff" , "> Basic denoised sequence");
const string diff_path = clo_option("-diff" , "" , "> Difference sequence (only when -add is true)");
const string meas_path = clo_option("-meas" , "measure.txt" , "> Text file with PSNR/RMSE (only when -add is true)");
#ifdef OPTICALFLOW
const string fflow_path = clo_option("-fflow", "" , "< Forward optical flow ");
const string bflow_path = clo_option("-bflow", "" , "< Backward optical flow ");
Expand All @@ -268,6 +294,8 @@ int main(int argc, char **argv)
const int dHard = clo_option("-dHard", -1, "< Bias toward center patches (first pass)");
const float tauHard = clo_option("-tauHard", -1., "< Distance threshold on neighbors (first pass)");
const float lambda3D = clo_option("-lambda3d", -1., "< Coefficient threhsold (first pass)");
const unsigned T_2D_hard = (unsigned) clo_option("-T2dh", NONE , "< 2D transform (first pass), choice is 4 (dct) or 5 (bior)");
const unsigned T_3D_hard = (unsigned) clo_option("-T3dh", NONE , "< 1D transform (first pass), choice is 6 (hadamard) or 7 (haar)");

const int kWien = clo_option("-kWien", -1, "< Spatial size of the patch (second pass)");
const int ktWien = clo_option("-ktWien", -1, "< Temporal size of the patch (second pass)");
Expand All @@ -279,70 +307,64 @@ int main(int argc, char **argv)
const int NWien = clo_option("-NWien", -1, "< Maximum number of neighbors (second pass)");
const int dWien = clo_option("-dWien", -1, "< Bias toward center patches (second pass)");
const float tauWien = clo_option("-tauWien", -1., "< Distance threshold on neighbors (second pass)");
const unsigned T_2D_wien = (unsigned) clo_option("-T2dw", NONE , "< 2D transform (second pass), choice is 4 (dct) or 5 (bior)");
const unsigned T_3D_wien = (unsigned) clo_option("-T3dw", NONE , "< 1D transform (first pass), choice is 6 (hadamard) or 7 (haar)");

const unsigned color_space = (unsigned) clo_option("-color", 0 , "< Set the color space (0 correspond to RGB->OPP, any other value keeps RGB for now)");

//! Check inputs
if (input_path == "")
{
fprintf(stderr, "%s: no input images.\nTry `%s --help' for more information.\n",
argv[0], argv[0]);
return EXIT_FAILURE;
}
return fprintf(stderr, "%s: no input images.\n"
"Try `%s --help' for more information.\n", argv[0], argv[0]),
EXIT_FAILURE;
#ifdef OPTICALFLOW
if (fflow_path == "" || bflow_path == "")
{
fprintf(stderr, "%s: no forward and backward flows.\nTry `%s --help' for more information.\n",
argv[0], argv[0]);
return EXIT_FAILURE;
}
if (fflow_path == "" || bflow_path == "")
return fprintf(stderr, "%s: no forward and backward flows.\n"
"Try `%s --help' for more information.\n", argv[0], argv[0]),
EXIT_FAILURE;
#endif

//! Variables initialization
const unsigned T_2D_hard = (unsigned) clo_option("-T2dh", NONE , "< 2D transform (first pass), choice is 4 (dct) or 5 (bior)");
//! Check for invalid inputs
if (T_2D_hard != NONE && T_2D_hard != DCT && T_2D_hard != BIOR)
{
cout << "T_2d_hard is not known. Choice is :" << endl;
cout << " dct (" << DCT << ")" << endl;
cout << " bior (" << BIOR << ")" << endl;
return EXIT_FAILURE;
}
return fprintf(stderr, "%s: unknown T_2d_hard."
"Try `%s --help' for the available choices.\n", argv[0], argv[0]),
EXIT_FAILURE;

const unsigned T_2D_wien = (unsigned) clo_option("-T2dw", NONE , "< 2D transform (second pass), choice is 4 (dct) or 5 (bior)");
if (T_2D_wien != NONE && T_2D_wien != DCT && T_2D_wien != BIOR)
{
cout << "T_2d_wien is not known. Choice is :" << endl;
cout << " dct (" << DCT << ")" << endl;
cout << " bior (" << BIOR << ")" << endl;
return EXIT_FAILURE;
};
return fprintf(stderr, "%s: unknown T_2d_wien."
"Try `%s --help' for the available choices.\n", argv[0], argv[0]),
EXIT_FAILURE;

const unsigned T_3D_hard = (unsigned) clo_option("-T3dh", NONE , "< 1D transform (first pass), choice is 6 (hadamard) or 7 (haar)");
if (T_3D_hard != NONE && T_3D_hard != HAAR && T_3D_hard != HADAMARD)
{
cout << "T_3d_hard is not known. Choice is :" << endl;
cout << " haar (" << HAAR << ")" << endl;
cout << " hadamard (" << HADAMARD << ")" << endl;
return EXIT_FAILURE;
}
return fprintf(stderr, "%s: unknown T_3d_hard."
"Try `%s --help' for the available choices.\n", argv[0], argv[0]),
EXIT_FAILURE;

const unsigned T_3D_wien = (unsigned) clo_option("-T3dw", NONE , "< 1D transform (first pass), choice is 6 (hadamard) or 7 (haar)");
if (T_3D_wien != NONE && T_3D_wien != HAAR && T_3D_wien != HADAMARD)
{
cout << "T_3d_wien is not known. Choice is :" << endl;
cout << " haar (" << HAAR << ")" << endl;
cout << " hadamard (" << HADAMARD << ")" << endl;
return EXIT_FAILURE;
};
return fprintf(stderr, "%s: unknown T_3d_wien."
"Try `%s --help' for the available choices.\n", argv[0], argv[0]),
EXIT_FAILURE;

//! Init parameters
Parameters prms_1;
Parameters prms_2;

initializeParameters_1(prms_1, kHard, ktHard, NfHard, NsHard, NprHard, NbHard, pHard, NHard, dHard, tauHard, lambda3D, T_2D_hard, T_3D_hard, fSigma);
initializeParameters_2(prms_2, kWien, ktWien, NfWien, NsWien, NprWien, NbWien, pWien, NWien, dWien, tauWien, T_2D_wien, T_3D_wien, fSigma);
initializeParameters_1(prms_1, kHard, ktHard, NfHard, NsHard, NprHard,
NbHard, pHard, NHard, dHard, tauHard, lambda3D, T_2D_hard, T_3D_hard,
fSigma);
initializeParameters_2(prms_2, kWien, ktWien, NfWien, NsWien, NprWien,
NbWien, pWien, NWien, dWien, tauWien, T_2D_wien, T_3D_wien, fSigma);

//! Print parameters
if (verbose)
{
printParameters(prms_1, "hard thresholding");
printParameters(prms_2, "Wiener filtering");
}

//! Declarations
Video<float> vid, vid_noisy, vid_basic, vid_denoised, vid_diff;

#ifdef OPTICALFLOW
Video<float> fflow;
Video<float> bflow;
Expand All @@ -357,14 +379,12 @@ int main(int argc, char **argv)
fflow.loadFullFlow(fflow_path, firstFrame, lastFrame-1, frameStep);
bflow.loadFullFlow(bflow_path, firstFrame+1, lastFrame, frameStep);

// Check that all sizes are consistent
if(fflow.sz.width != bflow.sz.width || fflow.sz.width != vid.sz.width
|| fflow.sz.height != bflow.sz.height || fflow.sz.height != vid.sz.height)
{
fprintf(stderr, "%s: Sizes (flows and video) are inconsistent.\nTry `%s --help' for more information.\n",
argv[0], argv[0]);
return EXIT_FAILURE;
}
// Check that all sizes are consistent
if(fflow.sz.width != bflow.sz.width || fflow.sz.width != vid.sz.width ||
fflow.sz.height != bflow.sz.height || fflow.sz.height != vid.sz.height)
return fprintf(stderr, "%s: incompatible flows and/or frame sizes.\n"
"Try `%s --help' for more information.\n", argv[0], argv[0]),
EXIT_FAILURE;
#endif

vid_noisy.resize(vid.sz);
Expand All @@ -373,71 +393,62 @@ int main(int argc, char **argv)
//! Add noise
if(addnoise)
{
cout << endl << "Add noise [sigma = " << fSigma << "] ...";
printf("Add noise with sigma = %f to input video\n");
VideoUtils::addNoise(vid, vid_noisy, fSigma, verbose);
cout << "done." << endl;
}
else
{
printf("Noisy input video with sigma = %f\n", fSigma);
vid_noisy = vid;
}

//! Denoising
#ifdef OPTICALFLOW
if (run_vbm3d(fSigma, vid_noisy, fflow, bflow, vid_basic, vid_denoised, prms_1, prms_2, color_space)
!= EXIT_SUCCESS)
if (run_vbm3d(fSigma, vid_noisy, fflow, bflow, vid_basic, vid_denoised,
prms_1, prms_2, color_space) != EXIT_SUCCESS)
return EXIT_FAILURE;
#else
if (run_vbm3d(fSigma, vid_noisy, vid_basic, vid_denoised, prms_1, prms_2, color_space)
!= EXIT_SUCCESS)
if (run_vbm3d(fSigma, vid_noisy, vid_basic, vid_denoised, prms_1, prms_2,
color_space) != EXIT_SUCCESS)
return EXIT_FAILURE;
#endif

//! Compute PSNR and RMSE
double psnr, rmse;
double psnr_basic, rmse_basic;
VideoUtils::computePSNR(vid, vid_basic, psnr_basic, rmse_basic);
VideoUtils::computePSNR(vid, vid_denoised, psnr, rmse);

cout << endl << "For noisy video :" << endl;
cout << "PSNR: " << psnr << endl;
cout << "RMSE: " << rmse << endl << endl;
cout << "(basic video) :" << endl;
cout << "PSNR: " << psnr_basic << endl;
cout << "RMSE: " << rmse_basic << endl << endl;

ofstream file(meas_path, ios::out | ios::trunc);
if(file)
if (addnoise)
{
file << endl << "************" << endl;
file << "-sigma = " << fSigma << endl;
file << "-PSNR_basic = " << psnr_basic << endl;
file << "-RMSE_basic = " << rmse_basic << endl;
file << "-PSNR = " << psnr << endl;
file << "-RMSE = " << rmse << endl << endl;
cout << endl;
file.close();
double psnr, rmse;
double psnr_basic, rmse_basic;
VideoUtils::computePSNR(vid, vid_basic, psnr_basic, rmse_basic);
VideoUtils::computePSNR(vid, vid_denoised, psnr, rmse);

if (verbose)
{
printf("Basic estimate: PSNR= %5.2f RMSE= %6.3f\n", psnr_basic, rmse_basic);
printf("Final estimate: PSNR= %5.2f RMSE= %6.3f\n", psnr, rmse);
}

FILE *file = fopen(meas_path.c_str(), "w");
if(file)
{
fprintf(file, "-sigma = %f\n-PSNR_basic = %f\n-RSME_basic = %f\n"
"-PSNR = %f\n-RMSE = %f\n",
fSigma, psnr_basic, rmse_basic, psnr, rmse);
fclose(file);
}
else fprintf(stderr, "Can't open %s\n", meas_path);

//! Compute Difference
if (diff_path != "")
VideoUtils::computeDiff(vid, vid_denoised, vid_diff, fSigma);

if (noisy_path != "") vid_noisy.saveVideo(noisy_path, firstFrame, frameStep);
if ( diff_path != "") vid_diff .saveVideo( diff_path, firstFrame, frameStep);
}
else
{
cout << "Can't open measures.txt !" << endl;
return EXIT_FAILURE;
}

//! Compute Difference
if (diff_path != "")
{
cout << endl << "Compute difference...";
VideoUtils::computeDiff(vid, vid_denoised, vid_diff, fSigma);
cout << "done." << endl;
}

//! save noisy, denoised and differences videos
cout << endl << "Save videos...";
if (noisy_path != "") vid_noisy.saveVideo(noisy_path, firstFrame, frameStep);
if (verbose) printf("Save output videos...\n");
if (basic_path != "") vid_basic.saveVideo(basic_path, firstFrame, frameStep);
if ( diff_path != "") vid_diff .saveVideo( diff_path, firstFrame, frameStep);
vid_denoised.saveVideo(final_path, firstFrame, frameStep);

cout << "done." << endl;
if (prms_2.k) vid_denoised.saveVideo(final_path, firstFrame, frameStep);

return EXIT_SUCCESS;
}
Loading

0 comments on commit a700b6c

Please sign in to comment.