00001 #include <stdlib.h>
00002
00003 #include "imageprocessing.h"
00004
00005
00006 int threshold_red=0;
00007 int threshold_green=0;
00008 int threshold_blue=0;
00009
00010
00011 int init_imageprocessing() {
00012
00013 char *green = getenv( "GREEN" );
00014 if ( green ) threshold_green= atoi(green);
00015 char *blue = getenv( "BLUE" );
00016 if ( blue ) threshold_blue= atoi(blue);
00017 char *red = getenv( "RED" );
00018 if ( red ) threshold_red= atoi(red);
00019 printf("red : %d , green %d, blue %d",threshold_red,threshold_green,threshold_blue);
00020
00021 return 0;
00022 }
00023
00024 Image *get_component (Image *inputImage , int color)
00025 {
00026 Image *resultImage;
00027 Matrix *mat;
00028 Image *temp;
00029
00031 if (color == RGB_RED)
00032 {
00033 Image *band2, *band3 ;
00034 band2 = duplicate_Image(inputImage);
00035 band3 = duplicate_Image(inputImage);
00036 band2 = extract_band(band2, 2);
00037 band3 = extract_band(band3, 3);
00038 inputImage = extract_band(inputImage, 1);
00039 inputImage = gray_multiply2(inputImage,2.0);
00040 inputImage = subtract_Image(inputImage, band2);
00041 inputImage = subtract_Image(inputImage, band3);
00042 }
00044
00045
00046 if (color == RGB_GREEN)
00047 {
00048 Image *band1, *band3 ;
00049 band1 = duplicate_Image(inputImage);
00050 band3 = duplicate_Image(inputImage);
00051 band1 = extract_band(band1, 1);
00052 band3 = extract_band(band3, 3);
00053 inputImage = extract_band(inputImage, 2);
00054 inputImage = gray_multiply2(inputImage,2.5);
00055 inputImage = subtract_Image(inputImage, band1);
00056 inputImage = subtract_Image(inputImage, band3);
00057 }
00058
00060
00061 if (color == RGB_BLUE)
00062 {
00063 Image *band1, *band2 ;
00064 band1 = duplicate_Image(inputImage);
00065 band2 = duplicate_Image(inputImage);
00066 band1 = extract_band(band1, 1);
00067 band2 = extract_band(band2, 2);
00068 inputImage = extract_band(inputImage, 3);
00069 inputImage = gray_multiply2(inputImage,2.0);
00070 inputImage = subtract_Image(inputImage , band1);
00071 inputImage = subtract_Image(inputImage , band2);
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 if (color == RGB_BLUE) {
00096 temp = gray_linear(inputImage ,threshold_blue,510.0,100.0 ,0.0,0, -1);
00097 }
00098 if (color == RGB_RED) {
00099 temp = gray_linear(inputImage ,threshold_red,510.0,100.0 ,0.0,0, -1);
00100 }
00101 if (color == RGB_GREEN) {
00102 temp = gray_linear(inputImage ,threshold_green,510.0,100.0 ,0.0,0, -1);
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 temp = remap_Image(temp ,CVIP_SHORT,0,100);
00119
00120
00121 mat=(Matrix *)get_default_filter(BLUR_SPATIAL,7,0);
00122 temp = (Image *)convolve_filter(temp,mat);
00123
00124
00125
00126
00127
00128 delete_Matrix(mat);
00129
00130
00131
00132 temp = gray_linear(temp ,50,255.0,255.0,0.0,0, -1);
00133
00134
00135 resultImage = remap_Image(temp ,CVIP_BYTE,0,255);
00136 delete_Image(temp);
00137
00138
00139
00140 return resultImage;
00141 }
00142
00143 Image *find_color_entities(Image *inputimage, int color ,
00144 ENTITYLIST *entitylist )
00145 {
00146 int row,col,angle,label,center_row,center_col,ret;
00147 long entityarea;
00148 int *ctr;
00149 Image *component_img;
00150 Image *labeled_img;
00151 Entity entity;
00152
00153
00154 if (color == RGB_BLUE && ! threshold_blue) {
00155 return inputimage;
00156 }
00157 if (color == RGB_RED && ! threshold_red) {
00158 return inputimage;
00159
00160 }
00161 if (color == RGB_GREEN && ! threshold_green) {
00162 return inputimage;
00163 }
00164
00165
00166
00167 component_img=get_component(inputimage, color);
00168
00169
00170 if ( !get_first_non_null_point(component_img,&row,&col) ) {
00171 printf("first label : EMPTY IMG ");
00172
00173 return component_img;
00174 }
00175 Image *temp = duplicate_Image(component_img);
00176 labeled_img = ::label(temp);
00177
00178
00179 entity.setColor((Entity::Color)color);
00180
00181
00182
00183
00184 label=1;
00185 row=col=0;
00186
00187
00188 while ( find_labeled_entity(labeled_img,row,col,label) ) {
00189
00190
00191
00192 entityarea=area(labeled_img,row,col);
00193 printf("area : %d",entityarea);
00194
00195
00196 if ( (SUBJECT_MINAREA < entityarea) && (entityarea <= GOAL_MAXAREA ) ) {
00197
00198 ctr=centroid(labeled_img,row,col);
00199 center_row=ctr[0];
00200 center_col=ctr[1];
00201
00202 entity.setX(center_col);
00203
00204 entity.setY(240-center_row);
00205 delete(ctr);
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 if ( (SUBJECT_MINAREA < entityarea) && (entityarea <= SUBJECT_MAXAREA ) ) {
00225
00226 entity.setType(Entity::SUBJECT);
00227
00228 entitylist->push_back(entity);
00229
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 label++;
00241 }
00242
00243
00244
00245
00246
00247
00248 delete_Image(labeled_img);
00249
00250 return component_img;
00251
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 bool get_first_non_null_point(Image *inputImage, int *row,int *column)
00264 {
00265 int r, c, rows ,cols;
00266 bool found=false;
00267 byte **image;
00268
00269
00270 rows = getNoOfRows_Image(inputImage);
00271 cols = getNoOfCols_Image(inputImage);
00272 image = (unsigned char ** )getData_Image(inputImage, 0);
00273 for(r=0; r < rows; r++)
00274 {
00275 for(c=0; c < cols; c++)
00276 {
00277 if (!found)
00278 {
00279 if ( image[r][c] != 0 )
00280 {
00281 *row=r;
00282 *column=c;
00283 found = true;
00284 }
00285 }
00286 }
00287 }
00288
00289 return found;
00290 }
00291
00292 bool find_labeled_entity(Image *inputImage, int &row,int &col, int &label) {
00293 int r, c, rows ,cols;
00294
00295
00296 int **image;
00297
00298
00299 rows = getNoOfRows_Image(inputImage);
00300 cols = getNoOfCols_Image(inputImage);
00301
00302
00303 image = (int ** )getData_Image(inputImage, 0);
00304
00305
00306 for(r=row; r < rows; r++)
00307 {
00308 for(c=0; c < cols; c++)
00309 {
00310
00311
00312
00313 if ( image[r][c] == label )
00314 {
00315 label=image[r][c];
00316 row=r;
00317 col=c;
00318 return true;
00319 }
00320 }
00321 }
00322 return false;
00323 }
00324
00325