Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phblob.h

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission :
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008 
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 #ifndef _PHBLOB_H
00027 #define _PHBLOB_H
00028 
00034 #include <phStdint.h>
00035 #include <ImageDefinitions.h>
00036 
00041 /* ---------------------------------------------------------------------- */
00042 #ifdef __cplusplus
00043 extern "C" 
00044 {
00045 #endif
00046 
00047 /* ------------------------------------------------------------------------ */
00052 typedef struct phblob_coord_t
00053 {
00054     uint32_t x;
00055     uint32_t y;
00056 } phblob_coord_type;
00057 
00058 /* ------------------------------------------------------------------------ */
00059 #define phSEGMENT_CONNECTION_COUNT 6
00060 
00064 typedef struct phblob_line_s
00065 {
00066     int32_t     index;
00067     uint32_t    length;
00068 
00069     phblob_coord_type  a;
00070     phblob_coord_type  b;
00071 
00072     /* for use only in the row_ptr_array */
00073     /* These don't denote that the lines linked to are actually part
00074      * of a blob; only that the lines belong to the same row */
00075     struct phblob_line_s   *next;
00076     int32_t                 next_index;
00077     struct phblob_line_s   *previous;
00078     int32_t                 previous_index;
00079 
00080     /* 1.) For use as blob linked segments */
00081     /* a.) connects the ends of each series of segments */
00082     /* the last_* pointers are used during traversal */
00083     struct phblob_line_s *last_up; 
00084     struct phblob_line_s *last_down;
00085     /* b.) connects the first segments of each series */
00086     struct phblob_line_s *first_up;
00087     struct phblob_line_s *first_down;
00088     /* c.) left/right connections within a series */
00089     /* These are also used in traversal */
00090     struct phblob_line_s *left;
00091     struct phblob_line_s *right;
00092     
00093     /* Can be used to reconstruct the pointers above when
00094      * all the segments are stored in an array (for example,
00095      * the way they are stored in the line pool). This is 
00096      * useful only when MEMCPYing the line pool to another
00097      * memory segment, at which point all the pointers above
00098      * will become invalid. */
00099     int32_t first_up_index;
00100     int32_t first_down_index;
00101     int32_t last_up_index;
00102     int32_t last_down_index;
00103     int32_t left_index;
00104     int32_t right_index;
00105     
00106     /* These are equal to the named pointers/variables above with the 
00107      * exception that they make the code used for traversing a 
00108      * grouping of interconnectedline segments */
00109     struct phblob_line_s   *connection[phSEGMENT_CONNECTION_COUNT];
00110     int32_t                 connection_index[phSEGMENT_CONNECTION_COUNT];
00111 
00112     /* Blob index value; Used when traversing the list of line segments
00113      * to identify blob groupings and it speeds searching also */
00114     /* Instead of wasting half the bit space available for the sole 
00115      * reason of having -1 as a value, MAX_INT will be used to denote
00116      * no blob grouping ID */
00117     int32_t blob_index; /* MAX_INT == no blob */
00118    
00119     /* Used in the iterative traversing of the line segment connections
00120      * to identify membership in a blob. These are made part of the 
00121      * structure for the case where user code wants to traverse as well,
00122      * not as much extra setup is required. This does add to memory
00123      * usage though.*/
00124     uint32_t                visited;
00125     struct phblob_line_s   *previous_visit;
00126     int32_t                 previous_visit_index; /* INT_MAX == no previous */
00127     
00128     int32_t         value_low[phCOLOR_MAXELEMS];
00129     int32_t         value[phCOLOR_MAXELEMS];
00130     int32_t         value_high[phCOLOR_MAXELEMS];
00131     
00132 } phblob_line_segment;
00133 
00134 /* Helper macros for the 'connections' array */
00135 /* The values are reordered compared to the order of the named pointer/variables
00136  * because of the traversal algorithm. The unused connections are still kept
00137  * around for consistency */
00138 /* Order of traversal Right:0,Up:1,Left:2,Down:3 */
00139 #define LAST_UP_VAL     (1)
00140 #define LAST_DOWN_VAL   (3)
00141 #define FIRST_UP_VAL    (4) /* not 1, not used in traversal */
00142 #define FIRST_DOWN_VAL  (5) /* not 3, not used in traversal */
00143 #define LEFT_VAL        (2)
00144 #define RIGHT_VAL       (0)
00145 
00146 /* These are never modified */
00147 #define OPPOSITE_DIR_BIT(dir)       (1<<((dir+2) %4))
00148 
00149 #define LAST_UP_PTR(segptr)         (segptr->connection[LAST_UP_VAL])
00150 #define LAST_DOWN_PTR(segptr)       (segptr->connection[LAST_DOWN_VAL])
00151 #define UP_PTR(s)                   LAST_UP_PTR(s)
00152 #define DOWN_PTR(s)                 LAST_DOWN_PTR(s)
00153 #define FIRST_UP_PTR(segptr)        (segptr->connection[FIRST_UP_VAL])
00154 #define FIRST_DOWN_PTR(segptr)      (segptr->connection[FIRST_DOWN_VAL])
00155 #define LEFT_PTR(segptr)            (segptr->connection[LEFT_VAL])
00156 #define RIGHT_PTR(segptr)           (segptr->connection[RIGHT_VAL])
00157 
00158 #define LAST_UP_INDEX(segptr)       (segptr->connection_index[LAST_UP_VAL])
00159 #define LAST_DOWN_INDEX(segptr)     (segptr->connection_index[LAST_DOWN_VAL])
00160 #define UP_INDEX(s)                 LAST_UP_INDEX(s)
00161 #define DOWN_INDEX(s)               LAST_DOWN_INDEX(s)
00162 #define FIRST_UP_INDEX(segptr)      (segptr->connection_index[FIRST_UP_VAL])
00163 #define FIRST_DOWN_INDEX(segptr)    (segptr->connection_index[FIRST_DOWN_VAL])
00164 #define LEFT_INDEX(segptr)          (segptr->connection_index[LEFT_VAL])
00165 #define RIGHT_INDEX(segptr)         (segptr->connection_index[RIGHT_VAL])
00166 
00167 /* Order fo visiting: Right, Up, Left, Down (think "Karel the Robot") */
00172 typedef enum visited_e
00173 {
00174     RightVisited    = (1<<0),
00175     UpVisited       = (1<<1),
00176     LeftVisited     = (1<<2),
00177     DownVisited     = (1<<3)
00178 } phblob_visited_enum;
00179 
00180 /* ------------------------------------------------------------------------ */
00185 typedef struct phblob_t 
00186 {
00187     int32_t     index;
00188     uint32_t    mass; /* number of pixels covered */
00189 
00190     /* first segment visited */
00191     /* last segment visited */
00192     /* These could be used as starting points for traversing the line
00193      * blob grouping in two different directions */
00194     phblob_line_segment   *first_segment;
00195     phblob_line_segment   *last_segment;
00196     int32_t                first_segment_index;
00197     int32_t                last_segment_index;
00198 
00199     uint32_t cx;
00200     uint32_t cy;
00201     uint32_t x1;
00202     uint32_t y1;
00203     uint32_t x2;
00204     uint32_t y2;
00205     uint32_t w;
00206     uint32_t h;
00207 
00208     struct phblob_t*    next;
00209     struct phblob_t*    previous;
00210     
00211     int32_t             next_index;
00212     int32_t             previous_index;
00213     
00214     int32_t         value           [phCOLOR_MAXELEMS];
00215     int32_t         value_low       [phCOLOR_MAXELEMS];
00216     int32_t         value_high      [phCOLOR_MAXELEMS];
00217 
00218 } phblob;
00219 
00220 /* ------------------------------------------------------------------------ */
00225 typedef struct phblob_data_t
00226 {
00227     uint32_t                    nlines;
00228     phblob_line_segment        *lines;
00229     uint32_t                    lines_size;
00230 
00231     uint32_t                    nrows;
00232     phblob_line_segment       **rows;
00233     uint32_t                    rows_size;
00234     int32_t                    *rowindex;
00235     uint32_t                    rowindex_size;    
00236 
00237     uint32_t                    nblobs;
00238     phblob                     *blobs;
00239     uint32_t                    blobs_size;
00240 
00241 #define phBLOB_COLOR (0)
00242 #define phBLOB_NEIGHBOR (1)
00243     int32_t             blob_method;
00244     
00245 } phblob_data;
00246 
00247 /* ------------------------------------------------------------------------ */
00252 typedef struct phblob_args_t
00253 {
00254     uint8_t           **imgptr;
00255     uint32_t            width;
00256     uint32_t            height;
00257     uint8_t             depth;
00258     uint32_t            format;
00259     
00260     uint32_t            color_count;
00261     phColor           **incolor;
00262     phColor           **threshold;
00263     volatile uint32_t  *nColors;
00264  
00265     int32_t             lower_threshold [phCOLOR_MAXELEMS];
00266     int32_t             upper_threshold [phCOLOR_MAXELEMS];
00267     int32_t             loop_threshold  [phCOLOR_MAXELEMS];
00268 
00269 } phblob_args;
00270 
00271 /* ---------------------------------------------------------------------- */
00272 /*
00273    phblob_new
00274 
00275    phblob_args_get / phblob_args_set
00276    image_settings
00277     imgptr
00278     width
00279     height
00280     depth
00281     format
00282    
00283    color_count
00284    incolor_ptr
00285    threshold_ptr
00286    nColors
00287 
00288    total_lines
00289    line_array
00290    line_array_size
00291 
00292    rowptr_array_ptr
00293    rowptr_array_size
00294 
00295    row_index_array_ptr
00296    row_index_array_size
00297 
00298    lower_threshold
00299    upper_threshold
00300    loop_threshold
00301  */
00302 /* --------------------------------------------------------------------- */
00307 int phblob_data_new  ( phblob_data **data );
00312 int phblob_data_init ( phblob_data *data  );
00317 int phblob_data_reset( phblob_data *data  );
00322 int phblob_data_free ( phblob_data **data );
00327 int phblob_data_copy ( phblob_data *in,
00328                        phblob_data *out   );
00329 
00330 /* --------------------------------------------------------------------- */
00335 int phblob_data_relink_line_ptrs( phblob_data *data );
00336 
00337 /* --------------------------------------------------------------------- */
00342 int phblob_find_line_segments_color     ( phblob_args *args,
00343                                           phblob_data *data );
00348 int phblob_find_line_segments_neighbor  ( phblob_args *args,
00349                                           phblob_data *data );
00354 int phblob_link_line_segments           ( phblob_args *args,
00355                                           phblob_data *data );
00356 
00357 /* ---------------------------------------------------------------------- */
00358 #define phBLOB_DRAW_ALL     (1<<0)
00359 #define phBLOB_CLEAR_IMG    (1<<3)
00360 /* #define phBLOB_DRAW_LARGEST */
00361 /* #define phBLOB_DRAW_SMALLEST */
00362 #define phBLOB_DRAW_INDEX   (1<<1)
00363 #define phBLOB_COLOR_BLACK  (1<<2)
00364 /* phBLOB_COLOR_BLACK uses black instead of the passed color */
00365 /* index goes unused unless phBLOB_DRAW_INDEX is specified */
00370 int phblob_draw_blob_rects( uint8_t        *imgptr,
00371                             uint32_t        width,
00372                             uint32_t        height,
00373                             uint32_t        format,
00374                             const phColor  *color,
00375                             phblob_data    *blobs,
00376                             uint32_t        index,
00377                             uint32_t        min_size,
00378                             uint32_t        flag );
00379 /* --------------------------------------------------------------------- */
00384 int phblob_color_lines(uint8_t         *imgptr,
00385                        uint32_t         width,
00386                        uint32_t         height,
00387                        uint32_t         format,
00388                        const phColor   *color,
00389                        phblob_data     *data,
00390                        uint32_t         blob_min_size,
00391                        uint32_t         index,
00392                        uint32_t         flag );
00393 
00394 /* --------------------------------------------------------------------- */
00399 int phblob_create_blobs(uint32_t        width,
00400                         uint32_t        height,
00401                         /* creates the blob list in the data object */
00402                         phblob_data    *data    );
00403                                
00404 
00405 /* --------------------------------------------------------------------- */
00410 int phblob_sort_blobs( phblob_data *data  /* returned */,
00411                        uint32_t    method /* 0: recursive; 1:iterative */
00412                        );
00413 
00414 /* ---------------------------------------------------------------------- */
00415 #ifdef __cplusplus
00416 } /* extern "C" */
00417 #endif
00418  /* Blobbing Functions */
00419 
00420 #endif /* _PHBLOB_H */




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:08 2007 for phission by  doxygen 1.4.4