做智能车比赛的时候学到的八邻域搜线,代码仅供参考学习,不能直接使用
//八邻域搜线
if(left_findflag)
{
L_edge[0].row = jilu_row_l;
L_edge[0].col = jilu_col_l;
L_edge[0].flag = 1;
uint8 curr_row = jilu_row_l;
uint8 curr_col = jilu_col_l;
dire_left = 0;
center_turn_flag = 1;
for(int i = 1;i < L_search_amount; i++)
{
if(curr_row < L_edge_end_row || curr_row>Image_H-1||curr_row+1<L_edge_end_row) break;
if(curr_col > max_col || curr_col < min_col)
{
if(++L_search_edge_count == 3)
break;
}
else
L_search_edge_count = 0;
if(dire_left != 2&&black_(image[curr_row-1][curr_col-1])&&white_(image[curr_row-1][curr_col]))
{
curr_row = curr_row -1;
curr_col = curr_col -1;
L_edge_count = L_edge_count +1;
dire_left = 7;
L_edge[i].row = curr_row;
L_edge[i].col = curr_col;
L_edge[i].flag = 1;
}
else if(dire_left!=3&&black_(image[curr_row-1][curr_col+1])&&white_(image[curr_row][curr_col+1]))
{
curr_row = curr_row -1;
curr_col = curr_col + 1;
L_edge_count = L_edge_count + 1;
dire_left = 6;
L_edge[i].row = curr_row;
L_edge[i].col = curr_col;
L_edge[i].flag = 1;
}
else if(black_(image[curr_row-1][curr_col])&&white_(image[curr_row-1][curr_col+1]))
{
curr_row = curr_row - 1;
L_edge_count = L_edge_count + 1;
dire_left = 0;
L_edge[i].row = curr_row;
L_edge[i].col = curr_col;
L_edge[i].flag = 1;
}
else if(dire_left!=5&&black_(image[curr_row][curr_col-1])&&white_(image[curr_row-1][curr_col-1]))
{
curr_col = curr_col - 1;
L_edge_count = L_edge_count +1;
dire_left = 4;
L_edge[i].row = curr_row;
L_edge[i].col = curr_col;
L_edge[i].flag = 1;
}
else if(dire_left!=4&&black_(image[curr_row][curr_col+1])&&white_(image[curr_row+1][curr_col+1]))
{
curr_col = curr_col + 1;
L_edge_count = L_edge_count +1;
dire_left = 5;
L_edge[i].row = curr_row;
L_edge[i].col = curr_col;
L_edge[i].flag = 1;
}
else if(dire_left!=6&&black_(image[curr_row+1][curr_col-1])&&white_(image[curr_row][curr_col-1]))
{
curr_row = curr_row + 1;
curr_col = curr_col -1;
L_edge_count = L_edge_count +1;
dire_left = 3;
L_edge[i].row = curr_row;
L_edge[i].col = curr_col;
L_edge[i].flag = 1;
}
else if(dire_left!=7&&black_(image[curr_row+1][curr_col+1])&&white_(image[curr_row+1][curr_col]))
{
curr_row = curr_row + 1;
curr_col = curr_col + 1;
L_edge_count = L_edge_count +1;
dire_left = 2;
L_edge[i].row = curr_row;
L_edge[i].col = curr_col;
L_edge[i].flag = 1;
}
else
break;
}
}
if(right_findflag)
{
R_edge[0].row = jilu_row_r;
R_edge[0].col = jilu_col_r;
R_edge[0].flag = 1;
uint8 curr_row = jilu_row_r;
uint8 curr_col = jilu_col_r;
dire_right = 0;
for(int i = 1;i<R_search_amount;i++)
{
if(curr_row < L_edge_end_row ||curr_row>Image_H-1||curr_row+1<L_edge_end_row) break;
if(curr_col > max_col || curr_col < min_col)
{
if(++R_search_edge_count == 3)
break;
}
else R_search_edge_count = 0;
if(curr_col<Image_W&&dire_right!=3&&black_(image[curr_row-1][curr_col+1])&&white_(image[curr_row-1][curr_col]))
{
curr_row = curr_row - 1;
curr_col = curr_col + 1;
R_edge_count = R_edge_count + 1;
dire_right = 6;
R_edge[i].row = curr_row;
R_edge[i].col = curr_col;
R_edge[i].flag = 1;
}
else if(dire_right!=2&&black_(image[curr_row-1][curr_col-1])&&white_(image[curr_row][curr_col-1]))
{
curr_row = curr_row-1;
curr_col = curr_col-1;
R_edge_count = R_edge_count + 1;
dire_right = 7;
R_edge[i].row = curr_row;
R_edge[i].col = curr_col;
R_edge[i].flag = 1;
}
else if(black_(image[curr_row-1][curr_col])&&white_(image[curr_row-1][curr_col-1]))
{
curr_row = curr_row - 1;
R_edge_count = R_edge_count + 1;
dire_right = 0;
R_edge[i].row = curr_row;
R_edge[i].col = curr_col;
R_edge[i].flag = 1;
}
else if(dire_right!=5&&black_(image[curr_row][curr_col-1])&&white_(image[curr_row+1][curr_col-1]))
{
curr_col = curr_col-1;
R_edge_count = R_edge_count + 1;
dire_right = 4;
R_edge[i].row = curr_row;
R_edge[i].col = curr_col;
R_edge[i].flag = 1;
}
else if(dire_right!=4&&black_(image[curr_row][curr_col+1])&&white_(image[curr_row-1][curr_col+1]))
{
curr_col = curr_col + 1;
R_edge_count = R_edge_count + 1;
dire_right = 5;
R_edge[i].row = curr_row;
R_edge[i].col = curr_col;
R_edge[i].flag = 1;
}
else if(dire_right!=6&&black_(image[curr_row+1][curr_col-1])&&white_(image[curr_row+1][curr_col]))
{
curr_row = curr_row + 1;
curr_col = curr_col - 1;
R_edge_count = R_edge_count + 1;
dire_right = 3;
R_edge[i].row = curr_row;
R_edge[i].col = curr_col;
R_edge[i].flag = 1;
}
else if(dire_right!=7&&black_(image[curr_row+1][curr_col+1])&&white_(image[curr_row][curr_col+1]))
{
curr_row = curr_row + 1;
curr_col = curr_col + 1;
R_edge_count = R_edge_count + 1;
dire_right = 2;
R_edge[i].row = curr_row;
R_edge[i].col = curr_col;
R_edge[i].flag = 1;
}
else
break;
}
}
if(left_findflag)
{
for(int i= 1;i<L_edge_count -1;i++)
{
L_edge[i].row = (L_edge[i].row + L_edge[i+1].row)/2;
L_edge[i].col = (L_edge[i].col + L_edge[i+1].col)/2;
}
for(int i= L_edge_count-1;i>1;i--)
{
L_edge[i].row = (L_edge[i].row + L_edge[i-1].row)/2;
L_edge[i].col = (L_edge[i].col + L_edge[i-1].col)/2;
}
edge_process_flag = 0;
if(L_edge_count >=70)
{
num_cnt = 0;
L_count = L_edge_count/2;
while(L_count<L_edge_count)
{
if(L_edge[L_count].row == L_edge[L_count+1].row)
num_cnt = num_cnt +1;
else
num_cnt = 0;
if(num_cnt > 5)
break;
L_count = L_count +1;
}
L_edge_count = L_count;
}
}
if(right_findflag)
{
for(int i = 1;i<R_edge_count-1;i++)
{
R_edge[i].row = (R_edge[i].row + R_edge[i+1].row)/2;
R_edge[i].col = (R_edge[i].col + R_edge[i-1].col)/2;
}
for(int i = R_edge_count-1;i>1;i--)
{
R_edge[i].row = (R_edge[i].row + R_edge[i+1].row)/2;
R_edge[i].col = (R_edge[i].col + R_edge[i-1].col)/2;
}
if(R_edge_count>=70)
{
num_cnt = 0;
R_count = R_edge_count/2;
while(R_count < R_edge_count)
{
if(R_edge[R_count].row == R_edge[R_count+1].row)
num_cnt = num_cnt + 1;
else
num_cnt = 0;
if(num_cnt > 5)
break;
R_count = R_count + 1;
}
R_edge_count = R_count;
}
}
if(fabs(L_edge[0].col-L_edge[L_edge_count].col)>Image_W/2&&fabs(L_edge[0].col-L_edge[L_edge_count].col)>Image_W/2)
enable_check_l_r_edge_same = 1;
if(enable_check_l_r_edge_same)
{
uint8 i_left = L_edge_count - 1;
uint8 chongdie_cnt = 0;
for(int i = 0;i<R_edge_count;i++)
{
if(fabs(R_edge[i].row-L_edge[i_left].row)<5&&fabs(R_edge[i].col-L_edge[i_left].col)<5)
{
chongdie_cnt = chongdie_cnt + 1;
i_left = i_left - 1;
if(chongdie_cnt > 3) break;
if(i_left<0) break;
}
}
if(chongdie_cnt>=2)
{
x2 = fabs(L_edge[0].row-R_edge[0].row);
if(fabs(L_edge[0].row-R_edge[0].row)>=15)
{
if(L_edge[0].row>R_edge[0].row)
left_findflag = 0;
else if(R_edge[0].row>L_edge[0].row)
right_findflag = 0;
}
else if(fabs(L_edge[0].row-R_edge[0].row)<15)
{
}
}
}
if(L_edge_count <10)
left_findflag = 0;
if(R_edge_count <10)
right_findflag = 0;
if(left_findflag && right_findflag)
{
if(jilu_col_l>jilu_col_r)
{
if(jilu_row_l>jilu_row_r)
left_findflag = 0;
else if(jilu_row_l<jilu_row_r)
right_findflag = 0;
}
}
if(L_edge_count-R_edge_count>30&&right_findflag)
{
right_findflag = 0;
R_edge_count = 0;
}
if(R_edge_count-L_edge_count>30&&left_findflag)
{
left_findflag = 0;
L_edge_count = 0;
}
if(left_findflag&&right_findflag)
{
if(jilu_row_r-jilu_row_l>Image_H/2&&L_edge_count-R_edge_count>10)
{
right_findflag = 0;
R_edge_count = 0;
}
else if(jilu_row_l-jilu_row_r>Image_H/2&&R_edge_count-L_edge_count>10)
{
left_findflag = 0;
L_edge_count = 0;
}
}
if(L_edge[L_edge_count-1].row - L_edge[1].row>-10)
left_findflag = 0;
if(R_edge[R_edge_count-1].row - R_edge[1].row>-10)
right_findflag = 0;
if(left_findflag || right_findflag)
enable_midline = 1;
else
enable_midline = 0;
if(enable_L_corner)
{
if(L_edge_count > 9)
{
for(int i = 0; i<L_edge_count-9;i++)
{
if(L_edge[i+4].row>5)
{
if((L_edge[i].col - L_edge[i + 4].col) * (L_edge[i + 8].col - L_edge[i + 4].col) +
(L_edge[i].row - L_edge[i + 4].row) * (L_edge[i + 8].row - L_edge[i + 4].row) > 0)
{
L_corner_angle = Get_angle(L_edge[i].row, L_edge[i].col, L_edge[i + 4].row, L_edge[i + 4].col, L_edge[i + 8].row, L_edge[i + 8].col);
if(L_edge[i+4].col>L_edge[i+8].col)
{
L_corner_flag = 1;
L_corner_row = L_edge[i+4].row;
L_corner_col = L_edge[i+4].col;
break;
}
}
}
}
}
}
if(enable_R_corner)
{
if(R_edge_count > 9)
{
for(int i = 0; i<R_edge_count-9;i++)
{
if(R_edge[i+4].row>5)
{
if((R_edge[i].col - R_edge[i + 4].col) * (R_edge[i + 8].col - R_edge[i + 4].col) +
(R_edge[i].row - R_edge[i + 4].row) * (R_edge[i + 8].row - R_edge[i + 4].row) > 0)
{
R_corner_angle = Get_angle(R_edge[i].row, R_edge[i].col, R_edge[i + 4].row, R_edge[i + 4].col, R_edge[i + 8].row, R_edge[i + 8].col);
if(R_edge[i+8].col>R_edge[i+4].col)
{
R_corner_flag = 1;
R_corner_row = R_edge[i+4].row;
R_corner_col = R_edge[i+4].col;
break;
}
}
}
}
}
}
}
``