欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Min/Max Edge Detection

程序员文章站 2024-01-28 08:43:34
...

                                       Min/Max Edge Detection

Min/Max 边缘检测算法的主要思路:

  1. 图像去噪。本文算法中所使用的是中值滤波算法进行图像去噪处理
  2. 计算邻域内最大最小值。图像去噪之后,计算邻域内最大最小值
  3. 把最大最小值之差赋值给当前模板的中心像素

中值滤波算法主要代码为:

#include"Sharp.h"
int Sort(T_U8 *bArray,int size)
{
	int		i;
	int		j,k;	
	int  bTemp;

	// 用冒泡法对数组进行排序
	for (j = 0; j < size; j ++)
	{
		for (i = 0; i < size- j; i ++)
		{
			if (bArray[i] > bArray[i + 1])
			{
				// 互换
				bTemp = bArray[i];
				bArray[i] = bArray[i + 1];
				bArray[i + 1] = bTemp;
			}
		}
	}
}


T_U8 MedianFilter(T_U8 *src_img,DWORD width,DWORD height,T_U8 filterSize)
{
	int i,j,m,n,k,medianIndex,neighbourCount,filteroffset,index,neighbour,medianFilterPos;
	T_U8 *medianFilterResult,*Neighbours,*dst;
	T_U32 Gray_line_byte =  (width*8/8+3)/4*4;

	neighbourCount = filterSize*filterSize;
	medianIndex = neighbourCount/2;
	filteroffset = (filterSize-1)/2;

	Neighbours = (T_U8*)malloc(neighbourCount);
	if(Neighbours == NULL)
	{
		printf("can't malloc\r\n" );
		return 0;
	}
	memset(Neighbours,0,neighbourCount);
	dst = src_img + 54 + 256*sizeof(RGBQUAD);

	for(i = filteroffset;i < height-filteroffset;i++)
	{
		for(j = filteroffset;j < width-filteroffset;j++)
		{
			index = i*Gray_line_byte+j;
			for(m = -filteroffset,neighbour = 0;m <= filteroffset;m++)
			{
				for(n = -filteroffset;n <= filteroffset;n++,neighbour++)
				{
					medianFilterPos = index+m*Gray_line_byte+n;
					Neighbours[neighbour] = dst[medianFilterPos];
				}
			}

			Sort(Neighbours,neighbourCount);
			
			dst[index] = Neighbours[medianIndex];
		}
	}

	return 0;
}

 最大最小值边缘检测算法代码如下:

#include"Sharp.h"

T_U8* MinMaxEdgeDetection(T_U8 *src_img,DWORD width,DWORD height,T_U8 filterSize)
{
	int i,j,m,n,k,medianIndex,neighbourCount,filteroffset,index,neighbour,medianFilterPos,MinPix = 0,MaxPix = 0;
	T_U8 *MinMaxEdgeDecter,*Neighbours,*dst;
	T_U32 Gray_line_byte =  (width*8/8+3)/4*4;

	neighbourCount = filterSize*filterSize;
	medianIndex = neighbourCount/2;
	filteroffset = (filterSize-1)/2;
	MinMaxEdgeDecter = (T_U8*)malloc(height*Gray_line_byte);
	Neighbours = (T_U8*)malloc(neighbourCount);
	if(Neighbours == NULL)
	{
		printf("can't malloc\r\n" );
		return 0;
	}
	memset(Neighbours,0,neighbourCount);
	memset(MinMaxEdgeDecter,0,height*Gray_line_byte);
	dst = src_img + 54 + 256*sizeof(RGBQUAD);

	for(i = filteroffset;i < height-filteroffset;i++)
	{
		for(j = filteroffset;j < width-filteroffset;j++)
		{
			index = i*Gray_line_byte+j;
			MinPix = 99999;
			MaxPix = -99999;
			for(m = -filteroffset,neighbour = 0;m <= filteroffset;m++)
			{
				for(n = -filteroffset;n <= filteroffset;n++,neighbour++)
				{
					medianFilterPos = index+m*Gray_line_byte+n;
					Neighbours[neighbour] = dst[medianFilterPos];
				}
			}

			Sort(Neighbours,neighbourCount);
			MinPix = Neighbours[0];
			MaxPix = Neighbours[neighbourCount-1];
			
			MinMaxEdgeDecter[index] = CLIP255((int)((MaxPix-MinPix)));
		}
	}

	return MinMaxEdgeDecter;
}

最大最小值边缘检测结果为:

Min/Max Edge Detection

Min/Max Edge Detection

Min/Max Edge Detection