如何使用中值滤波算法去除胡椒噪声
How to remove pepper noise using median filter algorithm
我已经编写了一个代码(用 C++)来使用 CImg 库在图像中添加噪声。现在我想用噪声加载图像并使用中值滤波算法去除图像内部的那些噪声。
下面是我的代码。
int main()
{
int x;
cout<<"Welcome to my app\n";
cout<<"Choose options below\n";
cout<<"1. Remove pepper 2. Add pepper\n";
cin>>x;
if (x==1)
{
cout<<"Needs help";
/*
* i tried to change the noise level to 0 but it did not work like below
* image.noise(0,2);
*
*/
}
else if(x==2)
{
//image file
CImg<unsigned char> image("new.bmp");
const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 };
image.noise(100,2);
image.save("new2.bmp");
CImgDisplay main_disp(image, "Image with Pepper noise");
while (!main_disp.is_closed())
{
main_disp.wait();
}
}
getchar();
return 0;
}
如果有其他方法可以使用 CImg 库执行此操作,我将不胜感激!!
根据this tutorial and the definition of the median函数你会得到这样的东西:
#include <algorithm>
using namespace std;
// ...
int ksize = 3; // 5, 7, N and so on... for NxN kernel
int ksize2 = ksize/2;
vector<uchar> kernel(ksize*ksize, 0);
for (int i=ksize2;i<image.dimx() - ksize2;i++)
for (int j=ksize2;j<image.dimy() - ksize2;j++)
for (int k=0;k<3;k++) {
// prepare kernel
int n = 0;
for(int l = -ksize2; l <= ksize2; l++)
for(int m = -ksize2; m <= ksize2; m++)
kernel[n++] = image(i + l,j + m,0,k);
// using std::algorithm to find median
sort(kernel.begin(), kernel.end());
// simple assign median value to created empty image
medianFilteredImage(i, j, 0, k) = kernel[kernel.size()/2]; // median is here now
}
}
}
我已经编写了一个代码(用 C++)来使用 CImg 库在图像中添加噪声。现在我想用噪声加载图像并使用中值滤波算法去除图像内部的那些噪声。 下面是我的代码。
int main()
{
int x;
cout<<"Welcome to my app\n";
cout<<"Choose options below\n";
cout<<"1. Remove pepper 2. Add pepper\n";
cin>>x;
if (x==1)
{
cout<<"Needs help";
/*
* i tried to change the noise level to 0 but it did not work like below
* image.noise(0,2);
*
*/
}
else if(x==2)
{
//image file
CImg<unsigned char> image("new.bmp");
const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 };
image.noise(100,2);
image.save("new2.bmp");
CImgDisplay main_disp(image, "Image with Pepper noise");
while (!main_disp.is_closed())
{
main_disp.wait();
}
}
getchar();
return 0;
}
如果有其他方法可以使用 CImg 库执行此操作,我将不胜感激!!
根据this tutorial and the definition of the median函数你会得到这样的东西:
#include <algorithm>
using namespace std;
// ...
int ksize = 3; // 5, 7, N and so on... for NxN kernel
int ksize2 = ksize/2;
vector<uchar> kernel(ksize*ksize, 0);
for (int i=ksize2;i<image.dimx() - ksize2;i++)
for (int j=ksize2;j<image.dimy() - ksize2;j++)
for (int k=0;k<3;k++) {
// prepare kernel
int n = 0;
for(int l = -ksize2; l <= ksize2; l++)
for(int m = -ksize2; m <= ksize2; m++)
kernel[n++] = image(i + l,j + m,0,k);
// using std::algorithm to find median
sort(kernel.begin(), kernel.end());
// simple assign median value to created empty image
medianFilteredImage(i, j, 0, k) = kernel[kernel.size()/2]; // median is here now
}
}
}