⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spdenoise.c

📁 图像置乱代码
💻 C
📖 第 1 页 / 共 2 页
字号:
								m_count ++;							}						}					if(m_count < MCOUNT)						adapt ++;				}				pixm = (float)(gray_sum/ws);//mean				count_adapt[adapt] ++;				im_put_pixf(pixm,destIm,i,j);			}			else 			{				im_put_pixf(pix,destIm,i,j);				count_adapt[0] ++;			}		}/*---------------------------------------------------------------------*/			break;	case 5:/*-------------------------------hwm-------------------------------------*/	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;				hws = 0.0;				while(m_count < MCOUNT )//at least have MCOUNT points in the mask				{					m_count = 0;					gray_sum = 0.0;					hws = 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)//orig pexel							{								//gray_sum += ho[(int)pixm]*pixm;								//hws += ho[(int)pixm];								gray_sum += hn[(int)pixm]*pixm;								hws += hn[(int)pixm];								m_count ++;							}						}					if(m_count < MCOUNT)						adapt ++;				}				pixm = (float)(gray_sum/hws);//mean				count_adapt[adapt] ++;				im_put_pixf(pixm,destIm,i,j);			}			else 			{				im_put_pixf(pix,destIm,i,j);				count_adapt[0] ++;			}		}/*---------------------------------------------------------------------*/			break;	case 6:/*-------------------------------nhwfm-------------------------------------*/	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;				hws = 0.0;				while(m_count < MCOUNT )//at least have MCOUNT points in the mask				{					m_count = 0;					gray_sum = 0.0;					hws = 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)//orig pexel							{								gray_sum += ho[(int)pixm]*pixm;								hws += ho[(int)pixm];								m_count ++;							}						}					if(m_count < MCOUNT)						adapt ++;				}				pixm = (float)(gray_sum/hws);//mean				count_adapt[adapt] ++;				im_put_pixf(pixm,destIm,i,j);			}			else 			{				im_put_pixf(pix,destIm,i,j);				count_adapt[0] ++;			}		}/*---------------------------------------------------------------------*/			break;		case 7:/*-------------------------------w-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;				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_median(togetmed,ws);//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;
		default:
			break;
    	}/*---------------------------------------------	strcpy(filename1,filename0);	strcat(filename1, "_");	gcvt(pol,5,string);	strcat(filename1, string);	strcat(filename1,".bmp");	ImrectToBMP8(destIm,filename1);/*----------------------------------------------*/	for(i = 0;i < 25; i ++)		if(count_adapt[i] != 0)		{			printf("count_adapt[%d] = %d\n",i,count_adapt[i]);			//fprintf(fp,"count_adapt[%d] = %d\n",i,count_adapt[i]);		}/*----------------------------------------------*/	int count[256];	for(i = 0;i < 256; i ++)		count[i] = 0;	float mse = 0.0 ,psnr = 0.0 ,mae = 0.0, nmse = 0.0 ,op = 0.0;	for(i = 0; i < height; i ++)		for(j = 0; j < width; j ++)		{			IM_PIX_GET(destIm, i, j, pixb);			IM_PIX_GET(comp_orig, i, j, pixa);			pixa = (int)pixa;			pixb = (int)pixb;			pix = fabs(pixa - pixb);					count[(int)pix] ++;			mse += pix*pix;			mae += pix;			op += pixa*pixa;		}/*----------------------------------------------	for(i = 0;i < 256; i ++)		if(count[i] != 0)		{			printf("count[%d] = %d\n",i,count[i]);			fprintf(fp, "count[%d] = %d\n",i,count[i]);		}/*----------------------------------------------*/	nmse = mse / op;	mse = mse/(height*width);	psnr = 10*log10(255*255/mse);	mae = mae/(height*width);	printf("Percent       MSE       PSNR          MAE        NMSE   \n");	printf("%f   %f   %f   %f   %f\n", (float)pol/100, mse, psnr, mae, nmse);	//fprintf(fp, "MSE = %f , PSNR = %f , MAE = %f , NMSE = %f\n", mse, psnr, mae, nmse);	fprintf(fp, "%f  %f  %f  %f  %f\n", (float)pol/100, mse, psnr, mae, nmse);/*------------------------------------------------*/}	fclose(fp);/*------------------------------------------------*/	denoise_im = im_copy(destIm);	tv_imrect2(tv1, denoise_im);/*--------------------------------------------------------------------	gnuplot_ctrl *hmulti;	strcpy(fpng,filename0);	strcat(fpng,"_multi.png");	hmulti = gnuplot_init();	gnuplot_cmd(hmulti,"set term png");	gnuplot_cmd(hmulti,"set output '%s' ",fpng);	gnuplot_cmd(hmulti,"set xlabel 'noise ratio'");	gnuplot_cmd(hmulti,"set xrange [0:1]");	gnuplot_cmd(hmulti,"set multiplot");	gnuplot_cmd(hmulti,"set origin 0.0,0.5");	gnuplot_cmd(hmulti,"set size 0.5,0.5");	gnuplot_cmd(hmulti,"set ylabel 'PSNR'");	gnuplot_cmd(hmulti,"plot '%s' using 1:3 title 'PSNR' with linespoints", filenametxt);	gnuplot_cmd(hmulti,"set origin 0.5,0.5");	gnuplot_cmd(hmulti,"set size 0.5,0.5");	gnuplot_cmd(hmulti,"set ylabel 'MAE'");	gnuplot_cmd(hmulti,"plot '%s' using 1:4 title 'MAE' with linespoints", filenametxt);	gnuplot_cmd(hmulti,"set origin 0.0,0.0");	gnuplot_cmd(hmulti,"set size 0.5,0.5");	gnuplot_cmd(hmulti,"set ylabel 'MSE'");	gnuplot_cmd(hmulti,"plot '%s' using 1:2 title 'MSE' with linespoints", filenametxt);	gnuplot_cmd(hmulti,"set origin 0.5,0.0");	gnuplot_cmd(hmulti,"set size 0.5,0.5");	gnuplot_cmd(hmulti,"set ylabel 'NMSE'");	gnuplot_cmd(hmulti,"plot '%s' using 1:5 title 'NMSE' with linespoints", filenametxt);	gnuplot_cmd(hmulti,"unset multiplot");	gnuplot_cmd(hmulti,"reset");/*-----------------------------------------------------------------------	strcpy(fpng,filename0);	strcat(fpng,"_PSNR.png");	gnuplot_ctrl *hm;	hm = gnuplot_init();	gnuplot_cmd(hm,"set xlabel 'noise ratio'");	gnuplot_cmd(hm,"set ylabel 'PSNR'");	gnuplot_cmd(hm,"set xrange [0:1]");	gnuplot_cmd(hm,"plot '%s' using 1:3 title '%s' with linespoints ",filenametxt,fn);	gnuplot_cmd(hm,"set term png");	gnuplot_cmd(hm,"set output '%s' ",fpng);	gnuplot_cmd(hm,"replot");/*-----------------------------------------------------------------------*/ 	free_int_matrix(flag, height);	im_free(srcIm0);	im_free(destIm);	printf("\n=== %s complete! ===\n",fn);}static void comp_all_proc(void){	gnuplot_ctrl *h;	h = gnuplot_init();	switch( image_choice )	{		case 0:	gnuplot_cmd(h,"set xlabel 'noise ratio'");	gnuplot_cmd(h,"set ylabel 'PSNR'");	gnuplot_cmd(h,"set xrange [0:1]");	gnuplot_cmd(h,"plot '/home/wzz/newimscramdegree/result/denoise/lena_median2.txt' using 1:3 title 'median2' with linespoints pointtype 3 pointsize 2,\'/home/wzz/newimscramdegree/result/denoise/lena_w-mean2.txt' using 1:3 title 'w-mean2' with linespoints pointtype 4 pointsize 2,\'/home/wzz/newimscramdegree/result/denoise/lena_hwm.txt' using 1:3 title 'hwm' with linespoints  pointtype 5 pointsize 2,\'/home/wzz/newimscramdegree/result/denoise/nhwfm.txt' using 1:3 title 'nhwfm' with linespoints pointtype 6 pointsize 2,\'/home/wzz/newimscramdegree/result/denoise/lena_input.txt' using 1:3 title 'lena input' with linespoints  pointtype 7 pointsize 2");	gnuplot_cmd(h,"set term png");	gnuplot_cmd(h,"set output '/home/wzz/newimscramdegree/result/denoise/lena_PSNR.png' ");	gnuplot_cmd(h,"replot");			break;		case 1:	gnuplot_cmd(h,"set xlabel 'noise ratio'");	gnuplot_cmd(h,"set ylabel 'PSNR'");	gnuplot_cmd(h,"set xrange [0:1]");	gnuplot_cmd(h,"plot '/home/wzz/newimscramdegree/result/denoise/bridge_w-mean2.txt' using 1:3 title 'w-mean2' with linespoints pointtype 4,\'/home/wzz/newimscramdegree/result/denoise/bridge_hwm.txt' using 1:3 title 'hwm' with linespoints  pointtype 5,\'/home/wzz/newimscramdegree/result/denoise/bridge_input.txt' using 1:3 title 'bridge input' with linespoints  pointtype 8");	gnuplot_cmd(h,"set term png");	gnuplot_cmd(h,"set output '/home/wzz/newimscramdegree/result/denoise/bridge_PSNR.png' ");	gnuplot_cmd(h,"replot");		default:			break;	}}static void comp_mean_proc(void){	gnuplot_ctrl *h;	h = gnuplot_init();	gnuplot_cmd(h,"set xlabel 'noise ratio'");	gnuplot_cmd(h,"set ylabel 'PSNR'");	gnuplot_cmd(h,"set xrange [0:1]");	gnuplot_cmd(h,"plot '/home/wzz/newimscramdegree/result/denoise/lena_mean1.txt' using 1:3 title 'mean1' with linespoints ,'/home/wzz/newimscramdegree/result/denoise/lena_mean2.txt' using 1:3 title 'mean2' with linespoints ,'/home/wzz/newimscramdegree/result/denoise/lena_mean3.txt' using 1:3 title 'mean3' with linespoints ");	gnuplot_cmd(h,"set term png");	gnuplot_cmd(h,"set output '/home/wzz/newimscramdegree/result/denoise/lena_mean_PSNR.png' ");	gnuplot_cmd(h,"replot");}static void comp_median_proc(void){	gnuplot_ctrl *h;	h = gnuplot_init();	gnuplot_cmd(h,"set xlabel 'noise ratio'");	gnuplot_cmd(h,"set ylabel 'PSNR'");	gnuplot_cmd(h,"set xrange [0:1]");	gnuplot_cmd(h,"plot '/home/wzz/newimscramdegree/result/denoise/lena_median1.txt' using 1:3 title 'median1' with linespoints ,'/home/wzz/newimscramdegree/result/denoise/lena_median2.txt' using 1:3 title 'median2' with linespoints ,'/home/wzz/newimscramdegree/result/denoise/lena_median3.txt' using 1:3 title 'median3' with linespoints ");	gnuplot_cmd(h,"set term png");	gnuplot_cmd(h,"set output '/home/wzz/newimscramdegree/result/denoise/lena_median_PSNR.png' ");	gnuplot_cmd(h,"replot");}static void comp_mode_proc(void){	gnuplot_ctrl *h;	h = gnuplot_init();	gnuplot_cmd(h,"set xlabel 'noise ratio'");	gnuplot_cmd(h,"set ylabel 'PSNR'");	gnuplot_cmd(h,"set xrange [0:1]");	gnuplot_cmd(h,"plot '/home/wzz/newimscramdegree/result/denoise/lena_mode1.txt' using 1:3 title 'mode1' with linespoints ,'/home/wzz/newimscramdegree/result/denoise/lena_mode2.txt' using 1:3 title 'mode2' with linespoints ,'/home/wzz/newimscramdegree/result/denoise/lena_mode3.txt' using 1:3 title 'mode3' with linespoints ");	gnuplot_cmd(h,"set term png");	gnuplot_cmd(h,"set output '/home/wzz/newimscramdegree/result/denoise/lena_mode_PSNR.png' ");	gnuplot_cmd(h,"replot");}static void comp_mm_proc(void){	gnuplot_ctrl *h;	h = gnuplot_init();	gnuplot_cmd(h,"set xlabel 'noise ratio'");	gnuplot_cmd(h,"set ylabel 'PSNR'");	gnuplot_cmd(h,"set xrange [0:1]");	gnuplot_cmd(h,"plot '/home/wzz/newimscramdegree/result/denoise/lena_mm1.txt' using 1:3 title 'mm1' with linespoints ,\'/home/wzz/newimscramdegree/result/denoise/lena_mm2.txt' using 1:3 title 'mm2' with linespoints ,\'/home/wzz/newimscramdegree/result/denoise/lena_mm3.txt' using 1:3 title 'mm3' with linespoints ");	gnuplot_cmd(h,"set term png");	gnuplot_cmd(h,"set output '/home/wzz/newimscramdegree/result/denoise/lena_mm_PSNR.png' ");	gnuplot_cmd(h,"replot");}static void comp_w_median_proc(void){	gnuplot_ctrl *h;	h = gnuplot_init();	gnuplot_cmd(h,"set xlabel 'noise ratio'");	gnuplot_cmd(h,"set ylabel 'PSNR'");	gnuplot_cmd(h,"set xrange [0:1]");	gnuplot_cmd(h,"plot '/home/wzz/newimscramdegree/result/denoise/lena_w-median1.txt' using 1:3 title 'w-median1' with linespoints ,'/home/wzz/newimscramdegree/result/denoise/lena_w-median2.txt' using 1:3 title 'w-median2' with linespoints ,'/home/wzz/newimscramdegree/result/denoise/lena_w-median3.txt' using 1:3 title 'w-median3' with linespoints ");	gnuplot_cmd(h,"set term png");	gnuplot_cmd(h,"set output '/home/wzz/newimscramdegree/result/denoise/lena_w-median_PSNR.png' ");	gnuplot_cmd(h,"replot");}static void fulldraw(Tv *tv){     tv_imrect2(tv, orig_im);}static void fulldraw1(Tv *tv){     tv_imrect2(tv, denoise_im);}static void tv_choice_proc(choice)
int     choice;
{
    switch (choice)
    	{
    	case 0: tv_set_next(tv);//original
		break;
    	case 1: tv_set_next(tv1);//denoise
		break;
    	default:error("tv_choice_proc: unknown choice\n", warning);
		break;
    	}}static void tool_proc(void)
{
    static int x = 200, y = 0;
    static int xr = 0, yr = 0;

    tv_tool(x+xr, y+yr);
    xr += 275;
    yr += 0;
    if (x+xr>780)
    {
       xr = yr = 0;
       y += 50;
    }
    if (y>400) y = 200;
}

/********** Tool creation **********/
void            spdenoise_tool(int x, int y)
{
	static void *tool = NULL;	if (tool)
	{
		tw_show_tool(tool);
		return;
	}
	tool = (void *)tw_tool("remove salt&peper noise tool", x, y);	tv = tv_create("original");
     	tv1 = tv_create("denoised");
        tw_choice("Tv choice", tv_choice_proc, 0, "original","denoised",NULL);	tv_set_fulldraw(tv, fulldraw);	tv_set_fulldraw(tv1,fulldraw1);	/*---------------------------*/	tw_newrow();        tw_choice("input image choice", image_choice_proc, 0, "lena","bridge", NULL);	tw_newrow();	tw_choice("denoise method:", denoise_choice_proc, 0,\
              "mean", "median", "mode", "mm","w-mean","hwm", "nhwfm","w-median",NULL);	tw_newrow();	tw_iglobal("not less than ", &MCOUNT, 5);	tw_label("original points in the mask ");
	tw_newrow();	tw_button("denoise",denoise_proc,NULL);	tw_newrow();	tw_button("comp_all",comp_all_proc,NULL);	tw_newrow();	tw_button("comp_mean",comp_mean_proc,NULL);	tw_button("comp_median",comp_median_proc,NULL);	tw_button("comp_mode",comp_mode_proc,NULL);	tw_button("comp_mm",comp_mm_proc,NULL);	tw_button("comp_w_median",comp_w_median_proc,NULL);	tw_newrow();	tw_newrow();	tw_button("nhwfm", nhwfm_proc, NULL);
	tw_end_tool();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -