您可以在 OpenCV 源代码库的 `samples/cpp/tutorial_code/ImgProc/motion_deblur_filter/motion_deblur_filter.cpp` 中找到源代码。
#include <iostream>
void help();
void calcPSF(
Mat& outputImg,
Size filterSize,
int len,
double theta);
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);
void edgetaper(
const Mat& inputImg,
Mat& outputImg,
double gamma = 5.0,
double beta = 0.2);
"{help h usage ? | | 打印此消息 }"
"{image |input.png | input image name }"
"{LEN |125 | length of a motion }"
"{THETA |0 | angle of a motion in degrees }"
"{SNR |700 | signal to noise ratio }"
;
int main(
int argc,
char *argv[])
{
help();
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
int LEN = parser.get<int>("LEN");
double THETA = parser.get<double>("THETA");
int snr = parser.get<int>("SNR");
string strInFileName = parser.get<
String>(
"image");
if (!parser.check())
{
parser.printErrors();
return 0;
}
imgIn = imread(strInFileName, IMREAD_GRAYSCALE);
{
cout << "ERROR : Image cannot be loaded..!!" << endl;
return -1;
}
calcPSF(h, roi.
size(), LEN, THETA);
calcWnrFilter(h, Hw, 1.0 / double(snr));
edgetaper(imgIn, imgIn);
filter2DFreq(imgIn(roi), imgOut, Hw);
normalize(imgOut, imgOut, 0, 255, NORM_MINMAX);
imwrite("result.jpg", imgOut);
return 0;
}
void help()
{
cout << "2018-08-14" << endl;
cout << "Motion_deblur_v2" << endl;
cout << "You will learn how to recover an image with motion blur distortion using a Wiener filter" << endl;
}
void calcPSF(
Mat& outputImg,
Size filterSize,
int len,
double theta)
{
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);
}
void edgetaper(
const Mat& inputImg,
Mat& outputImg,
double gamma,
double beta)
{
float* p1 = w1.ptr<float>(0);
float* p2 = w2.ptr<float>(0);
float dx = float(2.0 *
CV_PI / Nx);
for (int i = 0; i < Nx; i++)
{
p1[i] = float(0.5 * (
tanh((x + gamma / 2) / beta) -
tanh((x - gamma / 2) / beta)));
x += dx;
}
float dy = float(2.0 *
CV_PI / Ny);
for (int i = 0; i < Ny; i++)
{
p2[i] = float(0.5 * (
tanh((y + gamma / 2) / beta) -
tanh((y - gamma / 2) / beta)));
y += dy;
}
}
如果数组没有元素,则返回 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 multiply(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1)
计算两个数组的逐元素缩放积。
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
Quat< T > tanh(const Quat< T > &q)
int cvRound(double value)
将浮点数舍入为最接近的整数。
Definition fast_math.hpp:200
#define CV_PI
定义 cvdef.h:380
void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制简单或粗椭圆弧或填充椭圆扇区。
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
\(SNR\)、\(LEN\) 和 \(THETA\) 的值是手动选择的,以获得最佳的视觉效果。\(THETA\) 参数与汽车的移动方向一致,而 \(LEN\) 参数取决于汽车的移动速度。结果并非完美,但至少它为我们提供了图像内容的线索。经过一些努力,现在车牌可以识别了。