Beispiel #1
0
/*----------------------------------------------------------------------- */
SEXP
watershed (SEXP x, SEXP _tolerance, SEXP _ext) {
    SEXP res;
    int im, i, j, nx, ny, nz, ext, nprotect = 0;
    double tolerance;

    nx = INTEGER ( GET_DIM(x) )[0];
    ny = INTEGER ( GET_DIM(x) )[1];
    nz = getNumberOfFrames(x,0);
    tolerance = REAL( _tolerance )[0];
    ext = INTEGER( _ext )[0];

    PROTECT ( res = Rf_duplicate(x) );
    nprotect++;
  
    int * index = new int[ nx * ny ];

    for ( im = 0; im < nz; im++ ) {

        double * src = &( REAL(x)[ im * nx * ny ] );
        double * tgt = &( REAL(res)[ im * nx * ny ] );

        /* generate pixel index and negate the image -- filling wells */
        for ( i = 0; i < nx * ny; i++ ) {
	  tgt[ i ] = -src[ i ];
	  index[ i ] = i;
        }
        /* from R includes R_ext/Utils.h */
        /* will resort tgt as well */
        rsort_with_index( tgt, index, nx * ny );
        /* reassign tgt as it was reset above but keep new index */
        for ( i = 0; i < nx * ny; i++ )
            tgt[ i ] = -src[ i ];

        SeedList seeds;  /* indexes of all seed starting points, i.e. lowest values */

        IntList  equals; /* queue of all pixels on the same gray level */
        IntList  nb;     /* seed values of assigned neighbours */
        int ind, indxy, nbseed, x, y, topseed = 0;
        IntList::iterator it;
        TheSeed newseed;
        PointXY pt;
        bool isin;
        /* loop through the sorted index */
        for ( i = 0; i < nx * ny && src[ index[i] ] > BG; ) {
            /* pool a queue of equally lowest values */
            ind = index[ i ];
            equals.push_back( ind );
            for ( i = i + 1; i < nx * ny; ) {
                if ( src[ index[i] ] != src[ ind ] ) break;
                equals.push_back( index[i] );
                i++;
            }
            while ( !equals.empty() ) {
                /* first check through all the pixels if we can assign them to
                 * existing objects, count checked and reset counter on each assigned
                 * -- exit when counter equals queue length */
                for ( j = 0; j < (int) equals.size(); ) {
		  if ((j%1000)==0) R_CheckUserInterrupt();
                    ind = equals.front();
                    equals.pop_front();
                    /* check neighbours:
                     * - if exists one, assign
                     * - if two or more check what should be combined and assign to the steepest
                     * - if none, push back */
                    /* reset j to 0 every time we assign another pixel to restart the loop */
                    nb.clear();
                    pt = pointFromIndex( ind, nx );
                    /* determine which neighbour we have, push them to nb */
                    for ( x = pt.x - ext; x <= pt.x + ext; x++ )
                        for ( y = pt.y - ext; y <= pt.y + ext; y++ ) {
                            if ( x < 0 || y < 0 || x >= nx || y >= ny || (x == pt.x && y == pt.y) ) continue;
                            indxy = x + y * nx;
                            nbseed = (int) tgt[ indxy ];
                            if ( nbseed < 1 ) continue;
                            isin = false;
                            for ( it = nb.begin(); it != nb.end() && !isin; it++ )
                                if ( nbseed == *it ) isin = true;
                            if ( !isin ) nb.push_back( nbseed );
                        }
                    if ( nb.size() == 0 ) {
                        /* push the pixel back and continue with the next one */
                        equals.push_back( ind );
                        j++;
                        continue;
                    }
                    tgt[ ind ] = check_multiple(tgt, src, ind, nb, seeds, tolerance, nx, ny );
                    /* we assigned the pixel, reset j to restart neighbours detection */
                    j = 0;
                }
                /* now we have assigned all that we could */
                if ( !equals.empty() ) {
                    /* create a new seed for one pixel only and go back to assigning neighbours */
                    topseed++;
                    newseed.index = equals.front();
                    newseed.seed = topseed;
                    equals.pop_front();
                    tgt[ newseed.index ] = topseed;
                    seeds.push_back( newseed );
                }
            } // assigning equals
        } // sorted index

        /* now we need to reassign indexes while some seeds could be removed */
        double * finseed = new double[ topseed ];
        for ( i = 0; i < topseed; i++ )
            finseed[ i ] = 0;
        i = 0;
        while ( !seeds.empty() ) {
            newseed = seeds.front();
            seeds.pop_front();
            finseed[ newseed.seed - 1 ] = i + 1;
            i++;
        }
        for ( i = 0; i < nx * ny; i++ ) {
            j = (int) tgt[ i ];
            if ( 0 < j && j <= topseed )
                tgt[ i ] = finseed[ j - 1 ];
        }
        delete[] finseed;

    } // loop through images

    delete[] index;

    UNPROTECT (nprotect);
    return res;
}
Beispiel #2
0
int main()
{
  IntList theList;
  IntList theOtherList;
  IntList theFinalList;
  
  theList.display();
  cout << endl;

  theList.push_front(133);
  theList.display();
  cout << endl;
  
  theList.push_front(250);
  theList.display();
  cout << endl;

  theList.push_front(550);
  theList.display();
  cout << endl;
  
  theList.pop_front();
  theList.display();
  cout << endl;
  
  theList.push_back(13);
  theList.display();
  cout << endl;
  
  theList.select_sort();
  theList.display();
  cout << endl;
  
  theList.insert_sorted(10);
  theList.display();
  cout << endl;
  
  theList.insert_sorted(225);
  theList.display();
  cout << endl;
  
  theList.pop_front();
  theList.display();
  cout << endl;
  
  theList.pop_front();
  theList.display();
  cout << endl;
  
  theList.pop_front();
  theList.display();
  cout << endl;
  
  theList.push_back(45);
  theList.display();
  cout << endl;
  
  cout << "The other list shit:\n";
  
  theOtherList.insert_sorted(225);
  theOtherList.display();
  cout << endl;
  
  theOtherList.insert_sorted(12);
  theOtherList.display();
  cout << endl;
  
  theOtherList.insert_sorted(13);
  theOtherList.display();
  cout << endl;
  
  theOtherList.insert_sorted(13);
  theOtherList.display();
  cout << endl;
  
  theOtherList.insert_sorted(12);
  theOtherList.display();
  cout << endl;
  
  theOtherList.insert_sorted(5000);
  theOtherList.display();
  cout << endl;
  
  theOtherList.remove_duplicates();
  theOtherList.display();
  cout << endl;


  cout << "The other list shit:\n";
  
  theFinalList.push_front(10);
  theFinalList.display();
  cout << endl;
  
  theFinalList.push_front(10);
  theFinalList.display();
  cout << endl;
  
  theFinalList.push_front(30);
  theFinalList.display();
  cout << endl;
  
  theFinalList.push_front(5);
  theFinalList.display();
  cout << endl;
  
  theFinalList.push_front(20);
  theFinalList.display();
  cout << endl;
  
  theFinalList.push_front(10);
  theFinalList.display();
  cout << endl;
  
  theFinalList.push_back(30);
  theFinalList.display();
  cout << endl;
  
  theFinalList.remove_duplicates();
  theFinalList.display();
  cout << endl;
  
  return 0;
}
Beispiel #3
0
int main()
{
    IntList temp;
    temp.display();
    cout << endl;
    temp.push_front(5);
    temp.display();
    cout << endl;
    temp.push_front(15);
    temp.push_front(45);
    temp.display();
    cout << endl;
    temp.pop_front();
    temp.display();
    cout << endl;

    temp.push_back(32);
    temp.display();
    cout << endl;

    IntList temp2;
    temp2.push_back(32);
    temp2.display();
    cout << endl;
    temp2.push_back(45);
    temp2.display();
    cout << endl;

    temp.select_sort();
    temp.display();
    cout << endl;

    IntList temp3;
    temp3.select_sort();
    temp3.display();
    cout << endl;

    temp.display();
    cout << endl;
    temp.insert_sorted(10);
    temp.display();
    cout << endl;
    temp.insert_sorted(33);
    temp.display();
    cout << endl;
    temp.insert_sorted(4);
    temp.display();
    cout << endl;
    temp.insert_sorted(2);
    temp.display();
    cout << endl;

    IntList temp4;
    temp4.push_back(249);
    temp4.push_back(346);
    temp4.push_back(366);
    temp4.push_back(374);
    temp4.push_back(462);
    temp4.push_back(484);
    temp4.push_back(600);
    temp4.push_back(686);
    temp4.push_back(715);
    temp4.push_back(799);
    temp4.push_back(949);
    temp4.display();
    cout << endl;
    temp4.insert_sorted(800);
    temp4.display();
    cout << endl;

    IntList temp5;
    temp5.push_back(249);
    temp5.push_back(346);
    temp5.push_back(715);
    temp5.push_back(799);
    temp5.push_back(949);
    temp5.push_back(949);
    temp5.push_back(366);
    temp5.push_back(374);
    temp5.push_back(462);
    temp5.push_back(462);
    temp5.push_back(484);
    temp5.push_back(600);
    temp5.push_back(249);
    temp5.push_back(686);
    temp5.display();
    cout << endl;
    temp5.select_sort();
    temp5.display();
    cout << endl;

    temp5.remove_duplicates();
    temp5.display();
    cout << endl;

    IntList temp6;
    temp6.push_back(667);
    temp6.push_back(314);
    temp6.push_back(895);
    temp6.push_back(63);
    temp6.push_back(462);
    temp6.display();
    cout << endl;
    temp6.remove_duplicates();
    temp6.display();
    cout << endl;

    cout << "End" << endl;
}
Beispiel #4
0
int main()
{
   IntList a;
   IntList b;
   
   string divider = string(68, '-');
   
   cout << divider << endl;
   
   // testing push_front function
   a.push_front(1);
   a.push_front(4);
   a.push_front(412);
   
   // tests display
   cout << "IntList 1: testing push_front function" << endl;
   cout << "(should display 412 4 1): ";
   a.display(); cout << endl;
   
   cout << "IntList 2: testing empty list" << endl;
   cout << "(should display nothing): ";
   b.display(); cout << endl;
   
   cout << divider << endl;
   
   // tests pop_front;
   a.pop_front();
   cout << "IntList 1: testing pop_front function" << endl;
   cout << "(should display 4 1): ";
   a.display(); cout << endl;
   
   cout << "IntList 2: testing pop_front on empty" << endl;
   cout << "(should do nothing): ";
   b.pop_front();
   b.display(); cout << endl;
   
   b.push_front(4);
   cout << "IntList 2: testing pop_front on 1 element" << endl;
   cout << "(should see nothing): ";
   b.pop_front();
   b.display(); cout << endl;
   
   cout << divider << endl;
   
   a.push_back(3);
   a.push_back(213);
   a.push_back(61);
   
   // testing select_sort
   cout << "IntList 1: testing select_sort function" << endl;
   cout << "(should display 1 3 4 61 213): ";
   a.select_sort();
   a.display(); cout << endl;
   cout << "IntList 2: testing empty list" << endl;
   b.select_sort();
   b.display(); cout << endl;
   
   cout << divider << endl;
   
   // testing insert_sorted()
   
   cout << "IntList 1: testing insert_sorted function" << endl;
   cout << "(should display 1 2 3 4 61 213 400): ";
   a.insert_sorted(2);
   a.insert_sorted(400);
   a.display(); cout << endl;
   
   cout << "IntList 2: testing_sorted on empty list" << endl;
   cout << "(should display 4): ";
   b.insert_sorted(4);
   b.display(); cout << endl;
   
   cout << divider << endl;
   return 0;
}