图像退化/复原过程模型
高斯噪声
PDF(概率密度函数)
生成高斯随机数序列
算法可参考<http://www.doc.ic.ac.uk/~wl/papers/07/csur07dt.pdf>
代码实现
double gaussrand()
{
static double V1, V2, S;
static int phase = ;
double X; if(phase == ) {
do {
double U1 = (double)rand() / RAND_MAX;
double U2 = (double)rand() / RAND_MAX; V1 = * U1 - ;
V2 = * U2 - ;
S = V1 * V1 + V2 * V2;
} while(S >= || S == ); X = V1 * sqrt(- * log(S) / S);
} else
X = V2 * sqrt(- * log(S) / S); phase = - phase; return X * ;
}
生成高斯噪声图及直方图
下面给一幅图添加高斯噪声
代码实现
void add_gaussian_noise(short** in_array, short** out_array, long height, long width)
{
srand(time(NULL));
for (int i = ; i < height; i++){
for (int j = ; j < width; j++){
out_array[i][j] = in_array[i][j] + (short)gaussrand();
if (out_array[i][j] < 0x00)
out_array[i][j] = 0x00;
else if (out_array[i][j] > 0xff)
out_array[i][j] = 0xff;
}
}
}
原图
添加高斯噪声
椒盐噪声
添加椒盐噪声(胡椒噪声和盐粒噪声概率分别为5%)
void add_salt_pepper_noise(short** in_array, short** out_array, long height, long width)
{
srand(time(NULL));
int noise_p; for (int i = ; i < height; i++){
for (int j = ; j < width; j++){
noise_p = rand() % ;
if (noise_p == ){
int temp = rand() % ;
if (temp)
out_array[i][j] = 0x00;
else
out_array[i][j] = 0xff;
}
else
out_array[i][j] = in_array[i][j];
}
}
}
均值滤波器
算术均值滤波器
代码实现
int is_in_array(short x, short y, short height, short width)
{
if (x >= && x < width && y >= && y < height)
return ;
else
return ;
} /*
* element
* v0 v1 v2
* v3 v4 v5
* v6 v7 v8
*
*/
void filtering(short** in_array, short** out_array, long height, long width)
{
short value[]; for (int i = ; i < height; i++){
for (int j = ; j < width; j++){
value[] = is_in_array(j-, i-, height, width) ? in_array[i-][j-] : ;
value[] = is_in_array(j, i-, height, width) ? in_array[i-][j] : ;
value[] = is_in_array(j+, i-, height, width) ? in_array[i-][j+] : ;
value[] = is_in_array(j-, i, height, width) ? in_array[i][j-] : ;
value[] = in_array[i][j];
value[] = is_in_array(j+, i, height, width) ? in_array[i][j+] : ;
value[] = is_in_array(j-, i+, height, width) ? in_array[i+][j-] : ;
value[] = is_in_array(j, i+, height, width) ? in_array[i+][j] : ;
value[] = is_in_array(j+, i+, height, width) ? in_array[i+][j+] : ; /* Arithmetic Mean Filter */
for (int k = ; k < ARRAY_SIZE*ARRAY_SIZE; k++)
out_array[i][j] += value[k];
out_array[i][j] /= ARRAY_SIZE*ARRAY_SIZE; }
}
}
处理高斯噪声
处理椒盐噪声
结论:算术平均滤波对于高斯噪声和椒盐噪声都有一定的效果,但是同时会平滑图像
几何均值滤波器
实现
void filtering(short** in_array, short** out_array, long height, long width)
{
short value[]; for (int i = ; i < height; i++){
for (int j = ; j < width; j++){
value[] = is_in_array(j-, i-, height, width) ? in_array[i-][j-] : ;
value[] = is_in_array(j, i-, height, width) ? in_array[i-][j] : ;
value[] = is_in_array(j+, i-, height, width) ? in_array[i-][j+] : ;
value[] = is_in_array(j-, i, height, width) ? in_array[i][j-] : ;
value[] = in_array[i][j];
value[] = is_in_array(j+, i, height, width) ? in_array[i][j+] : ;
value[] = is_in_array(j-, i+, height, width) ? in_array[i+][j-] : ;
value[] = is_in_array(j, i+, height, width) ? in_array[i+][j] : ;
value[] = is_in_array(j+, i+, height, width) ? in_array[i+][j+] : ; /* Geometric Mean Filter */
double product = 1.0;
for (int k = ; k < ARRAY_SIZE*ARRAY_SIZE; k++)
product *= value[k];
product = pow(product, 1.0 / 9.0);
out_array[i][j] = (short)product; if (out_array[i][j] < 0x00)
out_array[i][j] = 0x00;
else if (out_array[i][j] > 0xff)
out_array[i][j] = 0xff;
}
}
}
几何均值滤波器与算术均值滤波器相比,丢失的图像细节更少
谐波均值滤波器
实现
void filtering(short** in_array, short** out_array, long height, long width)
{
short value[]; for (int i = ; i < height; i++){
for (int j = ; j < width; j++){
value[] = is_in_array(j-, i-, height, width) ? in_array[i-][j-] : ;
value[] = is_in_array(j, i-, height, width) ? in_array[i-][j] : ;
value[] = is_in_array(j+, i-, height, width) ? in_array[i-][j+] : ;
value[] = is_in_array(j-, i, height, width) ? in_array[i][j-] : ;
value[] = in_array[i][j];
value[] = is_in_array(j+, i, height, width) ? in_array[i][j+] : ;
value[] = is_in_array(j-, i+, height, width) ? in_array[i+][j-] : ;
value[] = is_in_array(j, i+, height, width) ? in_array[i+][j] : ;
value[] = is_in_array(j+, i+, height, width) ? in_array[i+][j+] : ; /* Harmonic Mean Filter */
double sum = ;
for (int k = ; k < ARRAY_SIZE*ARRAY_SIZE; k++)
sum += 1.0 / value[k];
out_array[i][j] = (short)(9.0 / sum); if (out_array[i][j] < 0x00)
out_array[i][j] = 0x00;
else if (out_array[i][j] > 0xff)
out_array[i][j] = 0xff;
}
}
}
处理高斯噪声
处理椒盐噪声
对盐粒噪声效果较好,不适用于胡椒噪声,善于处理高斯噪声
逆谐波均值滤波器
Q为滤波器的阶数,Q为正时,消除胡椒噪声,Q为负时消除盐粒噪声
Q=0为算术均值滤波器,Q=-1谐波均值滤波器
实现
void filtering(short** in_array, short** out_array, long height, long width)
{
short value[]; for (int i = ; i < height; i++){
for (int j = ; j < width; j++){
value[] = is_in_array(j-, i-, height, width) ? in_array[i-][j-] : ;
value[] = is_in_array(j, i-, height, width) ? in_array[i-][j] : ;
value[] = is_in_array(j+, i-, height, width) ? in_array[i-][j+] : ;
value[] = is_in_array(j-, i, height, width) ? in_array[i][j-] : ;
value[] = in_array[i][j];
value[] = is_in_array(j+, i, height, width) ? in_array[i][j+] : ;
value[] = is_in_array(j-, i+, height, width) ? in_array[i+][j-] : ;
value[] = is_in_array(j, i+, height, width) ? in_array[i+][j] : ;
value[] = is_in_array(j+, i+, height, width) ? in_array[i+][j+] : ; /* Contra-Harmonic Mean Filter */
int Q = ;
double num = 0.0, den = 0.0;
for (int k = ; k < ARRAY_SIZE*ARRAY_SIZE; k++){
num += pow(value[k], Q+);
den += pow(value[k], Q);
}
out_array[i][j] = (short)(num / den); if (out_array[i][j] < 0x00)
out_array[i][j] = 0x00;
else if (out_array[i][j] > 0xff)
out_array[i][j] = 0xff;
}
}
}
Q = 2 消除胡椒噪声
Q = -2消除盐粒噪声
Q = -2消除盐粒噪声后的图像使用Q = 2消除胡椒噪声
再来一次
再来
此时椒盐噪声已经基本消除