cmvision.h
Go to the documentation of this file.
1 /*=========================================================================
2  CMVision.h
3  -------------------------------------------------------------------------
4  API definition for the CMVision real time Color Machine Vision library
5  -------------------------------------------------------------------------
6  Copyright 1999, 2000 #### ### ### ## ## ## #### ## ### ## ##
7  James R. Bruce ## ####### ## ## ## ## ## ## ## ######
8  School of Computer Science ## ## # ## ## ## ## ### ## ## ## ## ###
9  Carnegie Mellon University #### ## ## ### ## #### ## ### ## ##
10  -------------------------------------------------------------------------
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 2.1 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  =========================================================================*/
25 
26 #ifndef __CMVISION_H__
27 #define __CMVISION_H__
28 
29 // uncomment if your compiler supports the "restrict" keyword
30 // #define restrict __restrict__
31 #define restrict
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 
37 /*
38 Ultra-fast intro to processing steps:
39  - Color segmentation
40  - load / save
41  - set new values
42  - process frame
43  - Connected Regions
44  - RLE
45  - merge
46  - extract blobs
47  - separate blobs
48  - sort blobs
49  - merge blobs
50  - Blob merging (not currently in release)
51  - merge by area occupied
52 
53 Options File Format: (RGB merge name)
54 [Colors]
55 (00,00,00) 0.95 'Orange'
56 (00,00,00) 0.00 'Pink'
57 (00,00,00) 0.00 'Red'
58 (00,00,00) 0.00 'DarkBlue'
59 (00,00,00) 0.00 'Blue'
60 
61 [Thresholds]
62 (<lo>:<hi>,<lo>:<hi>,<lo>:<hi>)
63 (<lo>:<hi>,<lo>:<hi>,<lo>:<hi>)
64 (<lo>:<hi>,<lo>:<hi>,<lo>:<hi>)
65 */
66 
67 #define CMV_COLOR_LEVELS 256
68 #define CMV_MAX_COLORS 32
69 
70 // sets tweaked optimal values for image size
71 #define CMV_DEFAULT_WIDTH 320
72 #define CMV_DEFAULT_HEIGHT 240
73 
74 // values may need tweaked, although these seem to work usually
75 #define CMV_MAX_RUNS (CMV_DEFAULT_WIDTH * CMV_DEFAULT_HEIGHT) / 4
76 #define CMV_MAX_REGIONS CMV_MAX_RUNS / 4
77 #define CMV_MIN_AREA 20
78 
79 #define CMV_NONE ((unsigned)(-1))
80 
81 #ifndef NULL
82 #define NULL (0)
83 #endif
84 
85 struct lab{
86  unsigned char l,a,b;
87 };
88 
89 typedef struct lab image_pixel;
90 
91 #ifndef RGB_STRUCT
92 #define RGB_STRUCT
93 struct rgb{
94  unsigned char red,green,blue;
95 };
96 #endif
97 
98 // Options for level of processing
99 // use enable()/disable() to change
100 #define CMV_THRESHOLD 0x01
101 #define CMV_COLOR_AVERAGES 0x02
102 #define CMV_DUAL_THRESHOLD 0x04
103 #define CMV_DENSITY_MERGE 0x08
104 
105 #define CMV_VALID_OPTIONS 0x0F
106 
107 
108 class CMVision{
109 public:
110  struct region{
111  int color; // id of the color
112  int area; // occupied area in pixels
113  int x1,y1,x2,y2; // bounding box (x1,y1) - (x2,y2)
114  float cen_x,cen_y; // centroid
115  lab average; // average color (if CMV_COLOR_AVERAGES enabled)
116 
117  int sum_x,sum_y,sum_z; // temporaries for centroid and avg color
118  // int area_check; // DEBUG ONLY
119 
120  region *next; // next region in list
121 
122  // int number; // DEBUG ONLY
123  };
124 
125  struct rle{
126  unsigned color; // which color(s) this run represents
127  int length; // the length of the run (in pixels)
128  int parent; // run's parent in the connected components tree
129  };
130 
131  struct color_info{
132  rgb color; // example color (such as used in test output)
133  char *name; // color's meaninful name (e.g. ball, goal)
134  double merge; // merge density threshold
135  int expected_num; // expected number of regions (used for merge)
136  int a_low,a_high; // L,A,B component thresholds
138  };
139 
140  struct point{
141  double x,y;
142  };
143 
144  struct line{
146  };
147 
148  struct rectangle{
149  int x,y,w,h;
150  };
151 
152 protected:
155 
159 
161 
164  unsigned *map;
165 
166  unsigned options;
167 
168 protected:
169 // Private functions
170  void classifyFrame(image_pixel * restrict img,unsigned * restrict map);
171  int encodeRuns(rle * restrict out,unsigned * restrict map);
172  void connectComponents(rle * restrict map,int num);
173  int extractRegions(region * restrict reg,rle * restrict rmap,int num);
174  void calcAverageColors(region * restrict reg,int num_reg,
175  image_pixel * restrict img,
176  rle * restrict rmap,int num_runs);
177  int separateRegions(region * restrict reg,int num);
178  region *sortRegionListByArea(region * restrict list,int passes);
179  void sortRegions(int max_area);
180 
181  // density based merging support
182  int mergeRegions(region *p,int num,double density_thresh);
183  int mergeRegions();
184 
185  void clear();
186 
187 public:
190 
191  bool initialize(int nwidth,int nheight);
192  bool loadOptions(const char *filename);
193  bool saveOptions(char *filename);
194  bool enable(unsigned opt);
195  bool disable(unsigned opt);
196  void close();
197 
198  bool testClassify(rgb * restrict out,image_pixel * restrict image);
199  bool getThreshold(int color,
200  int &a_low,int &a_high,
201  int &b_low,int &b_high);
202  bool setThreshold(int color,
203  int a_low,int a_high,
204  int b_low,int b_high);
205 
206  unsigned *getMap()
207  {return(map);}
208 
209  char *getColorName(int color)
210  {return(colors[color].name);}
211  rgb getColorVisual(int color)
212  {return(colors[color].color);}
213 
215  {return(&colors[color]);}
216  void getColorInfo(int color,color_info &info)
217  {info = colors[color];}
218  void setColorInfo(int color,color_info &info)
219  {colors[color] = info;}
220 
221  bool processFrame(image_pixel *image);
222  bool processFrame(unsigned *map);
223  int numRegions(int color_id);
224  region *getRegions(int color_id);
225 };
226 
227 #endif
CMVision::rmap
rle rmap[CMV_MAX_RUNS]
Definition: cmvision.h:160
lab::a
unsigned char a
Definition: cmvision.h:86
CMVision::classifyFrame
void classifyFrame(image_pixel *restrict img, unsigned *restrict map)
Definition: cmvision.cc:105
CMVision::mergeRegions
int mergeRegions()
Definition: cmvision.cc:587
rgb::green
unsigned char green
Definition: cmvision.h:94
CMVision::line::a
point a
Definition: cmvision.h:145
CMVision::region::cen_x
float cen_x
Definition: cmvision.h:114
CMVision::region::sum_z
int sum_z
Definition: cmvision.h:117
lab::l
unsigned char l
Definition: cmvision.h:86
CMVision::saveOptions
bool saveOptions(char *filename)
Definition: cmvision.cc:776
CMVision::color_info::b_high
int b_high
Definition: cmvision.h:137
CMVision::getColorInfo
void getColorInfo(int color, color_info &info)
Definition: cmvision.h:216
CMVision::color_info::b_low
int b_low
Definition: cmvision.h:137
CMVision::region::average
lab average
Definition: cmvision.h:115
CMVision::initialize
bool initialize(int nwidth, int nheight)
Definition: cmvision.cc:621
CMVision::line::b
point b
Definition: cmvision.h:145
CMVision::~CMVision
~CMVision()
Definition: cmvision.h:189
rgb
Definition: cmvision.h:93
CMVision::setThreshold
bool setThreshold(int color, int a_low, int a_high, int b_low, int b_high)
Definition: cmvision.cc:880
CMVision::rectangle
Definition: cmvision.h:148
CMVision::color_info
Definition: cmvision.h:131
CMVision::rectangle::x
int x
Definition: cmvision.h:149
CMVision::region
Definition: cmvision.h:110
CMVision::connectComponents
void connectComponents(rle *restrict map, int num)
Definition: cmvision.cc:169
CMVision::a_class
unsigned a_class[CMV_COLOR_LEVELS]
Definition: cmvision.h:153
CMVision::getThreshold
bool getThreshold(int color, int &a_low, int &a_high, int &b_low, int &b_high)
Definition: cmvision.cc:865
rgb::red
unsigned char red
Definition: cmvision.h:94
CMVision::rle
Definition: cmvision.h:125
CMVision::enable
bool enable(unsigned opt)
Definition: cmvision.cc:810
CMVision::getColorVisual
rgb getColorVisual(int color)
Definition: cmvision.h:211
CMVision::close
void close()
Definition: cmvision.cc:830
CMVision::clear
void clear()
Definition: cmvision.cc:608
CMVision::region_table
region region_table[CMV_MAX_REGIONS]
Definition: cmvision.h:156
CMVision::calcAverageColors
void calcAverageColors(region *restrict reg, int num_reg, image_pixel *restrict img, rle *restrict rmap, int num_runs)
Definition: cmvision.cc:313
CMVision::region_count
int region_count[CMV_MAX_COLORS]
Definition: cmvision.h:158
CMVision::getColorName
char * getColorName(int color)
Definition: cmvision.h:209
CMVision::loadOptions
bool loadOptions(const char *filename)
Definition: cmvision.cc:666
CMVision::region::sum_y
int sum_y
Definition: cmvision.h:117
CMVision::separateRegions
int separateRegions(region *restrict reg, int num)
Definition: cmvision.cc:431
CMVision::line
Definition: cmvision.h:144
CMVision::region_list
region * region_list[CMV_MAX_COLORS]
Definition: cmvision.h:157
CMVision::rectangle::w
int w
Definition: cmvision.h:149
CMVision::region::next
region * next
Definition: cmvision.h:120
CMVision::encodeRuns
int encodeRuns(rle *restrict out, unsigned *restrict map)
Definition: cmvision.cc:124
CMVision::setColorInfo
void setColorInfo(int color, color_info &info)
Definition: cmvision.h:218
lab
Definition: cmvision.h:85
CMVision::sortRegionListByArea
region * sortRegionListByArea(region *restrict list, int passes)
Definition: cmvision.cc:474
CMVision::getMap
unsigned * getMap()
Definition: cmvision.h:206
CMVision::color_info::color
rgb color
Definition: cmvision.h:132
CMV_COLOR_LEVELS
#define CMV_COLOR_LEVELS
Definition: cmvision.h:67
CMVision::region::x1
int x1
Definition: cmvision.h:113
restrict
#define restrict
Definition: cmvision.h:31
CMVision::color_info::a_low
int a_low
Definition: cmvision.h:136
CMVision::sortRegions
void sortRegions(int max_area)
Definition: cmvision.cc:517
CMVision::height
int height
Definition: cmvision.h:163
CMVision::extractRegions
int extractRegions(region *restrict reg, rle *restrict rmap, int num)
Definition: cmvision.cc:246
CMVision::colors
color_info colors[CMV_MAX_COLORS]
Definition: cmvision.h:162
CMVision::color_info::name
char * name
Definition: cmvision.h:133
CMVision::getColorInfo
color_info * getColorInfo(int color)
Definition: cmvision.h:214
CMVision::region::x2
int x2
Definition: cmvision.h:113
CMVision::region::sum_x
int sum_x
Definition: cmvision.h:117
CMVision::region::y1
int y1
Definition: cmvision.h:113
CMV_MAX_RUNS
#define CMV_MAX_RUNS
Definition: cmvision.h:75
CMV_MAX_COLORS
#define CMV_MAX_COLORS
Definition: cmvision.h:68
CMVision::point::x
double x
Definition: cmvision.h:141
CMVision::color_info::expected_num
int expected_num
Definition: cmvision.h:135
CMVision::region::cen_y
float cen_y
Definition: cmvision.h:114
CMVision::processFrame
bool processFrame(image_pixel *image)
Definition: cmvision.cc:906
CMVision
Definition: cmvision.h:108
CMVision::getRegions
region * getRegions(int color_id)
Definition: cmvision.cc:970
CMVision::disable
bool disable(unsigned opt)
Definition: cmvision.cc:820
CMVision::CMVision
CMVision()
Definition: cmvision.h:188
CMVision::b_class
unsigned b_class[CMV_COLOR_LEVELS]
Definition: cmvision.h:154
CMVision::region::area
int area
Definition: cmvision.h:112
CMVision::rle::length
int length
Definition: cmvision.h:127
CMVision::width
int width
Definition: cmvision.h:163
CMVision::options
unsigned options
Definition: cmvision.h:166
CMVision::rle::parent
int parent
Definition: cmvision.h:128
CMV_MAX_REGIONS
#define CMV_MAX_REGIONS
Definition: cmvision.h:76
CMVision::region::color
int color
Definition: cmvision.h:111
CMVision::color_info::a_high
int a_high
Definition: cmvision.h:136
CMVision::map
unsigned * map
Definition: cmvision.h:164
CMVision::point::y
double y
Definition: cmvision.h:141
CMVision::color_info::merge
double merge
Definition: cmvision.h:134
CMVision::point
Definition: cmvision.h:140
CMVision::numRegions
int numRegions(int color_id)
Definition: cmvision.cc:964
CMVision::testClassify
bool testClassify(rgb *restrict out, image_pixel *restrict image)
Definition: cmvision.cc:839
rgb::blue
unsigned char blue
Definition: cmvision.h:94
CMVision::rle::color
unsigned color
Definition: cmvision.h:126
CMVision::rectangle::y
int y
Definition: cmvision.h:149
CMVision::region::y2
int y2
Definition: cmvision.h:113
lab::b
unsigned char b
Definition: cmvision.h:86
CMVision::rectangle::h
int h
Definition: cmvision.h:149


cmvision
Author(s): Nate Koenig, Nate Koenig
autogenerated on Wed Mar 2 2022 00:03:25