版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN_he_01/article/details/76889288
2017年全国大学生电子设计竞赛(预测题)-腐蚀算法
腐蚀
腐蚀是图像处理中形态学图像处理部分的内容,具体可参考数字图像处理(第三版)[冈萨雷斯] 的第九章 形态学图像处理处理。
腐蚀算法的定义
--(摘自“数字图像处理(第三版)[冈萨雷斯] ”)
实例
--(摘自“数字图像处理(第三版)[冈萨雷斯] ”)
源码--(源自网络)
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; i<area->Y_End; i+=SpaceY)//Y轴扫描
{
for(j=area->X_Start; j<area->X_End; j+=SpaceX)//X轴扫描
{
FailCount = 0;//累加器,用于对累加匹配的颜色成功个数
for(k=0; k<SpaceX+SpaceY; k++)
{//这段代码先进行横向的X轴,Y轴的中点进行横向扫描,然后又以X轴的中点,Y轴移动进行竖向扫描
if(k<SpaceX)//每分一块进行横向扫描
ReadColor( j+k, i+SpaceY/2, &rgb );
else//每分一块进行竖向扫描
ReadColor( j+SpaceX/2, i+k-SpaceX, &rgb );
RGB2HSL( &rgb, &hls );
if(!ColorMatch( &hls, condition ))//颜色范围匹配
FailCount++;
if(FailCount>( (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<IMG_X+IMG_W; 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;
}
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<IMG_Y+IMG_H; 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;
}
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; i<ITERATER_NUM; i++)
{
Corrode(result.x, result.y, condition, &result);
}
if( Corrode(result.x, result.y, condition, &result) )
{
x0 = result.x;
y0 = result.y;
result_final->x = 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;
}
}