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

特別是由于圖像去噪可以看作是變分問(wèn)題,因此可以使用原始對(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ǔ)去噪圖像。 無(wú)需預(yù)先分配存儲(chǔ)空間,必要時(shí)會(huì)自動(dòng)分配。 |
| lambda | 對(duì)應(yīng)于上述公式中的 λ。 當(dāng)它被放大時(shí),平滑(模糊)的圖像比細(xì)節(jié)(但可能有更多噪點(diǎn))的圖像更受歡迎。 粗略地說(shuō),隨著它變小,結(jié)果會(huì)更加模糊,但會(huì)去除更多的異常值。 |
| niters | 算法將運(yùn)行的迭代次數(shù)。 當(dāng)然,越多的迭代越好,但是這個(gè)說(shuō)法很難量化細(xì)化,所以就使用默認(rèn)值,如果結(jié)果不好就增加它。 |
2、非局部均值去噪算法
使用非局部均值去噪算法,該方法基于一個(gè)簡(jiǎn)單的原理:將像素的顏色替換為相似像素顏色的平均值。 但是與給定像素最相似的像素根本沒(méi)有理由靠近。 因此,掃描圖像的大部分以尋找真正類(lèi)似于想要去噪的像素的所有像素是合法的。執(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í)行純非局部方法去噪,沒(méi)有任何簡(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)文章
基于C++實(shí)現(xiàn)kinect+opencv 獲取深度及彩色數(shù)據(jù)
本文的主要思想是Kinect SDK 讀取彩色、深度、骨骼信息并用OpenCV顯示,非常的實(shí)用,有需要的小伙伴可以參考下2015-12-12
C++遍歷磁盤(pán)驅(qū)動(dòng)器的示例代碼
這篇文章主要介紹了C++遍歷磁盤(pán)驅(qū)動(dòng)器的示例代碼,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下2021-01-01
c++矩陣計(jì)算性能對(duì)比:Eigen和GPU解讀
這篇文章主要介紹了c++矩陣計(jì)算性能對(duì)比:Eigen和GPU解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
C語(yǔ)言中隱藏結(jié)構(gòu)體的細(xì)節(jié)
以筆者粗淺的認(rèn)識(shí),有兩種最常用的方法,可以實(shí)現(xiàn)庫(kù)內(nèi)結(jié)構(gòu)體定義的隱藏:接口函數(shù)形參使用結(jié)構(gòu)體指針,接口函數(shù)形參使用句柄。2017-05-05
C++?OpenCV實(shí)現(xiàn)白平衡之完美反射算法
完美反射算法是白平衡各種算法中較常見(jiàn)的一種,比灰度世界算法更優(yōu)。本文將利用C++和OpenCV實(shí)現(xiàn)白平衡中的完美反射算法,需要的可以參考一下2022-05-05
C++中產(chǎn)生臨時(shí)對(duì)象的情況及其解決方案
這篇文章主要介紹了C++中產(chǎn)生臨時(shí)對(duì)象的情況及其解決方案,以值傳遞的方式給函數(shù)傳參,類(lèi)型轉(zhuǎn)換以及函數(shù)需要返回對(duì)象時(shí),并給對(duì)應(yīng)給出了詳細(xì)的解決方案,通過(guò)圖文結(jié)合的方式講解的非常詳細(xì),需要的朋友可以參考下2024-05-05
C++調(diào)用C接口的實(shí)現(xiàn)示例
這篇文章主要介紹了C++調(diào)用C接口的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

