KUAN滤波和Lee滤波的C++结合opencv实现

KUAN滤波和Lee滤波的opencv实现。

#include "stdafx.h"  
#include "cv.h"
#include "highgui.h"
#include   
#include   
#include   
#include   
#include "opencv2/imgproc/imgproc.hpp"    
#include   
using namespace cv;
using namespace std;
double **shuju(Mat src)//将图像转化为二维数组
{
	int row = src.rows;
	int col = src.cols;
	double **a;
	a = new double*[row];
	for (int i = 0;i < row;i++)
	{
		a[i] = new double[col];
	}

	for (int i = 0;i < src.rows;i++)
		for (int j = 0;j < src.cols;j++)
		{
			a[i][j] = (double)src.at < uchar >(i, j);
		}
	return a;
}
void KUAN(Mat &src, Mat &dst)
{
	double **i1, **i2;
	i1=shuju(src);
	double ybar;
	double ystad;
	double ENL;
	double sx2;
	double xcap;
	for (int i = 1; i < src.rows - 1; ++i)
		for (int j = 1; j < src.cols - 1; ++j)
		{
			//ybar = (i1[i][j] + i1(i - 1, j - 1) + i1(i - 1, j) + i1(i, j - 1)
				//+ i1(i + 1, j) + i1(i, j + 1) + i1(i + 1, j + 1) + i1(i - 1, j + 1) + i1(i + 1, j - 1)) / 9;
			ybar = (i1[i - 1][j - 1] + i1[i - 1][j] + i1[i - 1][j + 1] + i1[i][j - 1] + i1[i][j] + i1[i][j + 1] + i1[i + 1][j - 1] + i1[i + 1][j] + i1[i + 1][j + 1])/9;
			/*ystad = sqrt(((src.at(i, j) - ybar)*(src.at(i, j) - ybar)
				+ (src.at(i - 1, j - 1) - ybar)*(src.at(i - 1, j - 1) - ybar)
				+ (src.at(i - 1, j) - ybar)*(src.at(i - 1, j) - ybar)

				+ (src.at(i, j - 1) - ybar)*(src.at(i, j - 1) - ybar)
				+ (src.at(i + 1, j) - ybar)*(src.at(i + 1, j) - ybar)
				+ (src.at(i, j + 1) - ybar)*(src.at(i, j + 1) - ybar)

				+ (src.at(i + 1, j + 1) - ybar)*(src.at(i + 1, j + 1) - ybar)
				+ (src.at(i - 1, j + 1) - ybar)*(src.at(i - 1, j + 1) - ybar)
				+ (src.at(i + 1, j - 1) - ybar)*(src.at(i + 1, j - 1) - ybar)) / 9);*/
			ystad = sqrt(((i1[i][j]-ybar)*(i1[i][j] - ybar)+ (i1[i-1][j-1] - ybar)*(i1[i-1][j-1] - ybar)
				+(i1[i-1][j] - ybar)*(i1[i-1][j] - ybar) + (i1[i][j+1] - ybar)*(i1[i][j+1] - ybar)
				+ (i1[i][j-1] - ybar)*(i1[i][j-1] - ybar) + (i1[i][j+1] - ybar)*(i1[i][j+1] - ybar)
				+ (i1[i+1][j-1] - ybar)*(i1[i+1][j-1] - ybar) + (i1[i+1][j] - ybar)*(i1[i+1][j] - ybar)
				+ (i1[i + 1][j + 1] - ybar)*(i1[i + 1][j + 1] - ybar)) / 9);

			ENL = (ybar / (ystad + 0.1))*(ybar / (ystad + 0.1));
			sx2 = (ENL*ystad*ystad - ybar*ybar) / (ENL + 1);
			xcap = ybar + (sx2*(i1[i][j] - ybar)) / (sx2 + ((ybar*ybar + sx2) / ENL) + 0.1);
			if (xcap>255)
			{
				xcap = 255;
			}
			else if (xcap<0)
			{
				xcap = 0;
			}
			else
			{
				xcap = (int)(xcap + 0.5);
			}
			dst.at(i, j) = xcap;

		}

}
void Lee(Mat &src, Mat &dst)
{
	double **i1, **i2;
	i1 = shuju(src);
	double ybar;
	double ystad;
	double ENL;
	double sx2;
	double xcap;
	for (int i = 1; i < src.rows - 1; ++i)
		for (int j = 1; j < src.cols - 1; ++j)
		{
			ybar = (i1[i - 1][j - 1] + i1[i - 1][j] + i1[i - 1][j + 1] + i1[i][j - 1] + i1[i][j] + i1[i][j + 1] + i1[i + 1][j - 1] + i1[i + 1][j] + i1[i + 1][j + 1]) / 9;
			ystad = sqrt(((i1[i][j] - ybar)*(i1[i][j] - ybar) + (i1[i - 1][j - 1] - ybar)*(i1[i - 1][j - 1] - ybar)
				+ (i1[i - 1][j] - ybar)*(i1[i - 1][j] - ybar) + (i1[i][j + 1] - ybar)*(i1[i][j + 1] - ybar)
				+ (i1[i][j - 1] - ybar)*(i1[i][j - 1] - ybar) + (i1[i][j + 1] - ybar)*(i1[i][j + 1] - ybar)
				+ (i1[i + 1][j - 1] - ybar)*(i1[i + 1][j - 1] - ybar) + (i1[i + 1][j] - ybar)*(i1[i + 1][j] - ybar)
				+ (i1[i + 1][j + 1] - ybar)*(i1[i + 1][j + 1] - ybar)) / 9);

			ENL = (ybar / (ystad + 0.1))*(ybar / (ystad + 0.1));
			sx2 = (ENL*ystad*ystad - ybar*ybar) / (ENL + 1);
			xcap = ybar + (sx2*(i1[i][j] - ybar)) / (sx2 + (ybar*ybar  / ENL) + 0.1);
			if (xcap>255)
			{
				xcap = 255;
			}
			else if (xcap<0)
			{
				xcap = 0;
			}
			else
			{
				xcap = (int)(xcap + 0.5);
			}
			dst.at(i, j) = xcap;

		}

}

int main()
{
	 Mat image = imread("C:\\Users\\Lisai\\Desktop\\3.png",0);
	 Mat img = image;
	 double **img1;
	/* for (int i = 0;i < img.rows;i++)
		 for (int j = 0;j < img.cols;j++)
		 {
			 img.at < float >(i,j)= 0;
		 }*/
	 Lee(image, img);
	 imshow("hah",img);
	 waitKey();
    return 0;
}
结合下面matlab代码写的

%Lee filter for speckle noise reduction 
%Authors : Jeny Rajan, Chandrashekar P.S 
%Usage - lee(I) 
%I is the noisy image (gray level image m x n x 1) 
 
function [le]=lee(I) 
[x y z]=size(I) 
I=double(I); 
N=zeros(x,y,z); 
for i=1:x 
    i 
    for j=1:y 
        % Checking first and last pixel of first row%  
        if (i==1 & j==1) 
            mat(1)=0; 
            mat(2)=0; 
            mat(3)=0; 
            mat(4)=0; 
            mat(5)=I(i,j); 
            mat(6)=I(i,j+1); 
            mat(7)=0; 
            mat(8)=I(i+1,j); 
            mat(9)=I(i+1,j+1); 
        end 
         
        if (i==1 & j==y) 
            mat(1)=0; 
            mat(2)=0; 
            mat(3)=0; 
            mat(4)=I(i,j-1); 
            mat(5)=I(i,j); 
            mat(6)=0; 
            mat(7)=I(i+1,j-1); 
            mat(8)=I(i+1,j); 
            mat(9)=0; 
        end 
         
        % Checking first and last pixel of last row%  
        if (i==x & j==1) 
            mat(1)=0; 
            mat(2)=I(i-1,j); 
            mat(3)=I(i-1,j+1); 
            mat(4)=0; 
            mat(5)=I(i,j); 
            mat(6)=I(i,j+1); 
            mat(7)=0; 
            mat(8)=0; 
            mat(9)=0;  
        end 
         
        if (i==x & j==y) 
            mat(1)=I(i-1,j-1); 
            mat(2)=I(i-1,j); 
            mat(3)=0; 
            mat(4)=I(i,j-1); 
            mat(5)=I(i,j); 
            mat(6)=0; 
            mat(7)=0; 
            mat(8)=0; 
            mat(9)=0; 
        end 
        % Checking rest of the image%         
        if (i>1 & i1 & j1 & i1 & j



你可能感兴趣的:(图像处理)