CPP code fro deblurring an image using Weiner filter.
I need a code to deblur an image using weiner filter... I am not sure what to do... I was able to denoise the image using this code.. What changes should be done in the code to deblur the image as well.
enter code here//#include <boost/program_options.hpp>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
//namespace po = boost::program_options;
Mat wiener2(Mat I, Mat image_spectrum, int noise_stddev);
Mat padd_image(Mat I);
Mat get_spectrum(Mat I);
Mat get_dft(Mat I);
Mat with_noise(Mat image, int stddev);
Mat rand_noise(Mat I, int stddev);
int main(int argc, char *argv[]) {
int noise_stddev=50;
string input_filename="lena.png", output_filename="write.png";
cout << "noise standard deviation: " << noise_stddev << "\n";
cout << "input file: " << input_filename << "\n";
/*if (vm.count("help")) {
cout << desc << "\n";
return 1;
}*/
Mat I = imread(input_filename, CV_LOAD_IMAGE_GRAYSCALE);
if(I.data==NULL){
cout << "Can't open file: " << input_filename << "\n";
return 2;
}
Mat raw_sample = imread("lena.png", CV_LOAD_IMAGE_GRAYSCALE);
if(raw_sample.data==NULL){
cout << "Can't open file: sample.bmp\n";
return 3;
}
Mat padded = padd_image(I);
Mat noisy;
//if(vm.count("generate-noisy")){
noisy = with_noise(padded, noise_stddev);
//imwrite(output_filename, noisy);
//return 0;
//}else{
// noisy = padded;
//}
Mat sample(padded.rows, padded.cols, CV_8U);
resize(raw_sample, sample, sample.size());
Mat spectrum = get_spectrum(sample);
Mat enhanced = wiener2(noisy, spectrum, noise_stddev);
//imwrite(output_filename, enhanced);
//if(vm.count("show")){
imshow("image 1", noisy);
imshow("image 2", enhanced);
//}
waitKey();
}
Mat wiener2(Mat I, Mat image_spectrum, int noise_stddev){
Mat padded = padd_image(I);
Mat noise = rand_noise(padded, noise_stddev);
Mat noise_spectrum = get_spectrum(noise);
Scalar padded_mean = mean(padded);
Mat planes[2];
Mat complexI = get_dft(padded);
split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
Mat factor = image_spectrum / (image_spectrum + noise_spectrum);
multiply(planes[0],factor,planes[0]);
multiply(planes[1],factor,planes[1]);
merge(planes, 2, complexI);
idft(complexI, complexI);
split(complexI, planes);
// normalize(planes[0], planes[0], 0, 128, CV_MINMAX );
Scalar enhanced_mean = mean(planes[0]);
double norm_factor = padded_mean.val[0] / enhanced_mean.val[0];
multiply(planes[0],norm_factor, planes[0]);
Mat normalized;
planes[0].convertTo(normalized, CV_8UC1);
return normalized;
}
Mat padd_image(Mat I){
Mat padded;
int m = getOptimalDFTSize( I.rows );
int n = getOptimalDFTSize( I.cols ); // on the border add zero pixels
copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
return padded;
}
Mat get_spectrum(Mat I){
Mat complexI = get_dft(I);
Mat planes[2];
split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
Mat magI = planes[0];
multiply(magI,magI,magI);
return magI;
}
Mat get_dft(Mat I){
Mat image;
I.convertTo(image, CV_32F);
Mat planes[] = {Mat_<float>(image), Mat::zeros(image.size(), CV_32F)};
Mat complexI;
merge(planes, 2, complexI);
dft(complexI, complexI);
return complexI;
}
Mat with_noise(Mat image, int stddev){
Mat noise(image.rows, image.cols, CV_8U);
rand_noise(image, stddev).convertTo(noise, CV_8U);
Mat noisy = image.clone();
noisy += noise ...
do a dead man a favour, and spell his name correctly
(and yea, i know, it's already wrong in the python sample)