欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV實現(xiàn)圖像去噪算法的步驟詳解

 更新時間:2022年06月21日 14:23:12   作者:坐望云起  
這篇文章主要為大家介紹了OpenCV中圖像去噪算法的原理,文中通過示例為大家詳細(xì)講解了圖像去噪算法的使用,感興趣的小伙伴可以了解一下

一、函數(shù)參考

1、Primal-dual算法

Primal-dual algorithm是一種用于解決特殊類型的變分問題的算法(即找到一個函數(shù)來最小化一些泛函)。

特別是由于圖像去噪可以看作是變分問題,因此可以使用原始對偶算法進(jìn)行去噪,這正是該算法所實現(xiàn)的。

cv::denoise_TVL1 (const std::vector< Mat > &observations, Mat &result, double lambda=1.0, int niters=30)
observations該數(shù)組應(yīng)包含要恢復(fù)的圖像的一個或多個噪聲版本。
result這里將存儲去噪圖像。 無需預(yù)先分配存儲空間,必要時會自動分配。
lambda對應(yīng)于上述公式中的 λ。 當(dāng)它被放大時,平滑(模糊)的圖像比細(xì)節(jié)(但可能有更多噪點)的圖像更受歡迎。 粗略地說,隨著它變小,結(jié)果會更加模糊,但會去除更多的異常值。
niters算法將運行的迭代次數(shù)。 當(dāng)然,越多的迭代越好,但是這個說法很難量化細(xì)化,所以就使用默認(rèn)值,如果結(jié)果不好就增加它。

2、非局部均值去噪算法

使用非局部均值去噪算法,該方法基于一個簡單的原理:將像素的顏色替換為相似像素顏色的平均值。 但是與給定像素最相似的像素根本沒有理由靠近。 因此,掃描圖像的大部分以尋找真正類似于想要去噪的像素的所有像素是合法的。執(zhí)行圖像去噪,并進(jìn)行了多種計算優(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

針對彩色圖像的 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)

針對圖像序列的 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í)行純非局部方法去噪,沒有任何簡化,因此速度不快。

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實現(xiàn)圖像去噪算法的步驟詳解的文章就介紹到這了,更多相關(guān)OpenCV圖像去噪算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言淺析函數(shù)的用法

    C語言淺析函數(shù)的用法

    C語言函數(shù)是用來模塊化構(gòu)建程序的。如果你的功能少,你可以全都寫在mian函數(shù)中,但是當(dāng)實現(xiàn)功能多的時候,如果全寫在main的函數(shù)里,不僅代碼不美觀,而且函數(shù)實現(xiàn)的時候結(jié)構(gòu)復(fù)雜,代碼重復(fù)
    2022-07-07
  • 在vscode中快速新建html文件的2種方法總結(jié)

    在vscode中快速新建html文件的2種方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于在vscode中快速新建html文件的2種方法,以及如何快速打開HTML文件查看編輯效果的方法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • C++中關(guān)于union的使用方法說明

    C++中關(guān)于union的使用方法說明

    這篇文章主要介紹了C++中關(guān)于union的使用方法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 詳解利用C語言如何實現(xiàn)簡單的內(nèi)存池

    詳解利用C語言如何實現(xiàn)簡單的內(nèi)存池

    這篇文章主要給大家介紹了關(guān)于C語言如何實現(xiàn)簡單的內(nèi)存池的相關(guān)資料,設(shè)計內(nèi)存池的目標(biāo)是為了保證服務(wù)器長時間高效的運行,通過對申請空間小而申請頻繁的對象進(jìn)行有效管理,減少內(nèi)存碎片的產(chǎn)生,合理分配管理用戶內(nèi)存,需要的朋友可以參考下
    2021-08-08
  • C++while和do-while語句求和詳解

    C++while和do-while語句求和詳解

    對于C語言中的while與do-while,相信很多都再熟悉不過了,最近在工作中就用到了,所以想著總結(jié)一下,方便自己或者有需要的朋友們參考借鑒,文中通過示例代碼介紹的很詳細(xì),感興趣的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • C++實現(xiàn)二叉樹非遞歸遍歷算法詳解

    C++實現(xiàn)二叉樹非遞歸遍歷算法詳解

    在C++中,二叉樹非遞歸遍歷是一種常用的算法,可避免遞歸過程中的系統(tǒng)開銷和棧溢出問題。非遞歸遍歷算法利用棧數(shù)據(jù)結(jié)構(gòu)實現(xiàn),可以實現(xiàn)前序、中序和后序遍歷,是C++程序員必備技能之一
    2023-04-04
  • C++數(shù)據(jù)結(jié)構(gòu)紅黑樹全面分析

    C++數(shù)據(jù)結(jié)構(gòu)紅黑樹全面分析

    今天的這一篇博客,我要跟大家介紹二叉搜索樹中的另一顆樹——紅黑樹,它主要是通過控制顏色來控制自身的平衡,但它的平衡沒有AVL樹的平衡那么嚴(yán)格
    2022-02-02
  • OpenCV中findContours函數(shù)參數(shù)詳解

    OpenCV中findContours函數(shù)參數(shù)詳解

    Opencv中通過使用findContours函數(shù),簡單幾個的步驟就可以檢測出物體的輪廓,很方便。本文將和大家一起探討一下findContours方法中各參數(shù)的含義及用法,感興趣的可以了解一下
    2022-08-08
  • C++單鏈表實現(xiàn)大數(shù)加法

    C++單鏈表實現(xiàn)大數(shù)加法

    這篇文章主要為大家詳細(xì)介紹了C++單鏈表實現(xiàn)大數(shù)加法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • C++淺析類與對象的基礎(chǔ)

    C++淺析類與對象的基礎(chǔ)

    類和對象是兩種以計算機為載體的計算機語言的合稱。對象是對客觀事物的抽象,類是對對象的抽象。類是一種抽象的數(shù)據(jù)類型;變量就是可以變化的量,存儲在內(nèi)存中—個可以擁有在某個范圍內(nèi)的可變存儲區(qū)域
    2022-05-05

最新評論