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

📄 kuwaharafilter.c

📁 kuwaharaFilter sample good luck
💻 C
字号:
/*									
 *	KuwaharaFilter.c : kuwahara 鞘磐俊 狼茄 雀急				
 *	Programmed by Lee, Moon-Ho (conv2@nvision.gsnu.ac.kr)	
 *	Last Modification date : 2001/04/17					
 * 	窃荐 龋免 : KuwaharaGrayIMG(&KuwaharaGrayIMG, ˇ, )		
 */									

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/dirent.h>
#include <ctype.h>
#include <math.h>

double kuwahara_filter(double **Mask, int Mask_height, int Mask_width);
double Min(double A, double B);

void KuwaharaGrayIMG(unsigned char ***KuwaharaGrayIMG, unsigned char **GrayIMG, int height, int width) 
{
	int i,j;
	int m,n;
	double **Mask;
	int Mask_height=5;
	int Mask_width=5;
	
	// Mask俊 措茄 皋葛府 且寸 棺 拌荐 涝仿
	// ------------------------------------

	Mask = (double **)calloc(Mask_height,sizeof(double *));
	for(i=0; i<Mask_height; i++)
		Mask[i] = (double *)calloc(Mask_width,sizeof(double));


	*KuwaharaGrayIMG = (unsigned char **)calloc(height, sizeof(unsigned char *));

	for(i=0; i<height; i++)
	{
		(*KuwaharaGrayIMG)[i] = (unsigned char *)calloc(width, sizeof(unsigned char));
	}


	// 雀急
	// ----

	for(i=0; i<height-Mask_height; i++)
	{
		for(j=0; j<width-Mask_width; j++)
		{
			double var=0.;
			for(m=0; m<Mask_height; m++)
			{
				for(n=0; n<Mask_width; n++)
				{
					// Mask_height x Mask_width 付胶农俊 蔼阑 盲款促.
					Mask[m][n] = GrayIMG[i+m][j+n];
				}
			}
			
			var = kuwahara_filter(Mask,Mask_height,Mask_width);
				
			// clamping

			if(var > 255.0) var = 255.0;
			else if(var < 0.0) var = 0.0;
			
			(*KuwaharaGrayIMG)[i][j] = (unsigned char)(var); 
		}
	}

	// Mask俊 且寸茄 皋葛府 秦力
	// -------------------------

	for(i=0; i<Mask_height; i++)
	{
		free(Mask);
	}
	free(Mask);

	return;
}

// 5x5, 7x7, 9x9 殿苞 鞍捞 圈荐狼 农扁甫 啊柳 付胶农俊父 利侩

double kuwahara_filter(double **Mask, int Mask_height, int Mask_width)
{

	int i, j;
	int Region_height;
	int Region_width;
	double Region_ave[4]={0.,}; // 阿 康开狼 乞闭蔼
	double Region_variable[4]={0.,}; // 阿 康开狼 盒魂蔼
	double **Region1_Mask;
	double **Region2_Mask;
	double **Region3_Mask;
	double **Region4_Mask;
	double output_value;
	double min_var;
	int index;

	// 康开喊 付胶农狼 农扁甫 沥窃. 
	Region_height = (Mask_height + 1)/2;
	Region_width = (Mask_width + 1)/2;

	// 阿 康开俊 措茄 皋葛府 且寸
	Region1_Mask = (double **)calloc(Region_height,sizeof(double *));
	Region2_Mask = (double **)calloc(Region_height,sizeof(double *));
	Region3_Mask = (double **)calloc(Region_height,sizeof(double *));
	Region4_Mask = (double **)calloc(Region_height,sizeof(double *));

	for(i=0; i<Region_height; i++)
	{
		Region1_Mask[i] = (double *)calloc(Region_width,sizeof(double));
		Region2_Mask[i] = (double *)calloc(Region_width,sizeof(double));
		Region3_Mask[i] = (double *)calloc(Region_width,sizeof(double));
		Region4_Mask[i] = (double *)calloc(Region_width,sizeof(double));
	}	

	// Mask俊 措茄 郴侩阑 阿 康开栏肺 颗变促.

	// Region 1
	for(i=0; i<Region_height; i++)
		for(j=0; j<Region_width; j++)
			Region1_Mask[i][j] = Mask[i][j];

	// Region 2 
	for(i=Region_height-1; i<Mask_height; i++)
		for(j=0; j<Region_width; j++)
			Region2_Mask[i-Region_height+1][j] = Mask[i][j];


	// Region 3 
	for(i=0; i<Region_height; i++)
		for(j=Region_width-1; j<Mask_width; j++)
			Region3_Mask[i][Region_width-j+1] = Mask[i][j];

	// Region 4 
	for(i=Region_height-1; i<Mask_height; i++)
		for(j=Region_width-1; j<Mask_width; j++)
			Region4_Mask[i-Region_height+1][j-Region_width+1] = Mask[i][j];


	// 阿 康开俊辑 乞闭蔼阑 备茄促.
	for(i=0; i<Region_height; i++)
	{
		for(j=0; j<Region_width; j++)
		{
			Region_ave[0] += Region1_Mask[i][j]; 	
			Region_ave[1] += Region2_Mask[i][j]; 	
			Region_ave[2] += Region3_Mask[i][j]; 	
			Region_ave[3] += Region4_Mask[i][j]; 	
		}
	}

	Region_ave[0] /= (double)(Region_height*Region_width);
	Region_ave[1] /= (double)(Region_height*Region_width);
	Region_ave[2] /= (double)(Region_height*Region_width);
	Region_ave[3] /= (double)(Region_height*Region_width);


	// 阿 康开俊辑 盒魂蔼阑 备茄促.
	for(i=0; i<Region_height; i++)
	{
		for(j=0; j<Region_width; j++)
		{
			Region_variable[0] = pow((Region_ave[0] - Region1_Mask[i][j]),2.0); 	
			Region_variable[1] = pow((Region_ave[1] - Region2_Mask[i][j]),2.0); 	
			Region_variable[2] = pow((Region_ave[2] - Region3_Mask[i][j]),2.0); 	
			Region_variable[3] = pow((Region_ave[3] - Region4_Mask[i][j]),2.0); 	
		}
	}
	Region_variable[0] /= (double)(Region_height*Region_width);
	Region_variable[1] /= (double)(Region_height*Region_width);
	Region_variable[2] /= (double)(Region_height*Region_width);
	Region_variable[3] /= (double)(Region_height*Region_width);

	// 啊厘 累篮 盒魂蔼阑 啊瘤绰 康开阑 茫绰促.
	min_var = Min(Region_variable[0],Min(Region_variable[1],Min(Region_variable[2],Region_variable[3])));
	for(i=0; i<4; i++)
	{
		if(min_var == Region_variable[i])
		{
			index = i;
			break;
		}
	}
	
	// 且寸茄 阿 康开 皋葛府 秦力 
	for(i=0; i<Region_height; i++)
	{
		free(Region1_Mask[i]);
		free(Region2_Mask[i]);
		free(Region3_Mask[i]);
		free(Region4_Mask[i]);
	}
	free(Region1_Mask);			
	free(Region2_Mask);		
	free(Region3_Mask);		
	free(Region4_Mask);		

	// 啊厘 累篮 盒魂蔼阑 啊瘤绰 康开狼 乞闭蔼阑 火涝腮促.
	output_value = Region_ave[index];
	return(output_value);
}

double Min(double A, double B)
{
	if(A <=B) return(A);
	else if( A>=B) return(B);
}

⌨️ 快捷键说明

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