Ejemplo n.º 1
0
int
ninspect_proj(Nrrd *nout, Nrrd *nin, int axis, int smart, float amount) {
  static const char me[]="ninspect_proj";
  airArray *mop;
  Nrrd *ntmpA, *ntmpB, **nrgb;
  int bins;

  if (!(nout && nin)) {
    biffAddf(NINSPECT, "%s: got NULL pointer", me);
    return 1;
  }
  if (!( AIR_IN_CL(0, axis, 2) )) {
    biffAddf(NINSPECT, "%s: given axis %d outside valid range [0,1,2]",
             me, axis);
    return 1;
  }

  /* allocate a bunch of nrrds to use as basically temp variables */
  mop = airMopNew();
  airMopAdd(mop, ntmpA = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, ntmpB = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  /* HEY: this used to be nrgb[3], but its passing to nrrdJoin caused
     "dereferencing type-punned pointer might break strict-aliasing rules"
     warning; GLK not sure how else to fix it */
  nrgb = AIR_CALLOC(3, Nrrd*);
  airMopAdd(mop, nrgb, airFree, airMopAlways);
  airMopAdd(mop, nrgb[0] = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, nrgb[1] = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, nrgb[2] = nrrdNew(), (airMopper)nrrdNuke, airMopAlways);

  /* these arguments to nrrdHistoEq will control its behavior */
  bins = 3000;  /* equalization will use a histogram with this many bins */

  /* the following idiom is one way of handling the fact that any
     non-trivial nrrd call can fail, and if it does, then any subsequent
     nrrd calls should be avoided (to be perfectly safe), so that you can
     get the error message from biff.  Because of the left-to-right ordering
     ensured for logical expressions, this will all be called in sequence
     until one of them has a non-zero return.  If he had exception handling,
     we'd put all the nrrd calls in one "try" block.  */
  if (nrrdProject(ntmpA, nin, axis, nrrdMeasureSum, nrrdTypeDefault)
      || nrrdHistoEq(ntmpB, ntmpA, NULL, bins, smart, amount)
      || nrrdQuantize(nrgb[0], ntmpB, NULL, 8)
      || nrrdProject(ntmpA, nin, axis, nrrdMeasureVariance, nrrdTypeDefault)
      || nrrdHistoEq(ntmpB, ntmpA, NULL, bins, smart, amount)
      || nrrdQuantize(nrgb[1], ntmpB, NULL, 8)
      || nrrdProject(ntmpA, nin, axis, nrrdMeasureMax, nrrdTypeDefault)
      || nrrdQuantize(nrgb[2], ntmpA, NULL, 8)
      || nrrdJoin(nout, (const Nrrd*const*)nrgb, 3, 0, AIR_TRUE)) {
    biffMovef(NINSPECT, NRRD, "%s: trouble with nrrd operations", me);
    airMopError(mop); return 1;
  }

  airMopOkay(mop);
  return 0;
}
Ejemplo n.º 2
0
int
baneRawScatterplots(Nrrd *nvg, Nrrd *nvh, Nrrd *hvol, int histEq) {
  Nrrd *gA, *hA, *gB, *hB;
  char me[]="baneRawScatterplots", err[BIFF_STRLEN];
  int E;
  
  if (!( nvg && nvh && hvol )) {
    sprintf(err, "%s: got NULL pointer", me);
    biffAdd(BANE, err); return 1;
  }
  if (baneHVolCheck(hvol)) {
    sprintf(err, "%s: didn't get a valid histogram volume", me);
    biffAdd(BANE, err); return 1;
  }

  gA = nrrdNew(); gB = nrrdNew();
  hA = nrrdNew(); hB = nrrdNew();
  /* create initial projections */
  E = 0;
  if (!E) E |= nrrdProject(gA, hvol, 1, nrrdMeasureSum, nrrdTypeDefault);
  if (!E) E |= nrrdProject(hA, hvol, 0, nrrdMeasureSum, nrrdTypeDefault);
  if (E) {
    sprintf(err, "%s: trouble creating raw scatterplots", me);
    biffMove(BANE, err, NRRD); return 1;
  }

  /* do histogram equalization on them */
  if (histEq) {
    if (!E) E |= nrrdHistoEq(gB, gA, NULL, baneStateHistEqBins,
                             baneStateHistEqSmart, 1.0);
    if (!E) E |= nrrdHistoEq(hB, hA, NULL, baneStateHistEqBins,
                             baneStateHistEqSmart, 1.0);
  } else {
    if (!E) E |= nrrdCopy(gB, gA);
    if (!E) E |= nrrdCopy(hB, hA);
  }
  if (E) {
    sprintf(err, "%s: couldn't histogram equalize or copy", me);
    biffMove(BANE, err, NRRD); return 1;
  }

  /* re-orient them so they look correct on the screen */
  if (!E) E |= nrrdAxesSwap(gA, gB, 0, 1);
  if (!E) E |= nrrdAxesSwap(hA, hB, 0, 1);
  if (!E) E |= nrrdFlip(gB, gA, 1);
  if (!E) E |= nrrdFlip(hB, hA, 1);
  if (E) {
    sprintf(err, "%s: couldn't re-orient scatterplots", me);
    biffMove(BANE, err, NRRD); return 1;
  }
  
  if (!E) E |= nrrdCopy(nvg, gB);
  if (!E) E |= nrrdCopy(nvh, hB);
  if (E) {
    sprintf(err, "%s: trouble saving results to given nrrds", me);
    biffMove(BANE, err, NRRD); return 1;
  }

  nrrdNuke(gA); nrrdNuke(gB);
  nrrdNuke(hA); nrrdNuke(hB);
  return 0;
}
Ejemplo n.º 3
0
int
unrrdu_heqMain(int argc, char **argv, char *me, hestParm *hparm) {
  hestOpt *opt = NULL;
  char *out, *err, *mapS;
  Nrrd *nin, *nout, *nmap;
  int smart, pret;
  unsigned int bins;
  airArray *mop;
  float amount;

  /* we want to facilitate saving out the mapping as a text file,
     but with the domain included */
  /* this is commented out with the 8 Aug 2003 advent of nrrdDefGetenv
  nrrdDefWriteBareTable = AIR_FALSE;
  */

  hestOptAdd(&opt, "b,bin", "bins", airTypeInt, 1, 1, &bins, NULL,
             "# bins to use in histogram that is created in order to "
             "calculate the mapping that achieves the equalization.");
  hestOptAdd(&opt, "s,smart", "bins", airTypeInt, 0, 1, &smart, "0",
             "# bins in value histogram to ignore in calculating the mapping. "
             "Bins are ignored when they get more hits than other bins, and "
             "when the values that fall in them are constant.  This is an "
             "effective way to prevent large regions of background value "
             "from distorting the equalization mapping.");
  hestOptAdd(&opt, "a,amount", "amount", airTypeFloat, 1, 1, &amount, "1.0",
             "extent to which the histogram equalizing mapping should be "
             "applied; 0.0: no change, 1.0: full equalization");
  hestOptAdd(&opt, "m,map", "filename", airTypeString, 1, 1, &mapS, "",
             "The value mapping used to achieve histogram equalization is "
             "represented by a univariate regular map.  By giving a filename "
             "here, that map can be saved out and applied to other nrrds "
             "with \"unu rmap\"");
  OPT_ADD_NIN(nin, "input nrrd");
  OPT_ADD_NOUT(out, "output nrrd");

  mop = airMopNew();
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);

  USAGE(_unrrdu_heqInfoL);
  PARSE();
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);

  nout = nrrdNew();
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
  
  if (nrrdHistoEq(nout, nin, airStrlen(mapS) ? &nmap : NULL,
                  bins, smart, amount)) {
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
    fprintf(stderr, "%s: trouble histogram equalizing:\n%s", me, err);
    airMopError(mop);
    return 1;
  }

  if (airStrlen(mapS)) {
    SAVE(mapS, nmap, NULL);
  }
  SAVE(out, nout, NULL);

  airMopOkay(mop);
  return 0;
}
Ejemplo n.º 4
0
int
baneGkms_pvgMain(int argc, char **argv, char *me, hestParm *hparm) {
  hestOpt *opt = NULL;
  char *outS, *perr, err[BIFF_STRLEN], *mapS;
  Nrrd *ninfo, *nposA, *nposB, *ndon, *npvg;
  NrrdIoState *nio;
  airArray *mop;
  int i, pret, invert, sv, sg, smlI;
  float *pos, p, min, max, sml, newsml, newmin, newmax;
  NrrdRange *range;

  hestOptAdd(&opt, "inv", NULL, airTypeInt, 0, 0, &invert, NULL,
             "Draw on white background, instead of black");
  hestOptAdd(&opt, "m", "mapOut", airTypeString, 1, 1, &mapS, "",
             "save out the colormap used here, so that it can be applied "
             "to other nrrds with \"unu imap -r\"");
  hestOptAdd(&opt, "i", "infoIn", airTypeOther, 1, 1, &ninfo, NULL,
             "input info file (from \"gkms info\")",
             NULL, NULL, nrrdHestNrrd);
  hestOptAdd(&opt, "o", "imageOut", airTypeString, 1, 1, &outS, NULL,
             "output image, in PPM format");

  mop = airMopNew();
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
  USAGE(_baneGkms_pvgInfoL);
  PARSE();
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
  airMopAdd(mop, ndon=_baneGkmsDonNew(invert),
            (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, nposA=nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, nposB=nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, npvg=nrrdNew(), (airMopper)nrrdNuke, airMopAlways);
  airMopAdd(mop, nio=nrrdIoStateNew(), (airMopper)nrrdIoStateNix,
            airMopAlways);

  if (airStrlen(mapS)) {
    if (nrrdSave(mapS, ndon, NULL)) {
      sprintf(err, "%s: trouble saving colormap", me);
      biffMove(BANE, err, NRRD); airMopError(mop); return 1;
    }
  }

  /* we use sigma = 1.0: different sigmas will scale the position,
     which will not change the coloring
     gthresh = 0.0: we want to see everything, and Simian has taught
     us that there can be boundaries at extremely low gradients */
  if (banePosCalc(nposA, 1.0, 0.0, ninfo)) {
    sprintf(err, "%s: trouble calculating position", me);
    biffAdd(BANE, err); airMopError(mop); return 1;
  }
  sv = nposA->axis[0].size;
  sg = nposA->axis[1].size;
  pos = (float *)nposA->data;

  /* find min, max, sml, smlI: histo-eq will warp values around such
     that min-->min and max-->max, but 0-->??.  So, find smallest
     magnitide position (sml) as a stand-in for 0.0 and its index
     (smlI) */
  sml = 0;
  smlI = 0;
  min = max = AIR_NAN;
  for (i=0; i<sv*sg; i++) {
    p = pos[i];
    if (!AIR_EXISTS(p))
      continue;
    if (!AIR_EXISTS(min)) {
      min = max = p;
      sml = AIR_ABS(p);
      smlI = i;
      continue;
    }
    min = AIR_MIN(p, min);
    max = AIR_MAX(p, max);
    if (AIR_ABS(p) < sml) {
      sml = AIR_ABS(p);
      smlI = i;
    }
  }
  if (!AIR_EXISTS(min)) {
    sprintf(err, "%s: didn't see any real data in position array", me);
    biffAdd(BANE, err); airMopError(mop); return 1;
  }
  if (nrrdHistoEq(nposB, nposA, NULL, PVG_HISTEQ_BINS, 3, 1.0)) {
    sprintf(err, "%s: trouble doing histo-eq on p(v,g)", me);
    biffMove(BANE, err, NRRD); airMopError(mop); return 1;
  }

  /* warp position values that pos[smlI] gets mapped back to zero,
     and so that [newmin,newmax] is centered on zero */
  pos = (float *)nposB->data;
  newsml = pos[smlI];
  if (min < -max) {
    newmin = min;
    newmax = -min;
  } else {
    newmin = -max;
    newmax = max;
  }
  for (i=0; i<sv*sg; i++) {
    if (!AIR_EXISTS(pos[i])) {
      continue;
    }
    if (pos[i] < newsml) {
      pos[i] = AIR_CAST(float, AIR_AFFINE(min, pos[i], newsml, newmin, 0.0));
    } else {