Exemplo n.º 1
0
Set *ForestRemoval(ImageForest *fst, Set *Rm, AdjRel *A)
{
  int  i, p, q, n = fst->cost->ncols*fst->cost->nrows;
  Image *cost=fst->cost,*pred=fst->pred,*root=fst->root;
  Pixel u,v;
  Set  *Frontier=NULL, *aux=NULL;
  BMap *RemRoot = BMapNew(n), *inFrontier = BMapNew(n);  
  int  *RemNode=AllocIntArray(n),first=0,last=0;

  /* Mark all removal roots, whose trees will be removed, reinitialize
  cost and predecessor values of those roots, and insert them in
  RemNode, in order to reinitialize the cost and predecessor values of
  the remaining nodes of the removed trees. */

  aux = Rm;
  while (aux != NULL) { 
    p = root->val[aux->elem]; // get a removal root p 
    if (_fast_BMapGet(RemRoot,p)==0){ // p is not in RemRoot
      cost->val[p] = INT_MAX;  pred->val[p] = NIL;
      RemNode[last]=p; last++;
      _fast_BMapSet1(RemRoot,p);
    }
    aux = aux->next;
  }

  /* Forest removal: reinitialize nodes of the removed trees and
     create frontier set for the next DIFT. */

  while (first != last) {
    p = RemNode[first]; first++;
    u.x = p % cost->ncols; 
    u.y = p / cost->ncols; 
    for (i=1; i < A->n; i++) {
      v.x = u.x + A->dx[i];
      v.y = u.y + A->dy[i];
      if (ValidPixel(cost,v.x,v.y)){
	q   = v.x + cost->tbrow[v.y];
	if (pred->val[q]==p){
	  cost->val[q]=INT_MAX; pred->val[q] = NIL;
	  RemNode[last]=q; last++;
	}else{ 
	  if (_fast_BMapGet(RemRoot,root->val[q])==0){ 	                                   if (_fast_BMapGet(inFrontier,q)==0){
	      InsertSet(&Frontier,q); _fast_BMapSet1(inFrontier,q);
	    }
	  }
	}
      }
    }
  }

  BMapDestroy(inFrontier);
  BMapDestroy(RemRoot);
  free(RemNode);

  return(Frontier);
}
Exemplo n.º 2
0
// Create adjacent list in subgraph: a knn graph
void opf_CreateArcs(Subgraph *sg, int knn){
    int    i,j,l,k;
    float  dist;
    int   *nn=AllocIntArray(knn+1);
    float *d=AllocFloatArray(knn+1);

    /* Create graph with the knn-nearest neighbors */

    sg->df=0.0;
    for (i=0; i < sg->nnodes; i++)
    {
        for (l=0; l < knn; l++)
            d[l]=FLT_MAX;
        for (j=0; j < sg->nnodes; j++)
        {
            if (j!=i)
            {
	      if (!opf_PrecomputedDistance)
		d[knn] = opf_ArcWeight(sg->node[i].feat,sg->node[j].feat,sg->nfeats);
	      else
		d[knn] = opf_DistanceValue[sg->node[i].position][sg->node[j].position];
	      nn[knn]= j;
	      k      = knn;
	      while ((k > 0)&&(d[k]<d[k-1]))
                {
		  dist    = d[k];
		  l       = nn[k];
		  d[k]    = d[k-1];
		  nn[k]   = nn[k-1];
		  d[k-1]  = dist;
		  nn[k-1] = l;
		  k--;
                }
            }
        }

        for (l=0; l < knn; l++)
        {
            if (d[l]!=INT_MAX)
            {
                if (d[l] > sg->df)
                    sg->df = d[l];
		//if (d[l] > sg->node[i].radius)
		sg->node[i].radius = d[l];
                InsertSet(&(sg->node[i].adj),nn[l]);
            }
        }
    }
    free(d);
    free(nn);

    if (sg->df<0.00001)
        sg->df = 1.0;
}
Exemplo n.º 3
0
// Compute accuracy
float opf_Accuracy(Subgraph *sg){
	float Acc = 0.0f, **error_matrix = NULL, error = 0.0f;
	int i, *nclass = NULL, nlabels=0;

	error_matrix = (float **)calloc(sg->nlabels+1, sizeof(float *));
	for(i=0; i<= sg->nlabels; i++)
	  error_matrix[i] = (float *)calloc(2, sizeof(float));

	nclass = AllocIntArray(sg->nlabels+1);

	for (i = 0; i < sg->nnodes; i++){
	  nclass[sg->node[i].truelabel]++;
	}

	for (i = 0; i < sg->nnodes; i++){
	  if(sg->node[i].truelabel != sg->node[i].label){
	    error_matrix[sg->node[i].truelabel][1]++;
	    error_matrix[sg->node[i].label][0]++;
	  }
	}

	for(i=1; i <= sg->nlabels; i++){
	  if (nclass[i]!=0){
	    error_matrix[i][1] /= (float)nclass[i];
	    error_matrix[i][0] /= (float)(sg->nnodes - nclass[i]);
	    nlabels++;
	  }
	}

	for(i=1; i <= sg->nlabels; i++){
	  if (nclass[i]!=0)
	    error += (error_matrix[i][0]+error_matrix[i][1]);
	}

	Acc = 1.0-(error/(2.0*nlabels));

	for(i=0; i <= sg->nlabels; i++)
	  free(error_matrix[i]);
	free(error_matrix);
	free(nclass);

	return(Acc);
}
Exemplo n.º 4
0
int *QuantizeColors(CImage *cimg, int color_dim)
{
  ulong i;
  ulong r, g, b;
  ulong fator_g, fator_b;
  int *color, n;
  
  n = cimg->C[0]->nrows * cimg->C[0]->ncols;  

  color = AllocIntArray(n);
  
  fator_g = color_dim;
  fator_b = fator_g*color_dim;
  
  for(i=0; i<n; i++){
    r = color_dim*cimg->C[0]->val[i]/256;
    g = color_dim*cimg->C[1]->val[i]/256;
    b = color_dim*cimg->C[2]->val[i]/256;
    
    color[i] = (r + fator_g*g + fator_b*b);
  }
  return(color);
}
Exemplo n.º 5
0
// Split subgraph into two parts such that the size of the first part
// is given by a percentual of samples.
void opf_SplitSubgraph(Subgraph *sg, Subgraph **sg1, Subgraph **sg2, float perc1){
  int *label=AllocIntArray(sg->nlabels+1),i,j,i1,i2;
  int *nelems=AllocIntArray(sg->nlabels+1),totelems;
  srandom((int)time(NULL));

  for (i=0; i < sg->nnodes; i++) {
    sg->node[i].status = 0;
    label[sg->node[i].truelabel]++;
  }

  for (i=0; i < sg->nnodes; i++) {
    nelems[sg->node[i].truelabel]=MAX((int)(perc1*label[sg->node[i].truelabel]),1);
  }

  free(label);

  totelems=0;
  for (j=1; j <= sg->nlabels; j++)
    totelems += nelems[j];

  *sg1 = CreateSubgraph(totelems);
  *sg2 = CreateSubgraph(sg->nnodes-totelems);
  (*sg1)->nfeats = sg->nfeats;
  (*sg2)->nfeats = sg->nfeats;

  for (i1=0; i1 < (*sg1)->nnodes; i1++)
    (*sg1)->node[i1].feat = AllocFloatArray((*sg1)->nfeats);
  for (i2=0; i2 < (*sg2)->nnodes; i2++)
    (*sg2)->node[i2].feat = AllocFloatArray((*sg2)->nfeats);

  (*sg1)->nlabels = sg->nlabels;
  (*sg2)->nlabels = sg->nlabels;

  i1=0;
  while(totelems > 0){
    i = RandomInteger(0,sg->nnodes-1);
    if (sg->node[i].status!=NIL){
      if (nelems[sg->node[i].truelabel]>0){// copy node to sg1
	(*sg1)->node[i1].position = sg->node[i].position;
	for (j=0; j < (*sg1)->nfeats; j++)
	  (*sg1)->node[i1].feat[j]=sg->node[i].feat[j];
	(*sg1)->node[i1].truelabel = sg->node[i].truelabel;
	i1++;
	nelems[sg->node[i].truelabel] = nelems[sg->node[i].truelabel] - 1;
	sg->node[i].status = NIL;
	totelems--;
      }
    }
  }

  i2=0;
  for (i=0; i < sg->nnodes; i++){
    if (sg->node[i].status!=NIL){
      (*sg2)->node[i2].position = sg->node[i].position;
      for (j=0; j < (*sg2)->nfeats; j++)
	(*sg2)->node[i2].feat[j]=sg->node[i].feat[j];
      (*sg2)->node[i2].truelabel = sg->node[i].truelabel;
      i2++;
    }
  }

  free(nelems);
}
Exemplo n.º 6
0
// Create adjacent list in subgraph: a knn graph.
// Returns an array with the maximum distances
// for each k=1,2,...,kmax
float* opf_CreateArcs2(Subgraph *sg, int kmax)
{
    int    i,j,l,k;
    float  dist;
    int   *nn=AllocIntArray(kmax+1);
    float *d=AllocFloatArray(kmax+1);
    float *maxdists=AllocFloatArray(kmax);
    /* Create graph with the knn-nearest neighbors */

    sg->df=0.0;
    for (i=0; i < sg->nnodes; i++)
    {
        for (l=0; l < kmax; l++)
            d[l]=FLT_MAX;
        for (j=0; j < sg->nnodes; j++)
        {
            if (j!=i)
            {
                if(!opf_PrecomputedDistance)
                    d[kmax] = opf_ArcWeight(sg->node[i].feat,sg->node[j].feat,sg->nfeats);
                else
                    d[kmax] = opf_DistanceValue[sg->node[i].position][sg->node[j].position];
                nn[kmax]= j;
                k      = kmax;
                while ((k > 0)&&(d[k]<d[k-1]))
                {
                    dist    = d[k];
                    l       = nn[k];
                    d[k]    = d[k-1];
                    nn[k]   = nn[k-1];
                    d[k-1]  = dist;
                    nn[k-1] = l;
                    k--;
                }
            }
        }
        sg->node[i].radius = 0.0;
        sg->node[i].nplatadj = 0; //zeroing amount of nodes on plateaus
        //making sure that the adjacent nodes be sorted in non-decreasing order
        for (l=kmax-1; l >= 0; l--)
        {
            if (d[l]!=FLT_MAX)
            {
                if (d[l] > sg->df)
                    sg->df = d[l];
                if (d[l] > sg->node[i].radius)
                    sg->node[i].radius = d[l];
                if(d[l] > maxdists[l])
                    maxdists[l] = d[l];
                //adding the current neighbor at the beginnig of the list
                InsertSet(&(sg->node[i].adj),nn[l]);
            }
        }
    }
    free(d);
    free(nn);

    if (sg->df<0.00001)
        sg->df = 1.0;

    return maxdists;
}
Exemplo n.º 7
0
static void DoInterpolateMap(Widget w, StdForm *sf, XmListCallbackStruct *cb)
{
    int i, j, nX, nY;
    int err, type, order, corners;
    double xspa, yspa;
    string buf;
    MAP *m, *old = (MAP *)sf->any;
    int **L;
    double **A;
    
    int FillHolesInArray(), InterpolateArray();
    void FreeIntArray(), FreeDoubleArray();
    int **AllocIntArray();
    double **AllocDoubleArray();
    void send_line(), wsscanf(), SetWatchCursor();
    void MapDraw(), SetIntpOrder();
    int GetIntpType(), GetIntpOrder(), GetIntpCorners();
    MAP *new_map();
    list *get_maplist();

    if (!old) {
        PostErrorDialog(w, "No map selected to be interpolated.");
        return;
    }
    
    type    = GetIntpType();
    order   = GetIntpOrder();
    corners = GetIntpCorners();
    
    if (order <= 0) {
        PostWarningDialog(w, "Selected interpolation order is zero.");
        return;
    }
    
    nX   = old->i_no;
    nY   = old->j_no;
    xspa = old->xspacing;
    yspa = old->yspacing;
    
    A = AllocDoubleArray(nX, nY);
    if (!A) {
        PostErrorDialog(NULL, "InterpolateMap: Out of memory.");
        return;
    }
    L = AllocIntArray(nX, nY);
    if (!L) {
        FreeDoubleArray(A, nX, nY);
        PostErrorDialog(NULL, "InterpolateMap: Out of memory.");
        return;
    }
    
    SetWatchCursor(True);
    
    for (i=0; i<nX; i++) {
        for (j=0; j<nY; j++) {
            A[i][j] = old->d[i][j];
            if (old->f[i][j] <= BLANK)
                L[i][j] = EMPTY;
            else
                L[i][j] = FILLED;
        }
    }

    if (corners > 0)
        err = FillHolesInArray(A, L, nX, nY, corners);

    if ((i = order)) {
        while (i) {
            err = InterpolateArray(&A, &L, &nX, &nY, type);
            if (err != 0) {
                sprintf(buf, "Couldn't interpolate array. err=%d.", err);
                send_line(buf);
                if (A) FreeDoubleArray(A, nX, nY);
                if (L) FreeIntArray(L, nX, nY);
                SetWatchCursor(False);
                PostErrorDialog(NULL, buf);
                return;
            }
            xspa /= 2.0;
            yspa /= 2.0;
            i--;
            if (corners > 0)
                err = FillHolesInArray(A, L, nX, nY, corners);
        }
    }

    m = new_map(get_maplist(), nX, nY);
    
    m->type     = old->type;
    m->swapped  = old->swapped;
    m->memed    = old->memed;
    m->original = old->original;
    m->x0       = old->x0;
    m->y0       = old->y0;
    m->xleft    = old->xleft;
    m->xright   = old->xright;
    m->ylower   = old->ylower;
    m->yupper   = old->yupper;
    m->date     = old->date;
    strcpy(m->molecule, old->molecule);
    m->interpolated = old->interpolated + 1;
    m->equinox  = old->equinox;
    m->epoch    = old->epoch;
    
    m->ndata    = nX * nY;
    m->i_min    = NINT(m->xleft/xspa);
    m->i_max    = NINT(m->xright/xspa);
    m->j_min    = NINT(m->ylower/yspa);
    m->j_max    = NINT(m->yupper/yspa);
    m->xspacing = xspa;
    m->yspacing = yspa;
    
    for (i=0; i<nX; i++) {
        for (j=0; j<nY; j++) {
            if (L[i][j] == FILLED) {
                m->f[i][j] = UNBLANK;
                m->d[i][j] = A[i][j];
                m->e[i][j] = UNDEF;
            } else {
                m->f[i][j] = BLANK;
                m->d[i][j] = UNDEF;
                m->e[i][j] = UNDEF;
            }
        }
    }
    
    FreeDoubleArray(A, nX, nY);
    FreeIntArray(L, nX, nY);

    SetWatchCursor(False);

    wsscanf(sf->edit[0], m->name);
    sprintf(buf, "Interpolated map stored as '%s': %dx%d\n",  m->name, nX, nY);
    send_line(buf);
    
    XtDestroyWidget(sf->form);
    
    SetIntpOrder(0);
    MapDraw(NULL, m, NULL);
}