OpenCV實(shí)現(xiàn)圖像去噪算法的步驟詳解
一、函數(shù)參考
1、Primal-dual算法
Primal-dual algorithm是一種用于解決特殊類型的變分問題的算法(即找到一個(gè)函數(shù)來最小化一些泛函)。

特別是由于圖像去噪可以看作是變分問題,因此可以使用原始對(duì)偶算法進(jìn)行去噪,這正是該算法所實(shí)現(xiàn)的。
cv::denoise_TVL1 (const std::vector< Mat > &observations, Mat &result, double lambda=1.0, int niters=30)
| observations | 該數(shù)組應(yīng)包含要恢復(fù)的圖像的一個(gè)或多個(gè)噪聲版本。 |
| result | 這里將存儲(chǔ)去噪圖像。 無需預(yù)先分配存儲(chǔ)空間,必要時(shí)會(huì)自動(dòng)分配。 |
| lambda | 對(duì)應(yīng)于上述公式中的 λ。 當(dāng)它被放大時(shí),平滑(模糊)的圖像比細(xì)節(jié)(但可能有更多噪點(diǎn))的圖像更受歡迎。 粗略地說,隨著它變小,結(jié)果會(huì)更加模糊,但會(huì)去除更多的異常值。 |
| niters | 算法將運(yùn)行的迭代次數(shù)。 當(dāng)然,越多的迭代越好,但是這個(gè)說法很難量化細(xì)化,所以就使用默認(rèn)值,如果結(jié)果不好就增加它。 |
2、非局部均值去噪算法
使用非局部均值去噪算法,該方法基于一個(gè)簡(jiǎn)單的原理:將像素的顏色替換為相似像素顏色的平均值。 但是與給定像素最相似的像素根本沒有理由靠近。 因此,掃描圖像的大部分以尋找真正類似于想要去噪的像素的所有像素是合法的。執(zhí)行圖像去噪,并進(jìn)行了多種計(jì)算優(yōu)化。 噪聲預(yù)期為高斯白噪聲。
cv::cuda::fastNlMeansDenoising (InputArray src, OutputArray dst, float h, int search_window=21, int block_size=7, Stream &stream=Stream::Null()) cv::fastNlMeansDenoising (InputArray src, OutputArray dst, float h=3, int templateWindowSize=7, int searchWindowSize=21) cv::fastNlMeansDenoising (InputArray src, OutputArray dst, const std::vector< float > &h, int templateWindowSize=7, int searchWindowSize=21, int
針對(duì)彩色圖像的 fastNlMeansDenoising 函數(shù)。
cv::cuda::fastNlMeansDenoisingColored (InputArray src, OutputArray dst, float h_luminance, float photo_render, int search_window=21, int block_size=7, Stream &stream=Stream::Null()) cv::fastNlMeansDenoisingColored (InputArray src, OutputArray dst, float h=3, float hColor=3, int templateWindowSize=7, int searchWindowSize=21)
針對(duì)圖像序列的 fastNlMeansDenoising 函數(shù)。
cv::fastNlMeansDenoisingColoredMulti (InputArrayOfArrays srcImgs, OutputArray dst, int imgToDenoiseIndex, int temporalWindowSize, float h=3, float hColor=3, int templateWindowSize=7, int searchWindowSize=21) cv::fastNlMeansDenoisingMulti (InputArrayOfArrays srcImgs, OutputArray dst, int imgToDenoiseIndex, int temporalWindowSize, float h=3, int templateWindowSize=7, int searchWindowSize=21) cv::fastNlMeansDenoisingMulti (InputArrayOfArrays srcImgs, OutputArray dst, int imgToDenoiseIndex, int temporalWindowSize, const std::vector< float > &h, int templateWindowSize=7, int searchWindowSize=21, int normType=NORM_L2)
執(zhí)行純非局部方法去噪,沒有任何簡(jiǎn)化,因此速度不快。
cv::cuda::nonLocalMeans (InputArray src, OutputArray dst, float h, int search_window=21, int block_size=7, int borderMode=BORDER_DEFAULT, Stream &stream=Stream::Null())
三、OpenCV源碼
1、源碼路徑
opencv\modules\photo\src\denoise_tvl1.cpp
2、源碼代碼
#include "precomp.hpp"
#include <vector>
#include <algorithm>
#define ABSCLIP(val,threshold) MIN(MAX((val),-(threshold)),(threshold))
namespace cv{
class AddFloatToCharScaled{
public:
AddFloatToCharScaled(double scale):_scale(scale){}
inline double operator()(double a,uchar b){
return a+_scale*((double)b);
}
private:
double _scale;
};
using std::transform;
void denoise_TVL1(const std::vector<Mat>& observations,Mat& result, double lambda, int niters){
CV_Assert(observations.size()>0 && niters>0 && lambda>0);
const double L2 = 8.0, tau = 0.02, sigma = 1./(L2*tau), theta = 1.0;
double clambda = (double)lambda;
double s=0;
const int workdepth = CV_64F;
int i, x, y, rows=observations[0].rows, cols=observations[0].cols,count;
for(i=1;i<(int)observations.size();i++){
CV_Assert(observations[i].rows==rows && observations[i].cols==cols);
}
Mat X, P = Mat::zeros(rows, cols, CV_MAKETYPE(workdepth, 2));
observations[0].convertTo(X, workdepth, 1./255);
std::vector< Mat_<double> > Rs(observations.size());
for(count=0;count<(int)Rs.size();count++){
Rs[count]=Mat::zeros(rows,cols,workdepth);
}
for( i = 0; i < niters; i++ )
{
double currsigma = i == 0 ? 1 + sigma : sigma;
// P_ = P + sigma*nabla(X)
// P(x,y) = P_(x,y)/max(||P(x,y)||,1)
for( y = 0; y < rows; y++ )
{
const double* x_curr = X.ptr<double>(y);
const double* x_next = X.ptr<double>(std::min(y+1, rows-1));
Point2d* p_curr = P.ptr<Point2d>(y);
double dx, dy, m;
for( x = 0; x < cols-1; x++ )
{
dx = (x_curr[x+1] - x_curr[x])*currsigma + p_curr[x].x;
dy = (x_next[x] - x_curr[x])*currsigma + p_curr[x].y;
m = 1.0/std::max(std::sqrt(dx*dx + dy*dy), 1.0);
p_curr[x].x = dx*m;
p_curr[x].y = dy*m;
}
dy = (x_next[x] - x_curr[x])*currsigma + p_curr[x].y;
m = 1.0/std::max(std::abs(dy), 1.0);
p_curr[x].x = 0.0;
p_curr[x].y = dy*m;
}
//Rs = clip(Rs + sigma*(X-imgs), -clambda, clambda)
for(count=0;count<(int)Rs.size();count++){
transform<MatIterator_<double>,MatConstIterator_<uchar>,MatIterator_<double>,AddFloatToCharScaled>(
Rs[count].begin(),Rs[count].end(),observations[count].begin<uchar>(),
Rs[count].begin(),AddFloatToCharScaled(-sigma/255.0));
Rs[count]+=sigma*X;
min(Rs[count],clambda,Rs[count]);
max(Rs[count],-clambda,Rs[count]);
}
for( y = 0; y < rows; y++ )
{
double* x_curr = X.ptr<double>(y);
const Point2d* p_curr = P.ptr<Point2d>(y);
const Point2d* p_prev = P.ptr<Point2d>(std::max(y - 1, 0));
// X1 = X + tau*(-nablaT(P))
x = 0;
s=0.0;
for(count=0;count<(int)Rs.size();count++){
s=s+Rs[count](y,x);
}
double x_new = x_curr[x] + tau*(p_curr[x].y - p_prev[x].y)-tau*s;
// X = X2 + theta*(X2 - X)
x_curr[x] = x_new + theta*(x_new - x_curr[x]);
for(x = 1; x < cols; x++ )
{
s=0.0;
for(count=0;count<(int)Rs.size();count++){
s+=Rs[count](y,x);
}
// X1 = X + tau*(-nablaT(P))
x_new = x_curr[x] + tau*(p_curr[x].x - p_curr[x-1].x + p_curr[x].y - p_prev[x].y)-tau*s;
// X = X2 + theta*(X2 - X)
x_curr[x] = x_new + theta*(x_new - x_curr[x]);
}
}
}
result.create(X.rows,X.cols,CV_8U);
X.convertTo(result, CV_8U, 255);
}
}四、效果圖像示例

原圖

denoise_TVL1

fastNlMeansDenoising
到此這篇關(guān)于OpenCV實(shí)現(xiàn)圖像去噪算法的步驟詳解的文章就介紹到這了,更多相關(guān)OpenCV圖像去噪算法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在vscode中快速新建html文件的2種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于在vscode中快速新建html文件的2種方法,以及如何快速打開HTML文件查看編輯效果的方法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
詳解利用C語言如何實(shí)現(xiàn)簡(jiǎn)單的內(nèi)存池
這篇文章主要給大家介紹了關(guān)于C語言如何實(shí)現(xiàn)簡(jiǎn)單的內(nèi)存池的相關(guān)資料,設(shè)計(jì)內(nèi)存池的目標(biāo)是為了保證服務(wù)器長(zhǎng)時(shí)間高效的運(yùn)行,通過對(duì)申請(qǐng)空間小而申請(qǐng)頻繁的對(duì)象進(jìn)行有效管理,減少內(nèi)存碎片的產(chǎn)生,合理分配管理用戶內(nèi)存,需要的朋友可以參考下2021-08-08
C++數(shù)據(jù)結(jié)構(gòu)紅黑樹全面分析
今天的這一篇博客,我要跟大家介紹二叉搜索樹中的另一顆樹——紅黑樹,它主要是通過控制顏色來控制自身的平衡,但它的平衡沒有AVL樹的平衡那么嚴(yán)格2022-02-02
OpenCV中findContours函數(shù)參數(shù)詳解
Opencv中通過使用findContours函數(shù),簡(jiǎn)單幾個(gè)的步驟就可以檢測(cè)出物體的輪廓,很方便。本文將和大家一起探討一下findContours方法中各參數(shù)的含義及用法,感興趣的可以了解一下2022-08-08

