2017年全国大学生电子设计竞赛(预测题)-腐蚀算法

2017年全国大学生电子设计竞赛(预测题)-腐蚀算法

腐蚀
腐蚀是图像处理中形态学图像处理部分的内容,具体可参考数字图像处理(第三版)[冈萨雷斯] 第九章 形态学图像处理处理

腐蚀算法的定义 --(摘自“数字图像处理(第三版)[冈萨雷斯] ”)
2017年全国大学生电子设计竞赛(预测题)-腐蚀算法_第1张图片
实例 --(摘自“数字图像处理(第三版)[冈萨雷斯] ”)
2017年全国大学生电子设计竞赛(预测题)-腐蚀算法_第2张图片
2017年全国大学生电子设计竞赛(预测题)-腐蚀算法_第3张图片
2017年全国大学生电子设计竞赛(预测题)-腐蚀算法_第4张图片
源码--(源自网络)
static int SearchCenter(unsigned int* x, unsigned int* y, const TARGET_CONDITION* condition, SEARCH_AREA* area )
{
	unsigned int i, j, k;
	unsigned int FailCount=0;
	unsigned int SpaceX, SpaceY;
	COLOR_RGB rgb;
	COLOR_HLS hls;
	
	SpaceX = condition->WIDTH_MIN / 3;  //X间隔
	SpaceY = condition->HEIGHT_MIN / 3; //Y间隔,将区域矩形化,便于处理
	
	for(i=area->Y_Start; iY_End; i+=SpaceY)//Y轴扫描
	{
		for(j=area->X_Start; jX_End; j+=SpaceX)//X轴扫描
		{
			FailCount = 0;//累加器,用于对累加匹配的颜色成功个数
			for(k=0; k( (SpaceX+SpaceY) >> ALLOW_FAIL_PER ))//提高容错率
					break;
				
			}
			
			if(k == SpaceX+SpaceY)
			{//腐蚀中心的表达式
				*x = j + SpaceX / 2;
				*y = i + SpaceY / 2;
				return 1;
			}
			
		}
			
	}
	
	return 0;
		
}

static int Corrode(unsigned int oldX, unsigned int oldY, const TARGET_CONDITION* condition, RESULT* result )
{
	unsigned int Xmin, Xmax, Ymin, Ymax;
	unsigned int i;
	unsigned int FailCount=0;
	COLOR_RGB rgb;
	COLOR_HLS hls;
	
	for(i=oldX; i>IMG_X; i--)
	{
		ReadColor(i, oldY, &rgb);
		RGB2HSL(&rgb, &hls);
		if(!ColorMatch(&hls, condition))
			FailCount++;
		if(FailCount>(((condition->WIDTH_MIN+condition->WIDTH_MAX)>>2)>>ALLOW_FAIL_PER))
			break;
	}
	Xmin=i;
	
	FailCount=0;
	for(i=oldX; i(((condition->WIDTH_MIN+condition->WIDTH_MAX)>>2)>>ALLOW_FAIL_PER))
			break;
	}
	Xmax=i;
	
	FailCount=0;
	for(i=oldY; i>IMG_Y; i--)
	{
		ReadColor(oldX, i, &rgb);
		RGB2HSL(&rgb, &hls);
		if(!ColorMatch(&hls, condition))
			FailCount++;
		if(FailCount>(((condition->HEIGHT_MIN+condition->HEIGHT_MAX)>>2)>>ALLOW_FAIL_PER))
			break;
	}
	Ymin=i;
	
	FailCount=0;
	for(i=oldY; i(((condition->HEIGHT_MIN+condition->HEIGHT_MAX)>>2)>>ALLOW_FAIL_PER))
			break;
	}
	Ymax=i;
	
	FailCount=0;
	
	result->x = (Xmin + Xmax) / 2;
	result->y = (Ymin + Ymax) / 2;
	result->w = (Xmax - Xmin);
	result->h = (Ymax - Ymin);
	
	if( (result->w > condition->WIDTH_MIN) && (result->w < condition->WIDTH_MAX) &&
			(result->h > condition->HEIGHT_MIN) && (result->h < condition->HEIGHT_MAX)  )
		return 1;
	else
		return 0;
}


int Trace(const TARGET_CONDITION* condition, RESULT* result_final)
{
	unsigned int i;
	static unsigned int x0, y0, Flag = 0;
	static SEARCH_AREA area = {IMG_X, IMG_X+IMG_W, IMG_Y, IMG_Y+IMG_H};
	RESULT result;
	
	if(Flag == 0)
	{
		if(SearchCenter(&x0, &y0, condition, &area))
		{
			Flag = 1;
		}
		else
		{
			area.X_Start = IMG_X;
			area.X_End   = IMG_X+IMG_W;
			area.Y_Start = IMG_Y;
			area.Y_End   = IMG_Y+IMG_H;
			
			if(SearchCenter(&x0, &y0, condition, &area))
			{
				Flag = 0;
				return 0;
			}
		}
	}
	result.x = x0;
	result.y = y0;
	
	for(i=0; ix = result.x;
		result_final->y = result.y;
		result_final->w = result.w;
		result_final->h = result.h;
		Flag = 1;
		
		area.X_Start = result.x - ((result.w)>>1);
		area.X_End   = result.x + ((result.w)>>1);
		area.Y_Start = result.y - ((result.h)>>1);
		area.Y_End   = result.y + ((result.h)>>1);
		
		return 1;
	}
	else
	{
		Flag = 0;
		return 0;
	}
	
}

 
   

你可能感兴趣的:(全国大学生电子设计竞赛,TI杯)