云计算百科
云计算领域专业知识百科平台

乞丐哥的私房菜(Ubuntu OpenCV篇——Image Processing 节 之 Motion Deblur Filter 运动去模糊滤波器 滤镜)

  • 操作系统:手机安装 Termux ,Termux 内安装 ubuntu
    • Termux 0.119.0-beta.3
    • 内嵌 ubuntu 24.04
  • 编译器:GNU g++ 14.2.0
  • 编辑器:Emacs 29.3
  • OpenCV:4.13.0

目标

  • 什么是运动模糊图像的 PSF
  • 如何恢复运动模糊图像

理论

对于退化图像模型理论和维纳滤光片理论,您可以参考教程失焦去模糊滤镜。在此页面上,仅考虑线性运动模糊失真。此页面上的运动模糊图像是真实世界的图像。模糊是由移动的物体引起的。

运动模糊图像的 PSF 是多少

线性运动模糊畸变的点扩散函数 (PSF) 是线段。这样的 PSF 由两个参数指定:LENLENLEN 是模糊的长度,而 THETATHETATHETA 是运动角度
在这里插入图片描述

如何恢复模糊的图像

在此页面上,维纳滤镜用作恢复滤镜,有关详细信息,您可以参考教程失焦去模糊滤镜。为了合成运动模糊情况的维纳滤波器,它需要指定 PSF 的信噪比 (SNR)(SNR)(SNR)LENLENLENTHETATHETATHETA

源代码

#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"

using namespace cv;
using namespace std;

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);

const String keys =
"{help h usage ? | | print this message }"
"{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();
CommandLineParser parser(argc, argv, keys);
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;
}

Mat imgIn;
imgIn = imread(strInFileName, IMREAD_GRAYSCALE);
if (imgIn.empty()) //check whether the image is loaded or not
{
cout << "ERROR : Image cannot be loaded..!!" << endl;
return 1;
}

Mat imgOut;

// it needs to process even image only
Rect roi = Rect(0, 0, imgIn.cols & 2, imgIn.rows & 2);

//Hw calculation (start)
Mat Hw, h;
calcPSF(h, roi.size(), LEN, THETA);
calcWnrFilter(h, Hw, 1.0 / double(snr));
//Hw calculation (stop)

imgIn.convertTo(imgIn, CV_32F);
edgetaper(imgIn, imgIn);

// filtering (start)
filter2DFreq(imgIn(roi), imgOut, Hw);
// filtering (stop)

imgOut.convertTo(imgOut, CV_8U);
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)
{
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);
}

解释说明

  • 运动模糊图像恢复算法包括PSF生成、维纳滤波器生成和在频域中对模糊图像进行滤波: // it needs to process even image only
    Rect roi = Rect(0, 0, imgIn.cols & 2, imgIn.rows & 2);

    //Hw calculation (start)
    Mat Hw, h;
    calcPSF(h, roi.size(), LEN, THETA);
    calcWnrFilter(h, Hw, 1.0 / double(snr));
    //Hw calculation (stop)

    imgIn.convertTo(imgIn, CV_32F);
    edgetaper(imgIn, imgIn);

    // filtering (start)
    filter2DFreq(imgIn(roi), imgOut, Hw);
    // filtering (stop)

  • 函数calcPSF()根据输入参数 LENLENLENTHETATHETATHETA(以度为单位)形成PSF: 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];
    }

  • 函数edgetaper()使输入图像的边缘逐渐变细,以减少恢复图像中的振铃效应: 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);
    }

函数calcWnrFilter()、fftshift()和filter2DFreq()实现了频域中指定PSF的图像过滤。这些功能是从教程失焦去模糊滤镜中复制的

结果

  • 重要: 注意程序里面参数的调整,LEN, THETA, SNR
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
赞(0)
未经允许不得转载:网硕互联帮助中心 » 乞丐哥的私房菜(Ubuntu OpenCV篇——Image Processing 节 之 Motion Deblur Filter 运动去模糊滤波器 滤镜)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!