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

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

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

一、函數(shù)參考

1、Primal-dual算法

Primal-dual algorithm是一種用于解決特殊類型的變分問(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)單的原理:將像素的顏色替換為相似像素顏色的平均值。 但是與給定像素最相似的像素根本沒有理由靠近。 因此,掃描圖像的大部分以尋找真正類似于想要去噪的像素的所有像素是合法的。執(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)文章

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

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

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

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

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

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

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

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

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

    C++while和do-while語(yǔ)句求和詳解

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

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

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

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

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

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

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

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

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

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

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

最新評(píng)論