其他
OpenCV实现移动图象反模糊
点击上方↑↑↑“OpenCV学堂”关注我
详解OpenVINO道路分割模型使用!
觉得不错,请点【在看】支持!
基本原理
前面发过一篇文章,讲的是如何在频域空间实现图像的离焦模糊恢复,感觉大家很感兴趣,就再来一篇,同样来自OpenCV的官方教程翻译,讲述如何通过OpenCV实现移动对象反模糊,实现特定对象的恢复,先看效果:
其基本原理跟离焦模糊恢复非常相似,唯一不同的是对PSF的选择,对于离焦模糊恢复,使用圆形的PSF,对于移动模糊,我们需要选择线性PSF
其参数由两个因子决定,分别是长度LEN与移动方向THETA,加上之前的SNR(信噪比)三个参数决定了移动反模糊的效果。
代码实现
基于OpenCV4实现代码演示如下:
using namespace cv;
using namespace std;
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);
int adjust_len = 10;
void adjust_filter(int, void*);
double THETA = 15;
int snr = 500;
Mat src = imread("D:/images/text_motion.jpg", IMREAD_GRAYSCALE);
int main(int argc, char *argv[])
{
if (src.empty())
{
printf("ERROR : Image cannot be loaded..!!");
return -1;
}
imshow("input", src);
namedWindow("result", WINDOW_AUTOSIZE);
createTrackbar("Adjust R:", "result", &adjust_len, 50, adjust_filter);
Mat imgOut;
Rect roi = Rect(0, 0, src.cols & -2, src.rows & -2);
Mat Hw, h;
calcPSF(h, roi.size(), adjust_len, THETA);
calcWnrFilter(h, Hw, 1.0 / double(snr));
//Hw calculation (stop)
src.convertTo(src, CV_32F);
edgetaper(src, src);
// filtering (start)
filter2DFreq(src(roi), imgOut, Hw);
// filtering (stop)
imgOut.convertTo(imgOut, CV_8U);
normalize(imgOut, imgOut, 0, 255, NORM_MINMAX);
imwrite("D:/motion_deblur_result.jpg", imgOut);
imshow("result", imgOut);
waitKey(0);
return 0;
}
void adjust_filter(int, void*) {
Mat imgOut;
Rect roi = Rect(0, 0, src.cols & -2, src.rows & -2);
Mat Hw, h;
calcPSF(h, roi.size(), adjust_len, THETA);
calcWnrFilter(h, Hw, 1.0 / double(snr));
//Hw calculation (stop)
src.convertTo(src, CV_32F);
edgetaper(src, src);
// filtering (start)
filter2DFreq(src(roi), imgOut, Hw);
// filtering (stop)
imgOut.convertTo(imgOut, CV_8U);
normalize(imgOut, imgOut, 0, 255, NORM_MINMAX);
imwrite("D:/motion_deblur_result.jpg", imgOut);
imshow("result", imgOut);
}
void calcPSF(Mat& outputImg, Size filterSize, int len, double theta)
{
Mat h(filterSize, CV_32F, Scalar(0));
Point point(filterSize.width / 2, filterSize.height / 2);
ellipse(h, point, Size(0, cvRound(float(len) / 2.0)), 90.0 - theta, 0, 360, Scalar(255), FILLED);
Scalar summa = sum(h);
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));
Mat tmp;
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
}
void filter2DFreq(const Mat& inputImg, Mat& outputImg, const Mat& H)
{
Mat planes[2] = { Mat_<float>(inputImg.clone()), Mat::zeros(inputImg.size(), CV_32F) };
Mat complexI;
merge(planes, 2, complexI);
dft(complexI, complexI, DFT_SCALE);
Mat planesH[2] = { Mat_<float>(H.clone()), Mat::zeros(H.size(), CV_32F) };
Mat complexH;
merge(planesH, 2, complexH);
Mat complexIH;
mulSpectrums(complexI, complexH, complexIH, 0);
idft(complexIH, complexIH);
split(complexIH, planes);
outputImg = planes[0];
}
void calcWnrFilter(const Mat& input_h_PSF, Mat& output_G, double nsr)
{
Mat h_PSF_shifted;
fftshift(input_h_PSF, h_PSF_shifted);
Mat planes[2] = { Mat_<float>(h_PSF_shifted.clone()), Mat::zeros(h_PSF_shifted.size(), CV_32F) };
Mat complexI;
merge(planes, 2, complexI);
dft(complexI, complexI);
split(complexI, planes);
Mat denom;
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)
{
int Nx = inputImg.cols;
int Ny = inputImg.rows;
Mat w1(1, Nx, CV_32F, Scalar(0));
Mat w2(Ny, 1, CV_32F, Scalar(0));
float* p1 = w1.ptr<float>(0);
float* p2 = w2.ptr<float>(0);
float dx = float(2.0 * CV_PI / Nx);
float x = float(-CV_PI);
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);
float y = float(-CV_PI);
for (int i = 0; i < Ny; i++)
{
p2[i] = float(0.5 * (tanh((y + gamma / 2) / beta) - tanh((y - gamma / 2) / beta)));
y += dy;
}
Mat w = w2 * w1;
multiply(inputImg, w, outputImg);
}
缺点
调节那三个参数是技术活,必须有耐心,必须搞个进度条慢慢拉,这个算法最大的问题是无法对参数做到自适应,是不是应该考虑其它的方法拉!
参数不对,效果简直是惨不忍睹~~~~
往期精选
竹密不妨流水过,山高不碍白云飞
扫码加入OpenCV研习社
系统化学习OpenCV4,解锁更多技能!