Array * MedianFilter(RasterizedImage & img, ParaSet & imgPara, Array *original, int filterRadius){ // output image Array *median = Make_Array_With_Shape(PLAIN_KIND,UINT32_TYPE,Coord2(original->dims[1], original->dims[0])); uint32 *med = AUINT32(median); uint32 *orig = AUINT32(original); #ifdef DEVELOP Array *variance = Make_Array_With_Shape(PLAIN_KIND,UINT32_TYPE,Coord2(original->dims[1], original->dims[0])); uint32 *var = AUINT32(variance); #endif Use_Reflective_Boundary(); // frames defines the local neighborhood Frame * f = Make_Frame(original,Coord2(2*filterRadius+1,2*filterRadius+1),Coord2(filterRadius,filterRadius)); Histogram * h = Make_Histogram(UVAL,img.GetDepth(),ValU(1),ValU(0)); Place_Frame(f,0); for (Indx_Type p = 0; p < median->size; p++){ Empty_Histogram(h); Histagain_Array(h,f,0); h->counts[orig[p]]--; // excludes the height of p in the calculation of the median med[p] = Percentile2Bin(h,.5); // median is given at 50th percentile #ifdef DEVELOP var[p] = 10*Histogram_Sigma(h); #endif Move_Frame_Forward(f); } Kill_Histogram(h); Kill_Frame(f); #ifdef DEVELOP ShowArray(img, imgPara, "median.tif", median); ShowArray(img, imgPara, "variance.tif", variance); Free_Array(variance); #endif if (imgPara.verbose){ std::cout << "Planes filtered" << std::endl; } return (median); }
double ComputeSmoothness(Array * heightMap) { Array * distances = Make_Array_With_Shape(PLAIN_KIND,UINT32_TYPE,Coord2(heightMap->dims[1], heightMap->dims[0])); uint32 * distVals = AUINT32(distances); uint32 * hmVals = AUINT32(heightMap); Use_Reflective_Boundary(); HeightMapRange range = GetLevelRange(heightMap); // frame defines the local neighborhood Frame * f = Make_Frame(heightMap,Coord2(3,3),Coord2(1,1)); Histogram * h = Make_Histogram(UVAL,range.maxLayer,ValU(1),ValU(0)); Place_Frame(f,0); for (Indx_Type i=0; i< heightMap->size; i++) { Empty_Histogram(h); Histagain_Array(h, f, 0); int middleBin = Value2Bin(h, ValU(hmVals[i])); // To determine the median value of the pixel's neighborhood, we exlude the current pixel i from the histogram h->counts[middleBin]--; distVals[i] = std::abs(static_cast<double>(hmVals[i]) - static_cast<double>(Percentile2Bin(h, 0.5))); } #ifdef DEVELOP Write_Image("HeightMapSmoothness", distances, DONT_PRESS); #endif Empty_Histogram(h); Histagain_Array(h, distances, 0); double meanDistance = Histogram_Mean(h); std::cout << "Smoothness:\n Mean distance: " << meanDistance << "\n Sd: " << Histogram_Sigma(h) << std::endl; Free_Histogram(h); Free_Frame(f); Free_Array(distances); return meanDistance; }
void Segment_Channel(Array *input, Segmentation *seg) { double mean, sdev; int threshc, threshe, sizemin; Array *labels; Histogram *hist = Histogram_Array(input,0x100,VALU(1),VALU(0)); mean = Histogram_Mean(hist); sdev = Histogram_Sigma(hist); threshc = mean + Get_Double_Arg("-c")*sdev; threshe = mean + Get_Double_Arg("-e")*sdev; sizemin = Get_Int_Arg("-s"); #ifdef PROGRESS printf("\nChannel Segmentation:\n"); printf(" Mean = %.2f Std.Dev = %.2f\n",mean,sdev); printf(" Thresh-c = %d Thresh-e = %d Size-s = %d\n",threshc,threshe,sizemin); #ifdef DEBUG Print_Histogram(hist,stdout,4,BIN_COUNT|CUMULATIVE_COUNT|CLIP_HGRAM,0); #endif fflush(stdout); #endif Free_Histogram(hist); labels = Make_Array(PLAIN_KIND,UINT8_TYPE,3,input->dims); Array_Op_Scalar(labels,SET_OP,UVAL,VALU(0)); SEG_threshc = threshc; SEG_threshe = threshe; SEG_sizemin = sizemin; SEG_values = AUINT16(input); SEG_labels = AUINT8(labels); SEG_count = 0; SEG_coretouch = 0; SEG_id = 0; // Mark connected-components of pixels >= threshc that have not less than sizemin pixels Flood_All(input,0,ISCON2N,NULL,InCore,NULL,CountCore,NULL,GoodCore,NULL,MarkAsIn); // Mark all connected components of pixels >= threshe that contain a good core as above Flood_All(input,0,ISCON2N,NULL,InExtend,NULL,TouchCore,NULL,GoodExtend,NULL,SetLabel); // Capture each labeled region in "labels" with a Region { int i, nsegs; Indx_Type p; uint8 *val; Region **segs; seg->label = labels; seg->nsegs = nsegs = SEG_id; seg->segs = segs = (Region **) Guarded_Malloc(sizeof(Region *)*nsegs,Program_Name()); seg->mean = mean; seg->ethresh = threshe; seg->cthresh = threshc; for (i = 0; i < nsegs; i++) segs[i] = NULL; val = AUINT8(labels); for (p = 0; p < labels->size; p++) { i = val[p]; if (i > 0 && segs[i-1] == NULL) segs[i-1] = Record_Basic(labels,0,ISCON2N,p,1,EQ_COMP,VALU(i)); } } }