/* Median filter that only modifies a pixel when it significantly differs from the median of its 8-connected neighborhood that is weighted by the variance @param img: input image @param imgPara: struct with parameters @param original: current height map @param filterRadius: radius to define the 8-connected neighborhood @param threshold: threshold to identify pixels that need to be modified */ Array * LocalMedianFilter_InclVar(RasterizedImage & img, ParaSet & imgPara, Array * original, int filterRadius, double threshold){ uint32 *orig = AUINT32(original); Array *median = Make_Array_With_Shape(PLAIN_KIND, UINT32_TYPE, Coord2(original->dims[1], original->dims[0])); uint32 *med = AUINT32(median); Use_Extend_Boundary(); Frame * f = Make_Frame(original,Coord2(2*filterRadius+1,2*filterRadius+1),Coord2(filterRadius, filterRadius)); // Frame to compute the variance in intensity of the current grid element Frame * varF = Make_Frame(img.GetImage(),Coord3(1, imgPara.radius,imgPara.radius),Coord3(0, 0, 0)); Histogram * varH = Make_Histogram(UVAL,256,ValU(1),ValU(0)); uint32 * data; Place_Frame(f,0); Place_Frame(varF, 0); for (Dimn_Type y = 0; y <= img.GetHeight()-imgPara.radius; y += imgPara.radius){ for (Dimn_Type x = 0; x <= img.GetWidth()-imgPara.radius; x += imgPara.radius){ Indx_Type pHM = (y/imgPara.radius) * original->dims[0] + (x/imgPara.radius); Place_Frame(f, pHM); data = (uint32 *) Frame_Values(f); std::vector<double> depths; double sum = 0; double n = 0; for (int i = 0; i < AForm_Size(f); i++) { Indx_Type p = Coord2IdxA(img.GetImage(), Coord3(data[i],y, x)); Place_Frame(varF, p); Histagain_Array(varH,varF,0); sum += Histogram_Variance(varH) * data[i]; n += Histogram_Variance(varH); for (int j=0; j< floor(Histogram_Variance(varH)/10); j++) { // weight the current height with the variance depths.push_back(data[i]); } } double weightedMedian = VectorMedian(depths); if (abs(orig[pHM] - weightedMedian) > threshold ) { med[pHM] = floor(weightedMedian); } else { med[pHM] = orig[pHM]; } } } Kill_Histogram(varH); Kill_Frame(varF); Kill_Frame(f); return median; }
void RasterizedImage::SetGrid(const int radius){ gridX_ = ceil ((double)realWidth_/(double)radius); gridY_ = ceil ((double)realHeight_/(double)radius); Use_Extend_Boundary(); Pad_Array_Inplace(*image_, Coord3(0, 0, 0), Coord3(depth_, gridY_*radius, gridX_*radius)); SetWidth((*image_)->dims[0]); SetHeight((*image_)->dims[1]); }
//default world: // width = 1000, length = 1000, object at (500, 500) World::World() { this->object = Coord3(500, 500, 0); this->width = 1000; this->length = 1000; this->x_inc = 50; this->y_inc = 100; }
World::World(double obj_x, double obj_y, double obj_z, double width, double length) { this->object = Coord3(obj_x, obj_y, obj_z); this->width = width; this->length = length; this->x_inc = 50; this->y_inc = 100; }
int Read_All_Channels(string name, int maxchans) { Tiff *tif; int depth, height, width, chans; int i; if(strcmp(getSuffix(name), "tif")==0 || strcmp(getSuffix(name), "lsm")==0) { tif = Open_Tiff(name,"r"); if (tif == NULL) { fprintf(stderr,"Cannot open file %s for reading\n",name); exit (1); } Get_IFD_Shape(tif,&width,&height,&chans); printf("width=%d height=%d chans=%d\n", width, height, chans); if (chans < 2) { fprintf(stderr,"LSM %s does not at least 2 channels\n",name); exit (1); } depth = 0; while ( ! Tiff_EOF(tif)) { int w, h, c; Get_IFD_Shape(tif,&w,&h,&c); if (w == width && h == height && c == chans) { for (i = 0; i < chans; i++) if (Get_IFD_Channel_Type(tif,i) != UINT16_TYPE) { fprintf(stderr,"Channel %d of %s is not 16-bit)\n",i,name); exit (1); } depth += 1; } Advance_Tiff(tif); } Rewind_Tiff(tif); if (NumChans + chans > maxchans) { maxchans = 1.2*(NumChans+chans) + 20; Images = (Array **) Guarded_Realloc(Images,sizeof(Array *)*maxchans,Program_Name()); } for (i = NumChans; i < NumChans + chans; i++) Images[i] = Make_Array_With_Shape(PLAIN_KIND,UINT16_TYPE,Coord3(depth,height,width)); depth = 0; while ( ! Tiff_EOF(tif)) { int w, h, c; Get_IFD_Shape(tif,&w,&h,&c); if (w == width && h == height && c == chans) { for (i = 0; i < chans; i++) { Array_Bundle plane = *(Images[NumChans+i]); Get_IFD_Channel(tif,i,Get_Array_Plane(&plane,depth)); } depth += 1; } Advance_Tiff(tif); } Close_Tiff(tif); NumChans += chans; return (maxchans); } else if (strcmp(getSuffix(name), "raw")==0 || strcmp(getSuffix(name), "v3draw")==0) { unsigned char* img; long* sz; img=0; sz=0; int rawLoadResult=loadRaw2Stack(name, &img, &sz, 2); if (rawLoadResult!=0) { fprintf(stderr, "Load of raw file failed\n"); exit(1); } if (sizeof(unsigned short)!=2) { fprintf(stderr, "Read_All_Channels() making false assumption that unsigned short is size=2\n"); exit(1); } unsigned short* rawp=(unsigned short*)img; width=sz[0]; height=sz[1]; depth=sz[2]; chans=sz[3]; printf("width=%d height=%d depth=%d chans=%d\n", width, height, depth, chans); if (chans < 2) { fprintf(stderr,"File %s does not contain at least 2 channels\n",name); exit (1); } if (NumChans + chans > maxchans) { maxchans = 1.2*(NumChans+chans) + 20; Images = (Array **) Guarded_Realloc(Images,sizeof(Array *)*maxchans,Program_Name()); } for (i = NumChans; i < NumChans + chans; i++) { Images[i] = Make_Array_With_Shape(PLAIN_KIND,UINT16_TYPE,Coord3(depth,height,width)); { int rx,ry,rz; uint16 *v; Indx_Type p; v = AUINT16(Images[i]); p = 0; long oc=(i-NumChans)*depth*height*width; for (rz=0;rz<depth;rz++) { long oz = rz*height*width; for (ry=0;ry<height;ry++) { long oy = ry*width; for (rx=0;rx<width;rx++) { long offset=oc + oz + oy + rx; unsigned short uv = *(rawp + offset); v[p++] = uv; } } } } } NumChans += chans; return (maxchans); } else { fprintf(stderr, "Do not recognize file suffix %s\n", getSuffix(name)); exit(1); } }
/* Method that presents the image arr with a grid overlap @param image: input image stack @param imgPara: input parameter @param name: name of the output file @param arr: image which should be plotted */ void ShowArray(RasterizedImage & img, ParaSet & imgPara, char * name, Array * arr) { Dimn_Type x, y, z; Array * a, * b; if (imgPara.grid == false){ Write_Image((char *) name, arr, DONT_PRESS); return; } // Conversion of the image arr into an RGB-Kind image if (arr->type != UINT8_TYPE){ b = Copy_Array(arr); Scale_Array_To_Range(b,ValU(0),ValU(255)); Convert_Array_Inplace(b,b->kind,UINT8_TYPE,8,0); } else { b = arr; } if (b->kind != RGB_KIND){ if (b == arr){ a = Convert_Array(b,RGB_KIND,b->type,b->scale,0); } else { a = Convert_Array_Inplace(b,RGB_KIND,b->type,b->scale,0); } } else { a = b; } // Plotting of the image grid if (a->ndims == 4) for (z = 0; z < img.GetDepth(); z++){ for (y = 0; y < img.GetGridY(); y++) { if (y % 50 == 0 && y != 0){ Draw_Line(a,&YELLOW,Coord3(z,y*imgPara.radius,0),Coord3(z,y*imgPara.radius,img.GetGridX()*imgPara.radius-1)); } else if (y % 10 == 0 && y != 0){ Draw_Line(a,&PURPLE,Coord3(z,y*imgPara.radius,0),Coord3(z,y*imgPara.radius,img.GetGridX()*imgPara.radius-1)); } else { Draw_Line(a,&CYAN,Coord3(z,y*imgPara.radius,0),Coord3(z,y*imgPara.radius,img.GetGridX()*imgPara.radius-1)); } for (x = 0; x < img.GetGridX(); x++){ if (x % 50 == 0 && x != 0){ Draw_Line(a,&YELLOW,Coord3(z,0,x*imgPara.radius),Coord3(z,img.GetGridY()*imgPara.radius-1,x*imgPara.radius)); } else if (x % 10 == 0 && x != 0){ Draw_Line(a,&PURPLE,Coord3(z,0,x*imgPara.radius),Coord3(z,img.GetGridY()*imgPara.radius-1,x*imgPara.radius)); } else { Draw_Line(a,&CYAN,Coord3(z,0,x*imgPara.radius),Coord3(z,img.GetGridY()*imgPara.radius-1,x*imgPara.radius)); } } } } else{ for (y = 0; y < img.GetGridY(); y++){ if (y % 50 == 0 && y != 0){ Draw_Line(a,&YELLOW,Coord2(y*imgPara.radius,0),Coord2(y*imgPara.radius,img.GetGridX()*imgPara.radius-1)); } else if (y % 10 == 0 && y != 0){ Draw_Line(a,&PURPLE,Coord2(y*imgPara.radius,0),Coord2(y*imgPara.radius,img.GetGridX()*imgPara.radius-1)); } else{ Draw_Line(a,&CYAN,Coord2(y*imgPara.radius,0),Coord2(y*imgPara.radius,img.GetGridX()*imgPara.radius-1)); } for (x = 0; x < img.GetGridX(); x++){ if (x % 50 == 0 && x != 0){ Draw_Line(a,&YELLOW,Coord2(0,x*imgPara.radius),Coord2(img.GetGridY()*imgPara.radius-1,x*imgPara.radius)); } else if (x % 10 == 0 && x != 0){ Draw_Line(a,&PURPLE,Coord2(0,x*imgPara.radius),Coord2(img.GetGridY()*imgPara.radius-1,x*imgPara.radius)); } else{ Draw_Line(a,&CYAN,Coord2(0,x*imgPara.radius),Coord2(img.GetGridY()*imgPara.radius-1,x*imgPara.radius)); } } } } Write_Image(name, a, DONT_PRESS); if (arr != a) Free_Array(a); }