www.pudn.com > jseg.rar > jfunc.c


#include 
#include 
#include 
#include 
#include "mathutil.h"
#include "imgutil.h"
#include "memutil.h"
#include "ioutil.h"
#include "quan.h" 
#include "segment.h"

void getJ(unsigned char *cmap,int N,int ny,int nx,float *J,int offset,int step,
    short *rmap,unsigned char *rmap0,int TR)
{
  int iy,ix,jy,jx,i,*p,StN,StN1,corner[3];
  float St1,St,Sb,Sw,Stmeanx,Stmeany,*avgy,*avgx,*Jtmp,total,**weight;
  unsigned char *cmap1,*rmap1;
  int l,imgsize,imgsize2,ny2,nx2,loc,loc1,loc2,sy,ey,sx,ex,cornerI,cornerT[6];

  imgsize = ny*nx;
  ny2 = ny+2*offset; nx2 = nx+2*offset;
  imgsize2 = ny2*nx2;
  cmap1 = (unsigned char *) calloc (imgsize2,sizeof(unsigned char)); 
  rmap1 = (unsigned char *) calloc (imgsize2,sizeof(unsigned char));
  extendbounduc(cmap,cmap1,ny,nx,offset,1);
  extendbounduc(rmap0,rmap1,ny,nx,offset,1);
  corner[0]=0; corner[1]=step; corner[2]=3*step;

  cornerT[0]=-offset;        cornerT[1]=offset;
  cornerT[2]=-offset+step;   cornerT[3]=offset-step;
  cornerT[4]=-offset+2*step; cornerT[5]=offset-2*step;

  StN1=sqr(2*offset/step+1)-20;
  St1=0; 
  for (jy=-offset;jy<=offset;jy+=step)
  {
    if (jy==cornerT[0] || jy==cornerT[1]) cornerI = 2;
    else if (jy==cornerT[2] || jy==cornerT[3]) cornerI = 1;
    else if (jy==cornerT[4] || jy==cornerT[5]) cornerI = 1;
    else cornerI = 0;
    sx = -offset+corner[cornerI]; ex = offset-corner[cornerI];
    for (jx=sx;jx<=ex;jx+=step) St1 += sqr(jy)+sqr(jx);
  }

  for (l=0;l0) { avgy[i]/=p[i]; avgx[i]/=p[i]; }
        }
        Sw=0;
        sy = iy-offset; ey = iy+offset;
        for (jy=sy;jy<=ey;jy+=step)
        {
          if      (jy==cornerT[0] || jy==cornerT[1]) cornerI = 2;
          else if (jy==cornerT[2] || jy==cornerT[3]) cornerI = 1;
          else if (jy==cornerT[4] || jy==cornerT[5]) cornerI = 1;
          else cornerI= 0;
          sx = ix-offset+corner[cornerI]; ex = ix+offset-corner[cornerI];
          for (jx=sx;jx<=ex;jx+=step)
          {
            loc1 = LOC2(jy+offset,jx+offset,nx2);
            if (rmap1[loc1]==rmap1[loc2])
              Sw += sqr(jy-iy-avgy[cmap1[loc1]]) + sqr(jx-ix-avgx[cmap1[loc1]]);
          }
        }
        if (Sw==0) J[loc]=2;
        else 
        {
          Sb = St- Sw;
          if (Sb<0) Sb=0;
          J[loc] = Sb/Sw;
          if (J[loc]>2) J[loc]=2;
        }
      }
      loc ++;
    }
  }
  free(cmap1);
  free(rmap1);
  free(avgy); free(avgx); free(p);

  if (step>1)
  {
    step /=2;
    Jtmp=(float *)calloc(imgsize,sizeof(float));
    weight = (float **)fmatrix(2*step+1,2*step+1);
    genwindow(weight,2*step+1);
    for (i=0;i<2;i++)
    {
      for (l=0;l=0 && jy=0 && jx0) { avgJ[i] = avgJ[i]/count[i]; done[i]=0; }
    else done[i]=1; 
    alldone += done[i];
  }

  varJ = (float *)calloc(TR+1,sizeof(float));
  for (l=0;l0) varJ[i] = sqrt(varJ[i]/count[i]);
    if (status==0) threshJ1[i] = avgJ[i] + 0.1*varJ[i]; 
    else if (status==1) threshJ1[i] = avgJ[i] - 0.35*varJ[i];
    threshJ2[i] = varJ[i];
  }
  free(varJ);

  free(count); free(avgJ);
  return alldone;
}

void showJ(float *J0,float **threshJ1,float **threshJ2,unsigned char *rmap00,
    int nt,int ny,int nx)
{
  float *J1,*J;
  int i,imgsize,it;
  unsigned char *rmap0;
  char fname[200];

  J = J0; rmap0 = rmap00;
  imgsize = ny*nx;
  J1=(float *)calloc(imgsize,sizeof(float));
  for (it=0;it(threshJ1[it][rmap0[i]]-0.8*threshJ2[it][rmap0[i]])) J1[i]=200;
      else if (J[i]==0) J1[i]=100;
    }
    if (nt==1) sprintf(fname,"J1.seg.gray");
    else sprintf(fname,"J1.%d.seg.gray",it);
    outfloat2raw(fname,J1,ny,nx,1);
*/

    J += imgsize; rmap0 += imgsize;
  }
  free(J1);
}

void getJT(unsigned char *cmap,int N,int ny,int nx,float *JT,int offset,int step,
    short *rmap,unsigned char *rmap0,int TR)
{
  int iy,ix,jy,jx,i,*p,StN,StN1,corner[3];
  float St1,St,Sb,Sw,Stmeant,*avgt,*Jtmp,total,**weight;
  unsigned char *cmap1;
  int l,imgsize,imgsize2,ny2,nx2,loc,loc1,sy,ey,sx,ex,cornerI,cornerT[6];

  imgsize = ny*nx;
  ny2 = ny+2*offset; nx2 = nx+2*offset;
  imgsize2 = ny2*nx2;
  cmap1 = (unsigned char *) calloc (2*imgsize2,sizeof(unsigned char));
  extendbounduc(cmap,cmap1,ny,nx,offset,1);
  extendbounduc(cmap+imgsize,cmap1+imgsize2,ny,nx,offset,1);
  corner[0]=0; corner[1]=step; corner[2]=3*step;

  cornerT[0]=-offset;        cornerT[1]=offset;
  cornerT[2]=-offset+step;   cornerT[3]=offset-step;
  cornerT[4]=-offset+2*step; cornerT[5]=offset-2*step;

  StN1=2*(sqr(2*offset/step+1)-20); 
  St1=StN1*sqr(1);
  for (l=0;l0) avgt[i]/=p[i]; 
        }
        Sw=0;
        sy = iy-offset; ey = iy+offset;
        for (jy=sy;jy<=ey;jy+=step)
        {
          if      (jy==cornerT[0] || jy==cornerT[1]) cornerI = 2;
          else if (jy==cornerT[2] || jy==cornerT[3]) cornerI = 1;
          else if (jy==cornerT[4] || jy==cornerT[5]) cornerI = 1;
          else cornerI= 0;
          sx = ix-offset+corner[cornerI]; ex = ix+offset-corner[cornerI];
          for (jx=sx;jx<=ex;jx+=step)
          {
            loc1 = LOC2(jy+offset,jx+offset,nx2);
            Sw += sqr(-1-avgt[cmap1[loc1]]);
            Sw += sqr(1-avgt[cmap1[loc1+imgsize2]]);
          }
        }
        if (Sw==0) JT[loc]=2;
        else
        {
          Sb = St- Sw;
          if (Sb<0) Sb=0;
          JT[loc] = Sb/Sw;
          if (JT[loc]>2) JT[loc]=2;
        }
      }
      loc ++;
    }
  }
  free(cmap1);

  free(avgt); free(p);

  if (step>1)
  {
    step /=2;
    Jtmp=(float *)calloc(imgsize,sizeof(float));
    weight = (float **)fmatrix(2*step+1,2*step+1);
    genwindow(weight,2*step+1);
    for (i=0;i<2;i++)
    {
      for (l=0;l=0 && jy=0 && jx0)
      {
        i=rmap[l]; j=cmap[l];
        Stmeanx[i] += ix; Stmeany[i] += iy;
        StN[i] ++;
        avgx[i][j] += ix; avgy[i][j] += iy;
        avgN[i][j] ++;
      }
      l++;
    }
  }

  for (i=1;i<=TR;i++)
  {
    Stmeanx[i] /= StN[i]; Stmeany[i] /= StN[i];
    for (j=0;j0) { avgx[i][j] /= avgN[i][j]; avgy[i][j] /= avgN[i][j]; }
    }
  }

  l=0;
  for (iy=0;iy0)
      {
        i=rmap[l]; j=cmap[l];
        St[i] += sqr(iy-Stmeany[i]) + sqr(ix-Stmeanx[i]);
        var[i][j] += sqr(iy-avgy[i][j]) + sqr(ix-avgx[i][j]);
      }
      l++;
    }
  }
  for (i=1;i<=TR;i++)
  {
    for (j=0;j0)
  {
    for (i=1;i<=TR;i++)
    {
      for (j=0;j