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; } }