其中 \(S\) 是模糊(退化)图像的频谱,\(U\) 是原始真实(未退化)图像的频谱,\(H\) 是点扩散函数(PSF)的频率响应,\(N\) 是加性噪声的频谱。
#include <iostream>
void help();
void calcPSF(
Mat& outputImg,
Size filterSize,
int R);
void fftshift(
const Mat& inputImg,
Mat& outputImg);
void filter2DFreq(
const Mat& inputImg,
Mat& outputImg,
const Mat& H);
void calcWnrFilter(
const Mat& input_h_PSF,
Mat& output_G,
double nsr);
"{help h usage ? | | 打印此消息 }"
"{image |original.jpg | input image name }"
"{R |5 | radius }"
"{SNR |100 | signal to noise ratio}"
;
int main(
int argc,
char *argv[])
{
help();
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
int R = parser.get<int>("R");
int snr = parser.get<int>("SNR");
string strInFileName = parser.get<
String>(
"image");
samples::addSamplesDataSearchSubDirectory("doc/tutorials/imgproc/out_of_focus_deblur_filter/images");
if (!parser.check())
{
parser.printErrors();
return 0;
}
imgIn = imread(samples::findFile( strInFileName ), IMREAD_GRAYSCALE);
{
cout << "ERROR : Image cannot be loaded..!!" << endl;
return -1;
}
calcPSF(h, roi.
size(), R);
calcWnrFilter(h, Hw, 1.0 / double(snr));
filter2DFreq(imgIn(roi), imgOut, Hw);
normalize(imgOut, imgOut, 0, 255, NORM_MINMAX);
imshow("Original", imgIn);
imshow("Debluring", imgOut);
imwrite("result.jpg", imgOut);
waitKey(0);
return 0;
}
void help()
{
cout << "2018-07-12" << endl;
cout << "DeBlur_v8" << endl;
cout << "You will learn how to recover an out-of-focus image by Wiener filter" << endl;
}
void calcPSF(
Mat& outputImg,
Size filterSize,
int R)
{
circle(h, point, R, 255, -1, 8);
outputImg = h / summa[0];
}
void fftshift(
const Mat& inputImg,
Mat& outputImg)
{
outputImg = inputImg.
clone();
int cx = outputImg.
cols / 2;
int cy = outputImg.
rows / 2;
Mat q0(outputImg,
Rect(0, 0, cx, cy));
Mat q1(outputImg,
Rect(cx, 0, cx, cy));
Mat q2(outputImg,
Rect(0, cy, cx, cy));
Mat q3(outputImg,
Rect(cx, cy, cx, cy));
q3.copyTo(q0);
q1.copyTo(tmp);
q2.copyTo(q1);
}
void filter2DFreq(
const Mat& inputImg,
Mat& outputImg,
const Mat& H)
{
merge(planes, 2, complexI);
dft(complexI, complexI, DFT_SCALE);
merge(planesH, 2, complexH);
idft(complexIH, complexIH);
split(complexIH, planes);
outputImg = planes[0];
}
void calcWnrFilter(
const Mat& input_h_PSF,
Mat& output_G,
double nsr)
{
fftshift(input_h_PSF, h_PSF_shifted);
merge(planes, 2, complexI);
pow(abs(planes[0]), 2, denom);
denom += nsr;
divide(planes[0], denom, output_G);
}
如果数组没有元素,则返回 true。
int64_t int64
从 Mat 派生的模板矩阵类。
定义 mat.hpp:2257
CV_NODISCARD_STD Mat clone() const
创建数组及其底层数据的完整副本。
MatSize size
定义 mat.hpp:2187
void copyTo(OutputArray m) const
将矩阵复制到另一个矩阵。
cv::getTickFrequency
double getTickFrequency()
int rows
行数和列数,如果矩阵有超过2个维度,则为 (-1, -1)
定义 mat.hpp:2165
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
使用可选缩放将数组转换为另一种数据类型。
2D 矩形的模板类。
定义 types.hpp:444
Size_< _Tp > size() const
矩形的大小 (宽度, 高度)
用于指定图像或矩形大小的模板类。
Definition types.hpp:335
_Tp height
高度
Definition types.hpp:363
_Tp width
宽度
Definition types.hpp:362
void split(const Mat &src, Mat *mvbegin)
将多通道数组拆分为多个单通道数组。
void mulSpectrums(InputArray a, InputArray b, OutputArray c, int flags, bool conjB=false)
对两个傅里叶频谱进行逐元素乘法。
void divide(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1)
对两个数组或标量与数组进行逐元素除法。
Scalar sum(InputArray src)
计算数组元素的和。
void merge(const Mat *mv, size_t count, OutputArray dst)
将多个单通道数组合并为一个多通道数组。
void idft(InputArray src, OutputArray dst, int flags=0, int nonzeroRows=0)
计算一维或二维数组的逆离散傅里叶变换。
void dft(InputArray src, OutputArray dst, int flags=0, int nonzeroRows=0)
对一维或二维浮点数组执行正向或逆向离散傅里叶变换。
std::string String
定义 cvstd.hpp:151
#define CV_32F
Definition interface.h:78
@ circle
定义 gr_skig.hpp:62
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
使用了维纳滤波器,并手动选择了 \(R\) 和 \(SNR\) 的值以提供最佳的视觉效果。我们可以看到结果并不完美,但它为我们提供了图像内容的线索。经过一些努力,文本是可以阅读的。