您可以在 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\) 引數取決於汽車的移動速度。結果並非完美,但至少它為我們提供了影像內容的線索。經過一些努力,現在車牌可以識別了。