📄 spdenoise.c
字号:
/********************************************************************
*
* remove salt-and-peper noise tool
*
********************************************************************/#include <tina/all_tina.h>#include "bmp.h"#include "gnuplot_i.h"static Tv *tv = NULL;static Tv *tv1 = NULL;static Imrect *orig_im =NULL;static Imrect *denoise_im = NULL;static int image_choice = 0;static int denoise_method = 0;static int MCOUNT = 1;#define CUTPIX 254.56789static int iminarg1,iminarg2;
#define IMAX(a,b) (iminarg1 = (a),iminarg2 = (b),(iminarg1) > (iminarg2) ? (iminarg1) : (iminarg2))extern void nhwfm_proc(void);static image_choice_proc(int val){ image_choice = val;}static void denoise_choice_proc(int val){ denoise_method = val;}static void denoise_proc(void){ Imrect *srcIm = NULL,*destIm = NULL,*srcIm0 = NULL;
int width, height;
Imregion *roi; int i,j; int type;
//BYTE pix; float pix; float pixa,pixb; char filename[64]; char string[25]; int kn; char filenamedir[64]; char filename0[64]; char filename1[64]; char filenametxt[64]; char fpng[64]; char fn[32]; //float cutpix = 254.56789; float cutpix = 255; int adapt =1; int m_count =0; float gray_sum = 0.0; float pixm,pixm1,pixm2,pixm3; int t,h; int count_adapt[25]; const float f = 0.00001; float *neighbor,*togetmed; int n = 0; int r; int **flag; int w,ws = 0; int lp = 0; int pol; int hn[256]; float ho[256]; float hws = 0.0; Imrect *comp_orig = NULL; //Lenaorig = BmpToImrect("/home/wzz/newimscramdegree/result/receive/Lena.bmp"); //Lenaorig = ReadGIF("/home/wzz/newimscramdegree/result/receive/Lena.gif",1); //comp_orig = BmpToImrect("/home/wzz/newimscramdegree/result/g256.bmp"); //comp_orig = ReadGIF("/home/wzz/newimscramdegree/result/Lena.gif",1); //comp_orig = ReadGIF("/home/wzz/newimscramdegree/result/newscramdegree/Lena_512x512.gif",1); switch( image_choice ) { case 0: strcpy(filenamedir,"result/denoise/lena_cut_255/"); comp_orig = BmpToImrect("result/denoise/lena_cut_255/0.bmp"); strcpy(filename0,"result/denoise/lena_"); break; case 1: strcpy(filenamedir,"result/denoise/bridge_cut_255/"); comp_orig = BmpToImrect("result/denoise/bridge_cut_255/0.bmp"); strcpy(filename0,"result/denoise/bridge_"); break; default: break; } if (comp_orig == NULL) { printf("read image error."); return; } width = comp_orig->width;
height = comp_orig->height;
roi = comp_orig->region;
if (roi == NULL) return ; destIm = im_alloc( height, width, roi, float_v ); srcIm0 = im_alloc( height, width, roi, float_v ); gcvt(MCOUNT,5,string); switch( denoise_method )
{
case 0: strcat(fn, "mean"); strcat(fn, string);
break;
case 1:
strcat(fn, "median"); strcat(fn, string);
break;
case 2:
strcat(fn, "mode"); strcat(fn, string);
break;
case 3:
strcat(fn, "mm"); strcat(fn, string);
break;
case 4:
strcat(fn, "w-mean"); strcat(fn, string);
break;
case 5: strcat(fn, "hwm");
break;
case 6: strcat(fn, "nhwfm");
break; case 7: strcat(fn, "w-median"); strcat(fn, string);
break;
default:
break;
} strcat(filename0, fn); strcpy(filenametxt,filename0); strcat(filenametxt,".txt"); FILE *fp; fp = fopen(filenametxt,"wb"); printf("start denoise!\n");for(kn = 5; kn < 100; kn += 5){ strcpy(filename,filenamedir); gcvt(kn,5,string); strcat(filename, string); strcat(filename, ".bmp"); srcIm = BmpToImrect(filename); if (srcIm == NULL) { printf("read image error."); return; }
for(i = 0;i < height; i ++) for(j = 0;j < width; j++) { IM_PIX_GET(srcIm, i, j, pix); IM_PIX_SET(srcIm0, i, j, pix); } for(i = 0;i < 25; i ++) count_adapt[i] = 0; flag = int_matrix(height, width); for(i = 0;i < 256; i ++) hn[i] = 0; lp = 0; for(i = 0;i < height; i ++) for(j = 0;j < width; j ++) { pix = im_get_pixf(srcIm0,i,j); if(fabs(pix - cutpix) < f ) { flag[i][j] = 0; lp ++; } else { hn[(int)pix]++; flag[i][j] = 1; } } pol = (int)((float)lp/(float)(height*width)*100+0.5); printf("propability of lost pexel : %f \n",(float)pol/100); for(i = 0;i < 256; i ++) { ho[i] = (float)hn[i]/((1 - (float)pol/100)*height*width); }/*--------------------------------------------------------------------------*/ switch( denoise_method )
{
case 0:/*--------------------------------mean-------------------------------------*/ for(i = 0;i < height; i ++) for(j = 0;j < width; j ++) { pix = im_get_pixf(srcIm0,i,j); adapt = 1; //if(fabs(pix - cutpix) < f )//lost point if(flag[i][j]==0) { m_count = 0; gray_sum = 0.0; while(m_count < MCOUNT )//at least have MCOUNT points in the mask { m_count = 0; gray_sum = 0.0; for(t = i - adapt; t <= i + adapt; t ++) for(h = j - adapt; h <= j + adapt; h ++) { if((t<0)||(t>=height)||(h<0)||(h>=width)) continue; pixm = im_get_pixf(srcIm0,t,h); //if(fabs(pixm - cutpix) > f)//orig point if(flag[t][h]==1) { gray_sum += pixm; m_count ++; } } if(m_count < MCOUNT) adapt ++; } pixm = (float)(gray_sum/m_count);//mean count_adapt[adapt] ++; im_put_pixf(pixm,destIm,i,j); } else { im_put_pixf(pix,destIm,i,j); count_adapt[0] ++; } }/*---------------------------------------------------------------------*/
break;
case 1:/*------------------------------median---------------------------------------*/ for(i = 0;i < height; i ++) for(j = 0;j < width; j ++) { pix = im_get_pixf(srcIm0,i,j); adapt = 1; //if(fabs(pix - cutpix) < f )//lost point if(flag[i][j]==0) { m_count = 0; gray_sum = 0.0; while(m_count < MCOUNT )//at least have MCOUNT points in the mask { m_count = 0; gray_sum = 0.0; n = (2*adapt+1)*(2*adapt+1); neighbor = fl_vector(n); for(t = i - adapt; t <= i + adapt; t ++) for(h = j - adapt; h <= j + adapt; h ++) { if((t<0)||(t>=height)||(h<0)||(h>=width)) continue; pixm = im_get_pixf(srcIm0,t,h); //if(fabs(pixm - cutpix) > f)//orig point if(flag[t][h]==1) { neighbor[m_count]= pixm; m_count ++; } } if(m_count < MCOUNT) { adapt ++; free_fl_vector(neighbor); } } togetmed = fl_vector(m_count); for(r = 0;r < m_count;r ++) togetmed[r] = neighbor[r]; free_fl_vector(neighbor); pixm = get_median(togetmed,m_count);//median count_adapt[adapt] ++; im_put_pixf(pixm,destIm,i,j); free_fl_vector(togetmed); } else { im_put_pixf(pix,destIm,i,j); count_adapt[0] ++; } }/*----------------------------------------------------------------------------*/
break;
case 2:/*-------------------------------mode--------------------------------------*/ /* [Statistics] The value or item occurring most frequently in a series of observations or statistical data*/ for(i = 0;i < height; i ++) for(j = 0;j < width; j ++) { pix = im_get_pixf(srcIm0,i,j); adapt = 1; //if(fabs(pix - cutpix) < f )//lost point if(flag[i][j]==0) { m_count = 0; gray_sum = 0.0; ws = 0; while(m_count < MCOUNT )//at least have MCOUNT points in the mask { m_count = 0; gray_sum = 0.0; ws = 0; n = adapt*(2*adapt+1)*(2*adapt+1); neighbor = fl_vector(n); for(t = i - adapt; t <= i + adapt; t ++) for(h = j - adapt; h <= j + adapt; h ++) { if((t<0)||(t>=height)||(h<0)||(h>=width)) continue; pixm = im_get_pixf(srcIm0,t,h); //if(fabs(pixm - cutpix) > f)//orig point if(flag[t][h]==1) { w = adapt+1-IMAX(abs(i-t),abs(j-h)); m_count ++; while(w!=0) { neighbor[ws++]= pixm; w--; } } } if(m_count < MCOUNT) { adapt ++; free_fl_vector(neighbor); } } togetmed = fl_vector(ws); for(r = 0;r < ws;r ++) togetmed[r] = neighbor[r]; free_fl_vector(neighbor); pixm = get_mode(togetmed,ws);//mode count_adapt[adapt] ++; im_put_pixf(pixm,destIm,i,j); free_fl_vector(togetmed); } else { im_put_pixf(pix,destIm,i,j); count_adapt[0] ++; } }/*--------------------------------------------------------------------------*/
break; case 3:/*-------------------------------mm--------------------------------------*/ for(i = 0;i < height; i ++) for(j = 0;j < width; j ++) { pix = im_get_pixf(srcIm0,i,j); adapt = 1; //if(fabs(pix - cutpix) < f )//lost point if(flag[i][j]==0) { m_count = 0; gray_sum = 0.0; ws = 0; while(m_count < MCOUNT )//at least have MCOUNT points in the mask { m_count = 0; gray_sum = 0.0; ws = 0; n = (2*adapt+1)*(2*adapt+1); neighbor = fl_vector(n); for(t = i - adapt; t <= i + adapt; t ++) for(h = j - adapt; h <= j + adapt; h ++) { if((t<0)||(t>=height)||(h<0)||(h>=width)) continue; pixm = im_get_pixf(srcIm0,t,h); //if(fabs(pixm - cutpix) > f)//orig point if(flag[t][h]==1) { w = adapt+1-IMAX(abs(i-t),abs(j-h)); gray_sum += w*pixm; neighbor[m_count]= pixm; ws += w; m_count ++; } } if(m_count < MCOUNT) { adapt ++; free_fl_vector(neighbor); } } togetmed = fl_vector(m_count); for(r = 0;r < m_count;r ++) togetmed[r] = neighbor[r]; free_fl_vector(neighbor); pixm1 = (float)(gray_sum/ws);//w-mean pixm2 = get_median(togetmed,m_count);//median pixm3 = get_mode(togetmed,m_count);//mode //printf("mean = %f,median = %f,mode = %ff\n",pixm1,pixm2,pixm3); count_adapt[adapt] ++; //im_put_pixf((pixm1+pixm2+pixm3)/3 ,destIm,i,j); if(pol < 21) { im_put_pixf(pixm2 ,destIm,i,j);//median } else im_put_pixf(pixm1 ,destIm,i,j);//w-mean free_fl_vector(togetmed); } else { im_put_pixf(pix,destIm,i,j); count_adapt[0] ++; } }/*--------------------------------------------------------------------------*/ break; case 4:/*-------------------------------w-mean-------------------------------------*/ for(i = 0;i < height; i ++) for(j = 0;j < width; j ++) { pix = im_get_pixf(srcIm0,i,j); adapt = 1; //if(fabs(pix - cutpix) < f )//lost point if(flag[i][j]==0) { m_count = 0; gray_sum = 0.0; ws = 0; while(m_count < MCOUNT )//at least have MCOUNT points in the mask { m_count = 0; gray_sum = 0.0; ws = 0; for(t = i - adapt; t <= i + adapt; t ++) for(h = j - adapt; h <= j + adapt; h ++) { if((t<0)||(t>=height)||(h<0)||(h>=width)) continue; pixm = im_get_pixf(srcIm0,t,h); //if(fabs(pixm - cutpix) > f)//orig point if(flag[t][h]==1)//orig pexel { w = adapt+1-IMAX(abs(i-t),abs(j-h)); gray_sum += w*pixm; ws += w;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -