summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/svm/src
diff options
context:
space:
mode:
Diffstat (limited to 'SD-VBS/benchmarks/svm/src')
0 files changed, 0 insertions, 0 deletions
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714









































































































































































































































































































































































































































































































































































































































































































































                                                                                                   
/********************************
Author: Sravanthi Kota Venkata
********************************/

/***
%   MSER  Maximally Stable Extremal Regions
%   R=MSER(I,DELTA) computes the Maximally Stable Extremal Regions
%   (MSER) of image I with stability threshold DELTA. I is any
%   array of class UINT8, while DELTA is a scalar of the same class.
%   R is an index set (of class UINT32) which enumerates the
%   representative pixels of the detected regions.
%
%   A region R can be recovered from a representative pixel X as the
%   connected component of the level set {Y:I(Y) <= I(X)} which
%   contains X.
***/


#include "mser.h"
#include <string.h>

/* advance N-dimensional subscript */
void
adv(iArray *dims, int ndims, iArray *subs_pt)
{
    int d = 0 ;
    while(d < ndims) 
    {
        sref(subs_pt,d) = sref(subs_pt,d) + 1;
        if( sref(subs_pt,d)  < sref(dims,d) ) 
            return ;
        sref(subs_pt,d++) = 0 ;
    }
}

/** driver **/
I2D* mser(I2D* I, int in_delta,
          iArray* subs_pt, iArray* nsubs_pt, iArray* strides_pt, iArray* visited_pt, iArray* dims, 
	  uiArray* joins_pt,
	  region_t* regions_pt, 
	  pair_t* pairs_pt,
	  node_t* forest_pt,
	  ulliArray* acc_pt, ulliArray* ell_pt,
	  I2D* out)
{
   idx_t i, rindex=0;
   int k;
   int nout = 1;
   
   int OUT_REGIONS=0;
   int OUT_ELL = 1;
   int OUT_PARENTS = 2;
   int OUT_AREA = 3;
   int BUCKETS = 256;
   
   //I2D* out;

   int IN_I = 0;
   int IN_DELTA = 1;   

    /* configuration */
    int verbose = 1 ;      /* be verbose                              */
    int small_cleanup= 1 ; /* remove very small regions               */
    int big_cleanup  = 1 ; /* remove very big regions                 */
    int bad_cleanup  = 0 ; /* remove very bad regions                 */
    int dup_cleanup  = 1 ; /* remove duplicates                       */
    val_t delta ;          /* stability delta                         */

    /* node value denoting a void node */
    idx_t const node_is_void = 0xffffffff ;

    //iArray*   subs_pt ;       /* N-dimensional subscript                 
    //iArray*   nsubs_pt ;      /* diff-subscript to point to neigh.       
//    uiArray* strides_pt ;    /* strides to move in image array          
//    uiArray* visited_pt ;    /* flag                                    

    int nel ;              /* number of image elements (pixels)       */
    int ner = 0 ;          /* number of extremal regions              */
    int nmer = 0 ;         /* number of maximally stable              */
    int ndims ;            /* number of dimensions                    */
//    iArray* dims ;           /* dimensions                              
    int njoins = 0 ;       /* number of join ops                      */

    I2D* I_pt ;    /* source image                            */
    //pair_t*   pairs_pt ;   /* scratch buffer to sort pixels           
//    node_t*   forest_pt ;  /* the extremal regions forest             
//    region_t* regions_pt ; /* list of extremal regions found          
    int regions_pt_size;
    int pairs_pt_size;
    int forest_pt_size;

    /* ellipses fitting */
    //ulliArray* acc_pt ;        /* accumulator to integrate region moments
    //ulliArray* ell_pt ;        /* ellipses parameters                     
    int    gdl ;           /* number of parameters of an ellipse      */
    //uiArray* joins_pt ;      /* sequence of joins                       
   
    delta = 0;
    delta = in_delta;

    /* get dimensions */

    nel = I->height*I->width;            /* number of elements of src image */
    ndims = 2;
    //dims = malloc(sizeof(iArray) + sizeof(int)*ndims);
    I_pt = I;
    
    sref(dims,0) = I->height;
    sref(dims,1) = I->width;
    
    /* allocate stuff */
    //subs_pt = malloc(sizeof(iArray) + sizeof(int)*ndims);
    //nsubs_pt = malloc(sizeof(iArray) + sizeof(int)*ndims);

    //strides_pt = malloc(sizeof(uiArray)+sizeof(unsigned int)*ndims);
    //visited_pt = malloc(sizeof(uiArray) + sizeof(unsigned int)*nel);
    //joins_pt = malloc(sizeof(uiArray) + sizeof(unsigned int)*nel);

    //regions_pt = (region_t*)malloc(sizeof(region_t)*nel);
    regions_pt_size = nel;

    //pairs_pt = (pair_t*)malloc(sizeof(pair_t)*nel);
    pairs_pt_size = nel;

    //forest_pt = (node_t*)malloc(sizeof(node_t)*nel);
    forest_pt_size = nel;

    /* compute strides to move into the N-dimensional image array */
    sref(strides_pt,0) = 1;
    for(k = 1 ; k < ndims ; ++k)
    {
        sref(strides_pt,k) = sref(strides_pt,k-1) * sref(dims,k-1) ;
    }

    /* sort pixels in increasing order of intensity: using Bucket Sort */
    {
        int unsigned buckets [BUCKETS] ;
        memset(buckets, 0, sizeof(int unsigned)*BUCKETS) ;

        for(i = 0 ; i < nel ; ++i) 
        {
            val_t v = asubsref(I_pt,i) ;
            ++buckets[v] ;
        }
        
        for(i = 1 ; i < BUCKETS ; ++i) 
        {
            arrayref(buckets,i) += arrayref(buckets,i-1) ;
        }
        
        for(i = nel ; i >= 1 ; )
        {
            val_t v = asubsref(I_pt,--i) ;
            idx_t j = --buckets[v] ;
            pairs_pt[j].value = v ;
            pairs_pt[j].index = i ;
        }
    }
     
    /* initialize the forest with all void nodes */
    for(i = 0 ; i < nel ; ++i) 
    {
        forest_pt[i].parent = node_is_void  ;
    }
  
    /* number of ellipse free parameters */
    gdl = ndims*(ndims+1)/2 + ndims ;

    /* -----------------------------------------------------------------
    *                                     Compute extremal regions tree
    * -------------------------------------------------------------- */
  
    for(i = 0 ; i < nel ; ++i)
    {
        /* pop next node xi */
        idx_t index = pairs_pt [i].index ;    
        val_t value = pairs_pt [i].value ;

        /* this will be needed later */
        rindex = index ;
    
        /* push it into the tree */
        forest_pt [index] .parent   = index ;
        forest_pt [index] .shortcut = index ;
        forest_pt [index] .area     = 1 ;
#ifdef USE_RANK_UNION
        forest_pt [index] .height   = 1 ;
#endif
    
    /* convert index into a subscript sub; also initialize nsubs 
    to (-1,-1,...,-1) */
        {
            idx_t temp = index ;
            for(k = ndims-1 ; k >=0 ; --k) 
            {
                sref(nsubs_pt,k) = -1 ;
                sref(subs_pt,k) = temp / sref(strides_pt,k) ;
                temp         = temp % sref(strides_pt,k) ;
            }
        }
    
    /* process neighbors of xi */
        while(1) 
        {
            int good = 1 ;
            idx_t nindex = 0 ;
      
      /* compute NSUBS+SUB, the correspoinding neighbor index NINDEX
         and check that the pixel is within image boundaries. */
            for(k = 0 ; k < ndims && good ; ++k) 
            {
                int temp = sref(nsubs_pt,k) + sref(subs_pt,k) ;
                good &= 0 <= temp && temp < sref(dims,k) ;
                nindex += temp * sref(strides_pt,k) ;
            }      
      

      /* keep going only if
         1 - the neighbor is within image boundaries;
         2 - the neighbor is indeed different from the current node
             (this happens when nsub=(0,0,...,0));
         3 - the nieghbor is already in the tree, meaning that
             is a pixel older than xi.
      */
            if(good && nindex != index && forest_pt[nindex].parent != node_is_void ) 
            {
                idx_t nrindex = 0, nvisited ;
                val_t nrvalue = 0 ;
                

#ifdef USE_RANK_UNION
                int height  = forest_pt [ rindex] .height ;
                int nheight = forest_pt [nrindex] .height ;
#endif
        
        /* RINDEX = ROOT(INDEX) might change as we merge trees, so we
           need to update it after each merge */
        
        /* find the root of the current node */
        /* also update the shortcuts */
                nvisited = 0 ;
                while( forest_pt[rindex].shortcut != rindex ) 
                {          
                    sref(visited_pt,nvisited++) = rindex ;
                    rindex = forest_pt[rindex].shortcut ;
                }      
                while( nvisited-- ) 
                {
                    forest_pt [ sref(visited_pt,nvisited) ] .shortcut = rindex ;
                }
        
        /* find the root of the neighbor */
                nrindex  = nindex ;
                nvisited = 0 ;
                while( forest_pt[nrindex].shortcut != nrindex ) 
                {          
                    sref(visited_pt, nvisited++) = nrindex ;
                    nrindex = forest_pt[nrindex].shortcut ;
                }      
                while( nvisited-- ) 
                {
                    forest_pt [ sref(visited_pt,nvisited) ] .shortcut = nrindex ;
                }
        
        /*
          Now we join the two subtrees rooted at
          
            RINDEX = ROOT(INDEX) and NRINDEX = ROOT(NINDEX).
          
          Only three things can happen:
          
          a - ROOT(INDEX) == ROOT(NRINDEX). In this case the two trees
              have already been joined and we do not do anything.
          
          b - I(ROOT(INDEX)) == I(ROOT(NRINDEX)). In this case index
              is extending an extremal region with the same
              value. Since ROOT(NRINDEX) will NOT be an extremal
              region of the full image, ROOT(INDEX) can be safely
              addedd as children of ROOT(NRINDEX) if this reduces
              the height according to union rank.
               
          c - I(ROOT(INDEX)) > I(ROOT(NRINDEX)) as index is extending
              an extremal region, but increasing its level. In this
              case ROOT(NRINDEX) WILL be an extremal region of the
              final image and the only possibility is to add
              ROOT(NRINDEX) as children of ROOT(INDEX).
        */

                if( rindex != nrindex ) 
                {
                    /* this is a genuine join */
          
                    nrvalue = asubsref(I_pt,nrindex) ;
                    if( nrvalue == value 
#ifdef USE_RANK_UNION             
              && height < nheight
#endif
                    ) 
                    {
                        /* ROOT(INDEX) becomes the child */
                        forest_pt[rindex] .parent   = nrindex ;
                        forest_pt[rindex] .shortcut = nrindex ;          
                        forest_pt[nrindex].area    += forest_pt[rindex].area ;

#ifdef USE_RANK_UNION
                        forest_pt[nrindex].height = MAX(nheight, height+1) ;
#endif

                        sref(joins_pt,njoins++) = rindex ;

                    } 
                    else 
                    {
                        /* ROOT(index) becomes parent */
                        forest_pt[nrindex] .parent   = rindex ;
                        forest_pt[nrindex] .shortcut = rindex ;
                        forest_pt[rindex]  .area    += forest_pt[nrindex].area ;

#ifdef USE_RANK_UNION
                        forest_pt[rindex].height = MAX(height, nheight+1) ;
#endif
                        if( nrvalue != value ) 
                        {            
                            /* nrindex is extremal region: save for later */
                            forest_pt[nrindex].region = ner ;
                            regions_pt [ner] .index      = nrindex ;
                            regions_pt [ner] .parent     = ner ;
                            regions_pt [ner] .value      = nrvalue ;
                            regions_pt [ner] .area       = forest_pt [nrindex].area ;
                            regions_pt [ner] .area_top   = nel ;
                            regions_pt [ner] .area_bot   = 0 ;            
                            ++ner ;
                        }