Modules-old/ImageProcRobocup/lua_field_lines.cpp
//New function, find multiple connected line segments
//Instead of one best long line
New function, find multiple connected line segments
Instead of one best long line
New function find multiple connected line segments Instead of one best long line
int lua_field_lines(lua_State *L) {
  uint8_t *im_ptr = (uint8_t *) lua_touserdata(L, 1);
  if ((im_ptr == NULL) || !lua_islightuserdata(L, 1)) {
    return luaL_error(L, "Input image not light user data");
  }
  int ni = luaL_checkint(L, 2);
  int nj = luaL_checkint(L, 3);
  if (lua_gettop(L) >= 4)
    widthMax = luaL_checkint(L, 4);
  double connect_th = luaL_optnumber(L, 5, 1.4);
  int max_gap = luaL_optinteger(L, 6, 1);
  int min_count = luaL_optinteger(L, 7, 5);
  segment_init();
  for (int j = 0; j < nj; j++) {
    uint8_t *im_col = im_ptr + ni*j;
    for (int i = 0; i < ni; i++) {
      uint8_t label = *im_col++;
      int width = lineState(label);
      if ((width >= widthMin) && (width <= widthMax)) {
	      int iline = i - (width+1)/2;
	      addVerticalPixel(iline,j,connect_th,max_gap,width);
      }
    }
    segment_refresh();
  }
  segment_terminate();
  for (int i = 0; i < ni; i++) {
    uint8_t *im_row = im_ptr + i;
    for (int j = 0; j < nj; j++) {
      uint8_t label = *im_row;
      im_row += ni;
      int width = lineState(label);
      if ((width >= widthMin) && (width <= widthMax)) {
	      int jline = j - (width+1)/2;
	      addHorizontalPixel(i,jline,connect_th,max_gap,width);
      }
    }
    segment_refresh();
  }
  int valid_segments=0;
  for (int k=0;k<num_segments;k++){
    int dx=segments[k].x1-segments[k].x0;
    int dy=segments[k].y1-segments[k].y0;
    segments[k].length = (double) sqrt(dx*dx+dy*dy);
    if (segments[k].count>min_count){
      valid_segments++;
    }
  }
  lua_createtable(L, valid_segments, 0);
  int seg_count=0;
  for (int k=0;k<num_segments;k++){
    if (segments[k].count>min_count){
      lua_createtable(L, 0, 3);
      lua_pushstring(L, "length");
      lua_pushnumber(L, segments[k].length);
      lua_settable(L, -3);
      lua_pushstring(L, "max_width");
      lua_pushnumber(L, segments[k].max_width);
      lua_settable(L, -3);
      lua_pushstring(L, "endpoint");
      lua_createtable(L, 4, 0);
      lua_pushnumber(L, segments[k].x0);
      lua_rawseti(L, -2, 1);
      lua_pushnumber(L, segments[k].x1);
      lua_rawseti(L, -2, 2);
      lua_pushnumber(L, segments[k].y0);
      lua_rawseti(L, -2, 3);
      lua_pushnumber(L, segments[k].y1);
      lua_rawseti(L, -2, 4);
      lua_settable(L, -3);
      lua_rawseti(L, -2, seg_count+1);
      seg_count++;
    }
  }
  return 1;
}
int lua_field_lines ( lua_State * L ) { uint8_t * im_ptr = ( uint8_t * ) lua_touserdata ( L , 1 ) ; if ( ( im_ptr == NULL ) || ! lua_islightuserdata ( L , 1 ) ) { return luaL_error ( L , " " ) ; } int ni = luaL_checkint ( L , 2 ) ; int nj = luaL_checkint ( L , 3 ) ; if ( lua_gettop ( L ) >= 4 ) widthMax = luaL_checkint ( L , 4 ) ; double connect_th = luaL_optnumber ( L , 5 , 1.4 ) ; int max_gap = luaL_optinteger ( L , 6 , 1 ) ; int min_count = luaL_optinteger ( L , 7 , 5 ) ; segment_init ( ) ; for ( int j = 0 ; j < nj ; j ++ ) { uint8_t * im_col = im_ptr + ni * j ; for ( int i = 0 ; i < ni ; i ++ ) { uint8_t label = * im_col ++ ; int width = lineState ( label ) ; if ( ( width >= widthMin ) && ( width <= widthMax ) ) { int iline = i - ( width + 1 ) / 2 ; addVerticalPixel ( iline , j , connect_th , max_gap , width ) ; } } segment_refresh ( ) ; } segment_terminate ( ) ; for ( int i = 0 ; i < ni ; i ++ ) { uint8_t * im_row = im_ptr + i ; for ( int j = 0 ; j < nj ; j ++ ) { uint8_t label = * im_row ; im_row += ni ; int width = lineState ( label ) ; if ( ( width >= widthMin ) && ( width <= widthMax ) ) { int jline = j - ( width + 1 ) / 2 ; addHorizontalPixel ( i , jline , connect_th , max_gap , width ) ; } } segment_refresh ( ) ; } int valid_segments = 0 ; for ( int k = 0 ; k < num_segments ; k ++ ) { int dx = segments [ k ] . x1 - segments [ k ] . x0 ; int dy = segments [ k ] . y1 - segments [ k ] . y0 ; segments [ k ] . length = ( double ) sqrt ( dx * dx + dy * dy ) ; if ( segments [ k ] . count > min_count ) { valid_segments ++ ; } } lua_createtable ( L , valid_segments , 0 ) ; int seg_count = 0 ; for ( int k = 0 ; k < num_segments ; k ++ ) { if ( segments [ k ] . count > min_count ) { lua_createtable ( L , 0 , 3 ) ; lua_pushstring ( L , " " ) ; lua_pushnumber ( L , segments [ k ] . length ) ; lua_settable ( L , -3 ) ; lua_pushstring ( L , " " ) ; lua_pushnumber ( L , segments [ k ] . max_width ) ; lua_settable ( L , -3 ) ; lua_pushstring ( L , " " ) ; lua_createtable ( L , 4 , 0 ) ; lua_pushnumber ( L , segments [ k ] . x0 ) ; lua_rawseti ( L , -2 , 1 ) ; lua_pushnumber ( L , segments [ k ] . x1 ) ; lua_rawseti ( L , -2 , 2 ) ; lua_pushnumber ( L , segments [ k ] . y0 ) ; lua_rawseti ( L , -2 , 3 ) ; lua_pushnumber ( L , segments [ k ] . y1 ) ; lua_rawseti ( L , -2 , 4 ) ; lua_settable ( L , -3 ) ; lua_rawseti ( L , -2 , seg_count + 1 ) ; seg_count ++ ; } } return 1 ; }
New function, find multiple connected line segments
Instead of one best long line
New function find multiple connected line segments Instead of one best long line
// Scan for vertical line pixels: // Scan for horizontal field line pixels: // length field // max_width field // endpoint field