00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef SEGMENT_IMAGE
00010 #define SEGMENT_IMAGE
00011 #include <iostream>
00012
00013 #include <cstdlib>
00014 #include <image.h>
00015 #include <misc.h>
00016 #include <filter.h>
00017 #include <vector>
00018 #include "segment-graph.h"
00019 using std::vector;
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 rgb random_rgb(){
00034 rgb c;
00035 double r;
00036
00037 c.r = (uchar)random();
00038 c.g = (uchar)random();
00039 c.b = (uchar)random();
00040
00041 return c;
00042 }
00043
00044
00045
00046
00047 static inline float diff(image<float> *r, image<float> *g, image<float> *b,
00048 int x1, int y1, int x2, int y2) {
00049 return sqrt(square(imRef(r, x1, y1)-imRef(r, x2, y2)) +
00050 square(imRef(g, x1, y1)-imRef(g, x2, y2)) +
00051 square(imRef(b, x1, y1)-imRef(b, x2, y2)));
00052 }
00053
00054
00055
00056
00057
00059 double *** setArray3D (image<rgb> *image_input)
00060 {
00061
00062 double *** Array3D;
00063
00064 int n_set = image_input->width();
00065 int m_set = image_input->height();
00066 int t_set = 3;
00067
00068
00069 Array3D=(double***)malloc(sizeof(double**)*m_set);
00070
00071
00072
00073
00074 for (int n=0; n<m_set; n++)
00075 {
00076 Array3D[n] = (double**)malloc(sizeof(double*)*n_set);
00077 for (int m=0; m<n_set; m++)
00078 {
00079 Array3D[n][m] = (double*)malloc(sizeof(double)*t_set);
00080 }
00081
00082 }
00083
00084
00085
00086
00087
00088 for(int i=0;i<n_set;i++)
00089 {
00090
00091 for(int j=0;j<m_set;j++)
00092 {
00093
00094 Array3D[j][i][0]=imRef(image_input, i, j).r;
00095 Array3D[j][i][1]=imRef(image_input, i, j).g;
00096 Array3D[j][i][2]=imRef(image_input, i, j).b;
00097 }
00098
00099 }
00100
00101
00102
00103
00104 return (Array3D);
00105 }
00106
00108
00109 void BubbleSort(vector<double> *in_values, vector<int> *indices)
00110 {
00111
00112
00113 int swap_cond = 1;
00114 unsigned int j;
00115 unsigned int j_a;
00116 int swap_count;
00117 double value_1;
00118 double value_2;
00119 int vector_length = in_values->size();
00120 int index_1;
00121 int index_2;
00122
00123
00124 for (j_a=0;j_a<in_values->size();j_a++)
00125 {
00126
00127 indices->push_back(j_a);
00128 }
00129
00130
00131 while (swap_cond>0)
00132 {
00133 swap_count = 0;
00134 for (j=0;j<in_values->size()-1;j++)
00135 {
00136 value_1 = in_values->at(j);
00137 index_1 = indices->at(j);
00138 value_2 = in_values->at(j+1);
00139 index_2 = indices->at(j+1);
00140 if (value_2<value_1)
00141 {
00142 in_values->at(j) = value_2;
00143 in_values->at(j+1) = value_1;
00144 indices->at(j) = index_2;
00145 indices->at(j+1) = index_1;
00146 swap_count = 1;
00147 }
00148
00149 }
00150
00151 if (swap_count==0)
00152 {swap_cond = 0;}
00153 }
00154
00155 }
00156
00157 void ReArrange (vector<int> *label_values_sort,vector<int> *label_values, vector<int> *indices)
00158 {
00159 int index_new;
00160 int vector_length = indices->size();
00161 label_values_sort->assign (vector_length,0);
00162 int j_a;
00163 for (j_a=0;j_a<vector_length;j_a++)
00164 {
00165 index_new = indices->at(j_a);
00166
00167 label_values_sort->at(j_a) = label_values->at(index_new);
00168 }
00169
00170
00171 }
00173
00174 double FittingError(double ** x_val, double ** y_val, double ** z_val, double * param_cur, int * components_elements_number, int * list_of_labels, int num_labels_in_list)
00175 {
00176 int num_p;
00177 int current_label;
00178 int total_number_values=0;
00179 int j_current;
00180 double surf_out = 0;
00181
00182
00183
00184 for (j_current=0;j_current<num_labels_in_list;j_current++)
00185 {
00186
00187 current_label = list_of_labels[j_current];
00188 num_p = components_elements_number[current_label];
00189 for (int j=0;j<num_p; j++)
00190 {
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 surf_out = surf_out + pow((param_cur[0]*pow(x_val[current_label][j],2) + param_cur[1]*pow(y_val[current_label][j],2) + param_cur[2]*x_val[current_label][j] + param_cur[3]*y_val[current_label][j] + param_cur[4] - z_val[current_label][j]),2);
00202 }
00203
00204 total_number_values = total_number_values + num_p;
00205
00206 }
00207 surf_out = surf_out/((double)(total_number_values));
00208 return (surf_out);
00209 }
00210
00211 double FittedDepthValue(double x_val, double y_val, double z_val, double ** param_models,int label)
00212 {
00213
00214 double depth_current = 0;
00215
00216
00217
00218 depth_current = param_models[label][0]*pow(x_val,2) + param_models[label][1]*pow(y_val,2) + param_models[label][2]*x_val + param_models[label][3]*y_val+ param_models[label][4];
00219
00220 return (depth_current);
00221
00222 }
00223
00224
00225
00227
00228
00229
00230 void nelmin (int n, double * start, double * xmin,
00231 double * ynewlo, double reqmin, double * step, int konvge, int kcount,
00232 int *icount, int *numres, int *ifault,double ** x_val, double ** y_val, double ** z_val, int * number_of_elements_in_segment, int * current_label_list, int number_current_labels)
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 {
00327 double ccoeff = 0.5;
00328 double del;
00329 double dn;
00330 double dnn;
00331 double ecoeff = 2.0;
00332 double eps = 0.001;
00333 int i;
00334 int ihi;
00335 int ilo;
00336 int j;
00337 int jcount;
00338 int l;
00339 int nn;
00340 double *p;
00341 double *p2star;
00342 double *pbar;
00343 double *pstar;
00344 double rcoeff = 1.0;
00345 double rq;
00346 double x;
00347 double *y;
00348 double y2star;
00349 double ylo;
00350 double ystar;
00351 double z;
00352 double * start_init;
00353
00354
00355
00356 if ( reqmin <= 0.0 )
00357 {
00358 *ifault = 1;
00359 return;
00360 }
00361
00362 if ( n < 1 )
00363 {
00364 *ifault = 1;
00365 return;
00366 }
00367
00368 if ( konvge < 1 )
00369 {
00370 *ifault = 1;
00371 return;
00372 }
00373
00374 p = new double[n*(n+1)];
00375 pstar = new double[n];
00376 p2star = new double[n];
00377 pbar = new double[n];
00378 y = new double[n+1];
00379
00380
00381
00382
00383
00384
00385
00386
00387 *icount = 0;
00388 *numres = 0;
00389
00390 jcount = konvge;
00391 dn = ( double ) ( n );
00392 nn = n + 1;
00393 dnn = ( double ) ( nn );
00394 del = 1.0;
00395 rq = reqmin * dn;
00396
00397
00398
00399 for ( ; ; )
00400 {
00401 for ( i = 0; i < n; i++ )
00402 {
00403 p[i+n*n] = start[i];
00404 }
00405 y[n] = FittingError(x_val,y_val,z_val,start,number_of_elements_in_segment,current_label_list, number_current_labels);
00406 *icount = *icount + 1;
00407
00408 for ( j = 0; j < n; j++ )
00409 {
00410 x = start[j];
00411 start[j] = start[j] + step[j] * del;
00412 for ( i = 0; i < n; i++ )
00413 {
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 p[i+j*n] = start[i];
00425 }
00426 y[j] = FittingError(x_val,y_val,z_val,start,number_of_elements_in_segment,current_label_list, number_current_labels);
00427 *icount = *icount + 1;
00428 start[j] = x;
00429 }
00430
00431
00432
00433
00434
00435
00436 ylo = y[0];
00437 ilo = 0;
00438
00439 for ( i = 1; i < nn; i++ )
00440 {
00441 if ( y[i] < ylo )
00442 {
00443 ylo = y[i];
00444 ilo = i;
00445 }
00446 }
00447
00448
00449
00450 for ( ; ; )
00451 {
00452 if ( kcount <= *icount )
00453 {
00454 break;
00455 }
00456 *ynewlo = y[0];
00457 ihi = 0;
00458
00459 for ( i = 1; i < nn; i++ )
00460 {
00461 if ( *ynewlo < y[i] )
00462 {
00463 *ynewlo = y[i];
00464 ihi = i;
00465 }
00466 }
00467
00468
00469
00470
00471 for ( i = 0; i < n; i++ )
00472 {
00473 z = 0.0;
00474 for ( j = 0; j < nn; j++ )
00475 {
00476 z = z + p[i+j*n];
00477 }
00478 z = z - p[i+ihi*n];
00479 pbar[i] = z / dn;
00480 }
00481
00482
00483
00484 for ( i = 0; i < n; i++ )
00485 {
00486 pstar[i] = pbar[i] + rcoeff * ( pbar[i] - p[i+ihi*n] );
00487 }
00488 ystar = FittingError(x_val,y_val,z_val,pstar,number_of_elements_in_segment,current_label_list, number_current_labels);
00489 *icount = *icount + 1;
00490
00491
00492
00493 if ( ystar < ylo )
00494 {
00495 for ( i = 0; i < n; i++ )
00496 {
00497 p2star[i] = pbar[i] + ecoeff * ( pstar[i] - pbar[i] );
00498 }
00499 y2star = FittingError(x_val,y_val,z_val,p2star,number_of_elements_in_segment,current_label_list, number_current_labels);
00500 *icount = *icount + 1;
00501
00502
00503
00504 if ( ystar < y2star )
00505 {
00506 for ( i = 0; i < n; i++ )
00507 {
00508 p[i+ihi*n] = pstar[i];
00509 }
00510 y[ihi] = ystar;
00511 }
00512
00513
00514
00515 else
00516 {
00517 for ( i = 0; i < n; i++ )
00518 {
00519 p[i+ihi*n] = p2star[i];
00520 }
00521 y[ihi] = y2star;
00522 }
00523 }
00524
00525
00526
00527 else
00528 {
00529 l = 0;
00530 for ( i = 0; i < nn; i++ )
00531 {
00532 if ( ystar < y[i] )
00533 {
00534 l = l + 1;
00535 }
00536 }
00537
00538 if ( 1 < l )
00539 {
00540 for ( i = 0; i < n; i++ )
00541 {
00542 p[i+ihi*n] = pstar[i];
00543 }
00544 y[ihi] = ystar;
00545 }
00546
00547
00548
00549 else if ( l == 0 )
00550 {
00551 for ( i = 0; i < n; i++ )
00552 {
00553 p2star[i] = pbar[i] + ccoeff * ( p[i+ihi*n] - pbar[i] );
00554 }
00555 y2star = FittingError(x_val,y_val,z_val,p2star,number_of_elements_in_segment,current_label_list, number_current_labels);
00556 *icount = *icount + 1;
00557
00558
00559
00560 if ( y[ihi] < y2star )
00561 {
00562 for ( j = 0; j < nn; j++ )
00563 {
00564 for ( i = 0; i < n; i++ )
00565 {
00566 p[i+j*n] = ( p[i+j*n] + p[i+ilo*n] ) * 0.5;
00567 xmin[i] = p[i+j*n];
00568 }
00569 y[j] = FittingError(x_val,y_val,z_val,xmin,number_of_elements_in_segment,current_label_list, number_current_labels);
00570 *icount = *icount + 1;
00571 }
00572 ylo = y[0];
00573 ilo = 0;
00574
00575 for ( i = 1; i < nn; i++ )
00576 {
00577 if ( y[i] < ylo )
00578 {
00579 ylo = y[i];
00580 ilo = i;
00581 }
00582 }
00583 continue;
00584 }
00585
00586
00587
00588 else
00589 {
00590 for ( i = 0; i < n; i++ )
00591 {
00592 p[i+ihi*n] = p2star[i];
00593 }
00594 y[ihi] = y2star;
00595 }
00596 }
00597
00598
00599
00600 else if ( l == 1 )
00601 {
00602 for ( i = 0; i < n; i++ )
00603 {
00604 p2star[i] = pbar[i] + ccoeff * ( pstar[i] - pbar[i] );
00605 }
00606 y2star = FittingError(x_val,y_val,z_val,p2star,number_of_elements_in_segment,current_label_list, number_current_labels);
00607 *icount = *icount + 1;
00608
00609
00610
00611 if ( y2star <= ystar )
00612 {
00613 for ( i = 0; i < n; i++ )
00614 {
00615 p[i+ihi*n] = p2star[i];
00616 }
00617 y[ihi] = y2star;
00618 }
00619 else
00620 {
00621 for ( i = 0; i < n; i++ )
00622 {
00623 p[i+ihi*n] = pstar[i];
00624 }
00625 y[ihi] = ystar;
00626 }
00627 }
00628 }
00629
00630
00631
00632 if ( y[ihi] < ylo )
00633 {
00634 ylo = y[ihi];
00635 ilo = ihi;
00636 }
00637 jcount = jcount - 1;
00638
00639 if ( 0 < jcount )
00640 {
00641 continue;
00642 }
00643
00644
00645
00646 if ( *icount <= kcount )
00647 {
00648 jcount = konvge;
00649
00650 z = 0.0;
00651 for ( i = 0; i < nn; i++ )
00652 {
00653 z = z + y[i];
00654 }
00655 x = z / dnn;
00656
00657 z = 0.0;
00658 for ( i = 0; i < nn; i++ )
00659 {
00660 z = z + pow ( y[i] - x, 2 );
00661 }
00662
00663 if ( z <= rq )
00664 {
00665 break;
00666 }
00667 }
00668 }
00669
00670
00671
00672 for ( i = 0; i < n; i++ )
00673 {
00674 xmin[i] = p[i+ilo*n];
00675 }
00676 *ynewlo = y[ilo];
00677
00678 if ( kcount < *icount )
00679 {
00680 *ifault = 2;
00681 break;
00682 }
00683
00684 *ifault = 0;
00685
00686 for ( i = 0; i < n; i++ )
00687 {
00688 del = step[i] * eps;
00689 xmin[i] = xmin[i] + del;
00690 z = FittingError(x_val,y_val,z_val,xmin,number_of_elements_in_segment,current_label_list, number_current_labels);
00691 *icount = *icount + 1;
00692 if ( z < *ynewlo )
00693 {
00694 *ifault = 2;
00695 break;
00696 }
00697 xmin[i] = xmin[i] - del - del;
00698 z = FittingError(x_val,y_val,z_val,xmin,number_of_elements_in_segment,current_label_list, number_current_labels);
00699 *icount = *icount + 1;
00700 if ( z < *ynewlo )
00701 {
00702 *ifault = 2;
00703 break;
00704 }
00705 xmin[i] = xmin[i] + del;
00706 }
00707
00708 if ( *ifault == 0 )
00709 {
00710 break;
00711 }
00712
00713
00714
00715 for ( i = 0; i < n; i++ )
00716 {
00717 start[i] = xmin[i];
00718 }
00719 del = eps;
00720 *numres = *numres + 1;
00721 }
00722 delete [] p;
00723 delete [] pstar;
00724 delete [] p2star;
00725 delete [] pbar;
00726 delete [] y;
00727
00728 return;
00729 }
00730
00731
00732
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753 image<rgb> *segment_image(image<rgb> *im, image<rgb> *pc, float sigma, float c, int min_size,
00754 int *num_ccs, int *num_ccs_fin, int **& ClusterArray) {
00755 int width = im->width();
00756 int height = im->height();
00757
00758 image<float> *r = new image<float>(width, height);
00759 image<float> *g = new image<float>(width, height);
00760 image<float> *b = new image<float>(width, height);
00761
00762
00763 for (int y = 0; y < height; y++) {
00764 for (int x = 0; x < width; x++) {
00765 imRef(r, x, y) = imRef(im, x, y).r;
00766 imRef(g, x, y) = imRef(im, x, y).g;
00767 imRef(b, x, y) = imRef(im, x, y).b;
00768 }
00769 }
00770 image<float> *smooth_r = smooth(r, sigma);
00771 image<float> *smooth_g = smooth(g, sigma);
00772 image<float> *smooth_b = smooth(b, sigma);
00773 delete r;
00774 delete g;
00775 delete b;
00776
00777
00778 edge *edges = new edge[width*height*4];
00779 int num = 0;
00780 for (int y = 0; y < height; y++) {
00781 for (int x = 0; x < width; x++) {
00782 if (x < width-1) {
00783 edges[num].a = y * width + x;
00784 edges[num].b = y * width + (x+1);
00785 edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y);
00786 num++;
00787 }
00788
00789 if (y < height-1) {
00790 edges[num].a = y * width + x;
00791 edges[num].b = (y+1) * width + x;
00792 edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1);
00793 num++;
00794 }
00795
00796 if ((x < width-1) && (y < height-1)) {
00797 edges[num].a = y * width + x;
00798 edges[num].b = (y+1) * width + (x+1);
00799 edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1);
00800 num++;
00801 }
00802
00803 if ((x < width-1) && (y > 0)) {
00804 edges[num].a = y * width + x;
00805 edges[num].b = (y-1) * width + (x+1);
00806 edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1);
00807 num++;
00808 }
00809 }
00810 }
00811 delete smooth_r;
00812 delete smooth_g;
00813 delete smooth_b;
00814
00815
00816 universe *u = segment_graph(width*height, num, edges, c);
00817
00818
00819 for (int i = 0; i < num; i++) {
00820 int a = u->find(edges[i].a);
00821 int b = u->find(edges[i].b);
00822 if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
00823 u->join(a, b);
00824 }
00825 delete [] edges;
00826 *num_ccs = u->num_sets();
00827
00828
00830
00831
00832 ClusterArray=(int**)malloc(sizeof(int*)*height);
00833
00834 for (int k=0; k<height; k++){
00835 ClusterArray[k] = (int*)malloc(sizeof(int)*width);
00836 }
00837
00838 int * comp_label_list;
00839 comp_label_list = (int*)malloc(sizeof(int)*height*width);
00840
00841 for (int j = 0; j<(height*width); j++){
00842 comp_label_list[j] = 0;
00843 }
00844
00845 int j_count = 1;
00846 for (int y = 0; y < height; y++) {
00847 for (int x = 0; x < width; x++) {
00848 int comp = u->find(y * width + x);
00849 if (comp_label_list[comp]==0){
00850 comp_label_list[comp]=j_count;
00851 ClusterArray[y][x] = j_count;
00852
00853
00854 j_count = j_count +1;
00855 } else if (comp_label_list[comp]>0){
00856 ClusterArray[y][x] = comp_label_list[comp];
00857
00858 }
00859 }
00860 }
00861
00863
00864
00865
00866
00867
00868
00870
00871 double ** ComponentListX;
00872 double ** ComponentListY;
00873 double ** ComponentListZ;
00874
00875 ComponentListX = (double**)malloc(sizeof(double*)*j_count);
00876 ComponentListY = (double**)malloc(sizeof(double*)*j_count);
00877 ComponentListZ = (double**)malloc(sizeof(double*)*j_count);
00878
00879 for (int k=0; k<j_count; k++){
00880 ComponentListX[k] = (double*)malloc(sizeof(double));
00881 ComponentListY[k] = (double*)malloc(sizeof(double));
00882 ComponentListZ[k] = (double*)malloc(sizeof(double));
00883 }
00884
00885
00886 int * comp_size_list;
00887 double * comp_depth_list;
00888 comp_size_list = (int*)malloc(sizeof(int)*j_count);
00889 comp_depth_list = (double*)malloc(sizeof(double)*j_count);
00890 for (int j = 0; j<j_count; j++){
00891 comp_size_list[j] = 0;
00892 comp_depth_list[j] = 0;
00893 }
00894
00895
00896
00897
00898 int size_thres = 20;
00899 int label;
00900 int comp_size;
00901 double comp_depth;
00902 double depth_value;
00903 for (int y = 0; y < height; y++) {
00904 for (int x = 0; x < width; x++) {
00905 depth_value = (double)(imRef(pc, x, y).b);
00906 if (depth_value>0){
00907 label = ClusterArray[y][x];
00908
00909 comp_size = comp_size_list[label-1];
00910 comp_depth = comp_depth_list[label-1];
00911 ComponentListX[label-1] = (double*) realloc(ComponentListX[label-1],(comp_size+1)*sizeof(double));
00912 ComponentListX[label-1][comp_size]= (double)(imRef(pc, x, y).r);
00913
00914 ComponentListY[label-1] = (double*) realloc(ComponentListY[label-1],(comp_size+1)*sizeof(double));
00915 ComponentListY[label-1][comp_size]= (double)(imRef(pc, x, y).g);
00916
00917 ComponentListZ[label-1] = (double*) realloc(ComponentListZ[label-1],(comp_size+1)*sizeof(double));
00918 ComponentListZ[label-1][comp_size]= (double)(imRef(pc, x, y).b);
00919
00920 comp_size_list[label-1] = comp_size + 1;
00921 comp_depth_list[label-1] = comp_depth + (double)(imRef(pc, x, y).b);
00922 }
00923 }
00924 }
00925
00927
00928 double ** SurfaceModelsParameters;
00929 double ** SurfaceModelsParameters_new;
00930 ;
00931 SurfaceModelsParameters= (double**)malloc(sizeof(double*)*j_count);
00932 SurfaceModelsParameters_new= (double**)malloc(sizeof(double*)*j_count);
00933
00934
00935 for (int k=0; k<j_count; k++)
00936 {
00937 SurfaceModelsParameters[k] = (double*)malloc(sizeof(double)*5);
00938 SurfaceModelsParameters_new[k] = (double*)malloc(sizeof(double)*5);
00939
00940 }
00941
00942
00943 double * ErrorFit;
00944 ErrorFit = (double*)malloc(sizeof(double)*j_count);;
00945 double * ErrorFit_new;
00946 ErrorFit_new = (double*)malloc(sizeof(double)*j_count);;
00947
00948
00949 double mean_depth;
00950 int list_of_labels_a [1] = {0};
00951
00952
00953 double initial_parameters_quadric [5] = {0,0,-0.05,-0.05,0};
00954 double parameters_at_minimum [5] = {0,0,0,0,0};
00955 double min_fit_value [1] = {100};
00956 double required_min = 0.1;
00957 double initial_simplex_step [5] = {0.01,0.01,0.1,0.1,10};
00958 int konvergence_check = 100;
00959 int max_number_evaluations = 2000;
00960 int used_number_evaluations [1] = {0};
00961 int number_restarts [1] = {0};
00962 int error_detected [1] = {0};
00963 int parameter_quadric = 5;
00964
00965
00966 for (int label_j=0;label_j<j_count-1;label_j++){
00967 mean_depth = comp_depth_list[label_j]/((double)(comp_size_list[label_j]));
00968
00969 initial_parameters_quadric[0]=0;
00970 initial_parameters_quadric[1]=0;
00971 initial_parameters_quadric[2]=-0.05;
00972 initial_parameters_quadric[3]=-0.05;
00973 initial_parameters_quadric[4]=mean_depth-5;
00974
00975 list_of_labels_a[0] = label_j;
00976
00977
00978
00979
00980
00981
00982 if (comp_size_list[label_j]>size_thres){
00983 nelmin (parameter_quadric,initial_parameters_quadric, parameters_at_minimum, min_fit_value, required_min, initial_simplex_step, konvergence_check, max_number_evaluations, used_number_evaluations,number_restarts, error_detected,ComponentListX, ComponentListY, ComponentListZ, comp_size_list,list_of_labels_a,1);
00984
00985
00986
00987 for (int k_a=0; k_a<5; k_a++){
00988 SurfaceModelsParameters[label_j][k_a] = parameters_at_minimum[k_a];
00989 SurfaceModelsParameters_new[label_j][k_a] = parameters_at_minimum[k_a];
00990 }
00991 ErrorFit[label_j] = min_fit_value[0];
00992 ErrorFit_new[label_j] = min_fit_value[0];
00993 } else {
00994 for (int k_a=0; k_a<5; k_a++){
00995 SurfaceModelsParameters[label_j][k_a] = 0;
00996 SurfaceModelsParameters_new[label_j][k_a] = 0;
00997 }
00998 }
00999
01000 ErrorFit[label_j] = 100;
01001 ErrorFit_new[label_j] = 100;
01002 }
01003
01004
01005
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019 image<rgb> *fitted_depth = new image<rgb>(width, height);
01020
01021 double x_val;
01022 double y_val;
01023 double z_val;
01024
01025 int cluster_label;
01026 for (int y = 0; y < height; y++) {
01027 for (int x = 0; x < width; x++) {
01028 x_val = (double)(imRef(pc, x, y).r);
01029 y_val = (double)(imRef(pc, x, y).g);
01030 z_val = (double)(imRef(pc, x, y).b);
01031
01032
01033 cluster_label = ClusterArray[y][x]-1;
01034
01035
01036 imRef(fitted_depth, x, y).r = (uchar)(x_val);
01037 imRef(fitted_depth, x, y).g = (uchar)(y_val);
01038 imRef(fitted_depth, x, y).b = (uchar)(FittedDepthValue(x_val,y_val,z_val,SurfaceModelsParameters,cluster_label));
01039
01040 }
01041 }
01042
01044
01045
01047 int is_boundary;
01048
01049
01050
01051 int ** NeighborGraph;
01052
01053 int delta = 4;
01054 double depth;
01055 double depth_neighbor;
01056 double depth_dist;
01057 double depth_thres = 5;
01058 int label_neighbor;
01059 int k_1;
01060 int k_2;
01061 NeighborGraph=(int**)malloc(sizeof(int*)*j_count);
01062
01063 for (int k=0; k<j_count; k++)
01064 {
01065 NeighborGraph[k] = (int*)malloc(sizeof(int)*j_count);
01066
01067
01068 }
01069
01070
01071 for (k_1=0; k_1<j_count; k_1++)
01072 {
01073 for (k_2=0; k_2<j_count; k_2++)
01074
01075
01076 {
01077 NeighborGraph[k_1][k_2]=0;
01078 }
01079
01080 }
01081
01082
01083
01084 int y;
01085 int x;
01086 int j_y;
01087 int j_x;
01088 for (y = 0; y < height; y++) {
01089 for (x = 0; x < width; x++) {
01090 is_boundary = 0;
01091 label = ClusterArray[y][x]-1;
01092 if (comp_size_list[label]>size_thres){
01093 depth = (double)(imRef(fitted_depth, x, y).b);
01094
01095
01096 for (j_y = y-1; j_y<y+2; j_y++) {
01097 for (j_x = x-1; j_x<x+2; j_x++) {
01098 if ((j_y>=0)&&(j_x>=0)&&(j_x<width)&&(j_y<height)){
01099 label_neighbor = ClusterArray[j_y][j_x]-1;
01100
01101 if (label!=label_neighbor){
01102 is_boundary = 1;
01103 }
01104 }
01105 }
01106 }
01107 }
01108 if (is_boundary==1){
01109 for (j_y = y-delta; j_y< y+1+delta; j_y++) {
01110 for (j_x = x-delta; j_x < x+1+delta; j_x++) {
01111 if ((j_y>=0)&&(j_x>=0)&&(j_x<width)&&(j_y<height)){
01112
01113 label_neighbor = ClusterArray[j_y][j_x]-1;
01114 if (comp_size_list[label_neighbor]>size_thres){
01115
01116
01117 depth_neighbor = (double)(imRef(fitted_depth, j_x, j_y).b);
01118 depth_dist = abs(depth - depth_neighbor);
01119 if ((label!=label_neighbor)&&(depth_dist<depth_thres)){
01120 NeighborGraph[label][label_neighbor]=1;
01121 NeighborGraph[label_neighbor][label]=1;
01122
01123
01124
01125 }
01126 }
01127 }
01128 }
01129 }
01130 }
01131 }
01132 }
01133
01134
01135
01137 vector<double> graph_links;
01138 vector<int> graph_label_1;
01139 vector<int> graph_label_2;
01140 double e_fit_12;
01141 double e_fit_21;
01142 double e_fit_min;
01143 double param_fit[5] = {0,0,0,0,0};
01144 int p_c;
01145 int label_dummy;
01146
01147 for (k_1=0; k_1<j_count; k_1++) {
01148 for (k_2=0; k_2<j_count; k_2++){
01149 if (k_1<k_2){
01150 if (NeighborGraph[k_1][k_2]==1){
01151
01152 for (p_c=0;p_c<5;p_c++){
01153 param_fit[p_c]= SurfaceModelsParameters[k_1][p_c];
01154 }
01155
01156 list_of_labels_a[0] = k_2;
01157
01158 e_fit_12 = FittingError(ComponentListX, ComponentListY, ComponentListZ,SurfaceModelsParameters[k_1],comp_size_list,list_of_labels_a,1);
01159 list_of_labels_a[0] =k_1;
01160 e_fit_21 = FittingError(ComponentListX, ComponentListY, ComponentListZ,SurfaceModelsParameters[k_2],comp_size_list,list_of_labels_a,1);
01161
01162
01163 if (e_fit_12<e_fit_21){
01164 e_fit_min = e_fit_12;
01165 } else {
01166 e_fit_min = e_fit_21;
01167 }
01168
01169 graph_links.push_back (e_fit_min);
01170 graph_label_1.push_back (k_1);
01171 graph_label_2.push_back (k_2);
01172 }
01173 }
01174 }
01175 }
01176
01177
01179
01180
01181
01182 vector<int> graph_index;
01183 vector<int> graph_label_1_sort;
01184 vector<int> graph_label_2_sort;
01185
01186 BubbleSort(&graph_links,&graph_index);
01187 ReArrange(&graph_label_1_sort,&graph_label_1,&graph_index);
01188 ReArrange(&graph_label_2_sort,&graph_label_2,&graph_index);
01189
01190
01191
01192
01193
01194
01195
01196 int * labels_new;
01197 labels_new = (int*)malloc(sizeof(int)*j_count);
01198
01199 int * comp_size_list_new;
01200 comp_size_list_new = (int*)malloc(sizeof(int)*j_count);
01201
01202 double * comp_depth_list_new;
01203 comp_depth_list_new = (double*)malloc(sizeof(double)*j_count);
01204
01205
01206 for (int label_j=0;label_j<j_count-1;label_j++)
01207 {labels_new[label_j]=label_j;
01208 comp_size_list_new[label_j]=comp_size_list[label_j];
01209 comp_depth_list_new[label_j]=comp_depth_list[label_j];
01210 }
01211
01212 int ** label_list;
01213 label_list = (int**)malloc(sizeof(int*)*j_count);
01214
01215 int * number_of_labels_in_list;
01216 number_of_labels_in_list = (int*)malloc(sizeof(int)*j_count);
01217 for (int k_b=0; k_b<j_count; k_b++){
01218 label_list[k_b] = (int*)malloc(sizeof(int));
01219 }
01220
01221 for (int label_j=0;label_j<j_count-1;label_j++){
01222 label_list[label_j][0]=label_j;
01223 number_of_labels_in_list[label_j] = 1;
01224 }
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235 int u_1;
01236 int u_2;
01237 int label_1;
01238 int label_2;
01239 int error_1;
01240 int error_2;
01241 int size_1;
01242 int size_2;
01243 int label_1_total_size;
01244 int label_2_total_size;
01245 double thres_merge = 15;
01246 int number_merged_labels;
01247 double min_error;
01248 int number_of_links = graph_links.size();
01249 int label_keep;
01250 int label_replace;
01251
01252
01253 for (int link_index=0;link_index<number_of_links;link_index++)
01254 {
01255 u_1 = graph_label_1_sort.at(link_index);
01256 u_2 = graph_label_2_sort.at(link_index);
01257
01258 label_1=labels_new[u_1];
01259 label_2=labels_new[u_2];
01260
01261 error_1 = ErrorFit_new[label_1];
01262 error_2 = ErrorFit_new[label_2];
01263
01264 size_1 = comp_size_list_new[label_1];
01265 size_2 = comp_size_list_new[label_2];
01266
01267
01268
01269
01270
01271
01272 if ((size_1>size_thres)&&(size_2>size_thres)&&(label_1!=label_2))
01273 {
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01298 e_fit_12 = FittingError(ComponentListX, ComponentListY, ComponentListZ,SurfaceModelsParameters_new[label_1],comp_size_list,label_list[label_2],number_of_labels_in_list[label_2]);
01299 e_fit_21 =FittingError(ComponentListX,ComponentListY,ComponentListZ,SurfaceModelsParameters_new[label_2],comp_size_list,label_list[label_1],number_of_labels_in_list[label_1]);
01300
01301
01302
01303
01304
01305
01306 min_error = e_fit_12;
01307 if (e_fit_21<e_fit_12)
01308 {min_error = e_fit_21;}
01309
01310
01311
01312
01313
01314 if (min_error<thres_merge)
01315 {
01316
01317
01318 number_merged_labels = number_of_labels_in_list[label_1]+number_of_labels_in_list[label_2];
01319 int * merged_labels = (int*)malloc(sizeof(int)*number_merged_labels);
01320
01321 for (int j_merge_1=0;j_merge_1<number_of_labels_in_list[label_1];j_merge_1++)
01322 {
01323 merged_labels[j_merge_1]= label_list[label_1][j_merge_1];
01324 }
01325
01326 for (int j_merge_2=0;j_merge_2<number_of_labels_in_list[label_2];j_merge_2++)
01327 {
01328 merged_labels[j_merge_2+number_of_labels_in_list[label_1]]= label_list[label_2][j_merge_2];
01329 }
01330
01331
01332
01333
01335
01336
01337 mean_depth = (comp_depth_list_new[label_1] + comp_depth_list_new[label_2])/((double)(comp_size_list_new[label_1]+comp_size_list_new[label_2]));
01338
01339 initial_parameters_quadric[0]=0;
01340 initial_parameters_quadric[1]=0;
01341 initial_parameters_quadric[2]=-0.05;
01342 initial_parameters_quadric[3]=-0.05;
01343 initial_parameters_quadric[4]=mean_depth-5;
01344
01345
01346 nelmin (parameter_quadric,initial_parameters_quadric, parameters_at_minimum,
01347 min_fit_value, required_min, initial_simplex_step, konvergence_check, max_number_evaluations,
01348 used_number_evaluations,number_restarts, error_detected,ComponentListX, ComponentListY, ComponentListZ,comp_size_list,merged_labels,number_merged_labels);
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359 if (min_fit_value[0]<10)
01360 {
01361
01362
01363 comp_depth_list_new[label_1] = comp_depth_list_new[label_1] + comp_depth_list_new[label_2];
01364 comp_depth_list_new[label_2] = comp_depth_list_new[label_1] + comp_depth_list_new[label_2];
01365 comp_size_list_new[label_1] = comp_size_list_new[label_1]+comp_size_list_new[label_2];
01366 comp_size_list_new[label_2] = comp_size_list_new[label_1]+comp_size_list_new[label_2];
01367 ErrorFit_new[label_1]=min_fit_value[0];
01368 ErrorFit_new[label_2]=min_fit_value[0];
01369 number_of_labels_in_list[label_1]=number_merged_labels;
01370 number_of_labels_in_list[label_2]=number_merged_labels;
01371
01372 for (int w_1=0;w_1<5;w_1++)
01373 {
01374 SurfaceModelsParameters_new[label_1][w_1] = parameters_at_minimum[w_1];
01375 SurfaceModelsParameters_new[label_2][w_1] = parameters_at_minimum[w_1];
01376 }
01377
01378 if (label_1<label_2)
01379 {
01380 label_replace = label_2;
01381 label_keep = label_1;
01382 }
01383 else
01384 {
01385 label_replace = label_1;
01386 label_keep = label_2;
01387 }
01388
01389
01390 for (int w_2=0;w_2<j_count-1;w_2++)
01391 {
01392 if (labels_new[w_2]==label_replace)
01393 {
01394 labels_new[w_2]=label_keep;
01395 }
01396 }
01397
01398
01399 label_list[label_keep] = (int*) realloc (label_list[label_keep], number_merged_labels * sizeof(int));
01400 for (int j_merge=0;j_merge<number_merged_labels;j_merge++)
01401 {
01402 label_list[label_keep][j_merge]=merged_labels[j_merge];
01403 }
01404
01405 }
01406
01407
01408 free (merged_labels);
01409
01410 }
01411
01412
01413 }
01414
01415
01416 }
01417
01418
01420 image<rgb> *output = new image<rgb>(width, height);
01421
01422
01423 rgb *colors = new rgb[width*height];
01424 for (int i = 0; i < width*height; i++)
01425 colors[i] = random_rgb();
01426
01427
01428
01429 int * comp_label_list_fin;
01430 comp_label_list_fin = (int*)malloc(sizeof(int)*height*width);
01431
01432
01433 for (int j = 0; j<(height*width); j++)
01434 {comp_label_list_fin[j] = 0;
01435 }
01436
01437 int comp;
01438 int j_count_fin = 1;
01439 int comp_fin;
01440 for (int y = 0; y < height; y++) {
01441 for (int x = 0; x < width; x++) {
01443 comp = ClusterArray[y][x]-1;
01444
01445 comp_fin = labels_new[comp];
01446 if (comp_size_list_new[comp_fin]>size_thres){
01447 if (comp_label_list_fin[comp_fin]==0){
01448 comp_label_list_fin[comp_fin]=j_count_fin;
01449 ClusterArray[y][x] = j_count_fin;
01450 j_count_fin = j_count_fin +1;
01451 }else{
01452 ClusterArray[y][x] = comp_label_list_fin[comp_fin];
01453 }
01454 imRef(output, x, y) =colors[comp_fin];
01455
01456 imRef(output, x, y) =colors[comp_label_list_fin[comp_fin]];
01457 imRef(output, x, y).r = (uchar) ClusterArray[y][x];
01458 }
01459 else
01460 {ClusterArray[y][x] = 0;
01461 imRef(output, x, y) = colors[0];
01462 imRef(output, x, y).r = (uchar) 0;
01463 }
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496 }
01497 }
01498
01499
01500
01501
01502 *num_ccs_fin = j_count_fin;
01503
01505 delete [] colors;
01506 delete u;
01507 delete fitted_depth;
01508
01509 free(comp_size_list);
01510 free(comp_depth_list);
01511 free(comp_depth_list_new);
01512 free(comp_label_list);
01513 free(comp_size_list_new);
01514 free(labels_new);
01515 free(comp_label_list_fin);
01516 free(ErrorFit);
01517 free(ErrorFit_new);
01518
01519 for (int k=0; k<j_count; k++)
01520 {
01521 free(SurfaceModelsParameters[k]);
01522 free(SurfaceModelsParameters_new[k]);
01523 free(label_list[k]);
01524 free(NeighborGraph[k]);
01525 free(ComponentListX[k]);
01526 free(ComponentListY[k]);
01527 free(ComponentListZ[k]);
01528 }
01529
01530 free(SurfaceModelsParameters_new);
01531 free(SurfaceModelsParameters);
01532 free(label_list);
01533 free(NeighborGraph);
01534 free(ComponentListX);
01535 free(ComponentListY);
01536 free(ComponentListZ);
01537
01538
01539
01540
01541
01542
01543 return output;
01544 }
01545
01546 #endif