/* smoothfill.cc Mark Jenkinson, FMRIB Image Analysis Group Copyright (C) 2001-2006 University of Oxford */ /* Part of FSL - FMRIB's Software Library http://www.fmrib.ox.ac.uk/fsl fsl@fmrib.ox.ac.uk Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance Imaging of the Brain), Department of Clinical Neurology, Oxford University, Oxford, UK LICENCE FMRIB Software Library, Release 5.0 (c) 2012, The University of Oxford (the "Software") The Software remains the property of the University of Oxford ("the University"). The Software is distributed "AS IS" under this Licence solely for non-commercial use in the hope that it will be useful, but in order that the University as a charitable foundation protects its assets for the benefit of its educational and research purposes, the University makes clear that no condition is made or to be implied, nor is any warranty given or to be implied, as to the accuracy of the Software, or that it will be suitable for any particular purpose or for use under any specific conditions. Furthermore, the University disclaims all responsibility for the use which is made of the Software. It further disclaims any liability for the outcomes arising from using the Software. The Licensee agrees to indemnify the University and hold the University harmless from and against any and all claims, damages and liabilities asserted by third parties (including claims for negligence) which arise directly or indirectly from the use of the Software or the sale of any products based on the Software. No part of the Software may be reproduced, modified, transmitted or transferred in any form or by any means, electronic or mechanical, without the express permission of the University. The permission of the University is not required if the said reproduction, modification, transmission or transference is done without financial return, the conditions of this Licence are imposed upon the receiver of the product, and all original and amended source code is included in any transmitted product. You may be held legally responsible for any copyright infringement that is caused or encouraged by your failure to abide by these terms and conditions. You are not permitted under this Licence to use this Software commercially. Use for which any financial return is received shall be defined as commercial use, and includes (1) integration of all or part of the source code or the Software into a product for sale or license by or on behalf of Licensee to third parties or (2) use of the Software or any derivative of it for research with the final aim of developing software products for sale or license to a third party or (3) use of the Software or any derivative of it for research with the final aim of developing non-software products for sale or license to a third party, or (4) use of the Software to provide any service to an external organisation for which payment is received. If you are interested in using the Software commercially, please contact Isis Innovation Limited ("Isis"), the technology transfer company of the University, to negotiate a licence. Contact details are: innovation@isis.ox.ac.uk quoting reference DE/9564. */ #include "utils/options.h" #include "miscmaths/miscmaths.h" #include "newimage/newimageall.h" #define _GNU_SOURCE 1 #define POSIX_SOURCE 1 using namespace Utilities; using namespace NEWMAT; using namespace MISCMATHS; using namespace NEWIMAGE; volume global_mask; //////////////////////////////////////////////////////////////////////////// // COMMAND LINE OPTIONS string title="smoothfill (Version 1.0)\nCopyright(c) 2012, University of Oxford (Mark Jenkinson)"; string examples="smoothfill -i inputimage -m mask -o smoothedresult"; Option verbose(string("-v,--verbose"), false, string("switch on diagnostic messages"), false, no_argument); Option help(string("-h,--help"), false, string("display this message"), false, no_argument); Option debug(string("--debug"), false, string("turn on debugging output"), false, no_argument); Option n_iter(string("-n,--niter"), 10, string("number of iterations"), false, requires_argument); Option maskname(string("-m,--mask"), string(""), string("filename for mask image"), true, requires_argument); Option outname(string("-o,--out"), string(""), string("filename for output (inverse warped) image"), true, requires_argument); Option inputname(string("-i,--in"), string(""), string("filename for input image"), true, requires_argument); //////////////////////////////////////////////////////////////////////////// // Optimisation functions // A temporary fix of including the std:: in front of all abs() etc // has been done for now using std::abs; bool estquadmin(float &xnew, float x1, float xmid, float x2, float y1, float ymid, float y2) { // Finds the estimated quadratic minimum's position float ad=0.0, bd=0.0, det=0.0; ad = (xmid - x2)*(ymid - y1) - (xmid - x1)*(ymid - y2); bd = -(xmid*xmid - x2*x2)*(ymid - y1) + (xmid*xmid - x1*x1)*(ymid - y2); det = (xmid - x2)*(x2 -x1)*(x1 - xmid); if ((fabs(det)>1e-15) && (ad/det < 0)) { // quadratic only has a maxima xnew = 0.0; return false; } if (fabs(ad)>1e-15) { xnew = -bd/(2*ad); return true; } else { // near linear condition -> get closer to an end point xnew = 0.0; return false; } return false; } float extrapolatept(float x1, float xmid, float x2) { // xmid must be between x1 and x2 // use the golden ratio (scale similar result) const float extensionratio = 0.3819660; float xnew; if (fabs(x2-xmid)>fabs(x1-xmid)) { xnew = extensionratio * x2 + (1 - extensionratio) * xmid; } else { xnew = extensionratio * x1 + (1 - extensionratio) * xmid; } return xnew; } float nextpt(float x1, float xmid, float x2, float y1, float ymid, float y2) { // x1 and x2 are the bounds, xmid is between them float xnew; bool quadok=false; quadok = estquadmin(xnew,x1,xmid,x2,y1,ymid,y2); // check to see that the quadratic result is in the range if ((!quadok) || (xnew < Min(x1,x2)) || (xnew > Max(x1,x2))) { xnew = extrapolatept(x1,xmid,x2); } return xnew; } void findinitialbound(float &x1, float &xmid, float &x2, float &y1, float &ymid, float &y2, float (*func)(const volume &), const volume &unitdir, const volume &pt) { const float extrapolationfactor = 1.6; const float maxextrap = extrapolationfactor*2; if (y1==0) y1 = (*func)(x1*unitdir + pt); if (ymid==0) ymid = (*func)(xmid*unitdir + pt); if (y1 y2) { // note: must maintain y1 >= ymid // cout << " <" << Min(x1,x2) << "," << xmid // << "," << Max(x1,x2) << ">" << endl; maxx2 = xmid + maxextrap*(x2 - xmid); quadok = estquadmin(newx2,x1,xmid,x2,y1,ymid,y2); if ((!quadok) || ((newx2 - x1)*dir<0) || ((newx2 - maxx2)*dir>0)) { newx2 = xmid + extrapolationfactor*(x2-x1); } newy2 = (*func)(newx2*unitdir + pt); if ((newx2 - xmid)*(newx2 - x1)<0) { // newx2 is between x1 and xmid if (newy2 < ymid) { // found a bracket! x2 = xmid; y2 = ymid; xmid = newx2; ymid = newy2; break; } else { // can use newx2 as a new value for x1 (as newy2 >= ymid) x1 = newx2; y1 = newy2; } } else { // newx2 is between xmid and maxx2 if (newy2 > ymid) { // found a bracket! x2 = newx2; y2 = newy2; break; } else if ((newx2 - x2)*dir<0) { // newx2 closer to xmid than old x2 x1 = xmid; y1 = ymid; xmid = newx2; ymid = newy2; } else { x1 = xmid; y1 = ymid; xmid = x2; ymid = y2; x2 = newx2; y2 = newy2; } } } if ( (y2 &pt, const volume& unitdir, float unittol, int &iterations_done, float (*func)(const volume&), int max_iter, float init_value, float boundguess) { // Golden Search Routine // Must pass in the direction vector in N-space (dir), the initial // N-dim point (pt), the acceptable tolerance (tol) and other // stuff // Note that the length of the direction vector is unimportant // Pass in previous costfn value as init_value, if known, otherwise // pass in 0.0 and it will force the calculation // Unlike the version in optimise.cc the boundguess is in absolute // units, not in units of unittol float y1,y2,ymid; float x1,x2,xmid; // set up initial points xmid = 0.0; x1 = boundguess; // initial guess (bound) if (init_value==0.0) ymid = (*func)(xmid*unitdir + pt); else ymid = init_value; y1 = (*func)(x1*unitdir + pt); findinitialbound(x1,xmid,x2,y1,ymid,y2,func,unitdir,pt); if (verbose.value()) { cout << "BOUND = (" << x1 << "," << y1 << ") "; cout << "(" << xmid << "," << ymid << ") "; cout << "(" << x2 << "," << y2 << ")" << endl; } float min_dist = 0.1 * unittol; float xnew, ynew; int it=0; while ( ((++it)<=max_iter) && (fabs((x2-x1)/unittol)>1.0) ) { // cout << " [" << Min(x1,x2) << "," << Max(x1,x2) << "]" << endl; if (it>0) { xnew = nextpt(x1,xmid,x2,y1,ymid,y2); } else { xnew = extrapolatept(x1,xmid,x2); } float dirn=1.0; if (x2 ptlist; vec3p v, newv; // initial pass for (int z=0; z<=im.maxz(); z++) { for (int y=0; y<=im.maxy(); y++) { for (int x=0; x<=im.maxx(); x++) { if (ext_edge(mask,x,y,z)) { v.x=x; v.y=y; v.z=z; v.n=2; ptlist.push_front(v); } } } } while (!ptlist.empty()) { v = ptlist.back(); ptlist.pop_back(); if (mask(v.x,v.y,v.z)<=(float) 0.5) { // only do it if the voxel is still unset im(v.x,v.y,v.z)=dilateval(im,mask,v.x,v.y,v.z); mask(v.x,v.y,v.z)=(float)v.n; // check neighbours and add them to the list if necessary for (int zz=Max(0,v.z-1); zz<=Min(v.z+1,im.maxz()); zz++) { for (int yy=Max(0,v.y-1); yy<=Min(v.y+1,im.maxy()); yy++) { for (int xx=Max(0,v.x-1); xx<=Min(v.x+1,im.maxx()); xx++) { if (ext_edge(mask,xx,yy,zz)) { newv.x=xx; newv.y=yy; newv.z=zz; newv.n=v.n+1; ptlist.push_front(newv); } } } } } } return 0; } /////////////////////////////////////////////////////////////////////////// float calc_cost(const volume& invol) { // Cost = Laplacian of image (only counting areas _outside_ of the mask - the portion to be filled) float Lap=0.0, d2wdx2=0.0; // now add in square Laplacian regularisation for (int z=invol.minz(); z<=invol.maxz(); z++) { for (int y=invol.miny(); y<=invol.maxy(); y++) { for (int x=invol.minx(); x<=invol.maxx(); x++) { if (global_mask(x,y,z)<0.5) { if ((x>invol.minx()) && (xinvol.miny()) && (yinvol.minz()) && (z mask; mask=global_mask; dilall_extra(invol,mask); save_volume(invol,fslbasename(outname.value())+"_init"); float cost = calc_cost(invol); cout << "Cost is " << cost << endl; // generate subsampled versions of the dilated volume for spatial blurring (distance dependent) volume vol2, vol4, vol8, vol16, vol32; invol.setextrapolationmethod(extraslice); vol2 = subsample_by_2(invol,true); vol4 = subsample_by_2(vol2,true); vol8 = subsample_by_2(vol4,true); vol16 = subsample_by_2(vol8,true); vol32 = subsample_by_2(vol16,true); save_volume(vol2,fslbasename(outname.value())+"_vol2"); save_volume(vol32,fslbasename(outname.value())+"_vol32"); for (int z=0; z<=invol.maxz(); z++) { for (int y=0; y<=invol.maxy(); y++) { for (int x=0; x<=invol.maxx(); x++) { // interpolate between subsampled versions of the input image (gives smooth, but spatially non-uniform, blurring) float idx=log(mask(x,y,z))/log(2.0); // values in mask encode "distance" idx-=1; // so that it only starts interpolating at higher values if (idx>4) idx=4; if (idx>0) { int idx1=(int) floor(idx); int idx2=(int) ceil(idx); float val1=0, val2=0; if (idx1==0) { val1=vol2.interpolate(x/2.0,y/2.0,z/2.0); val2=vol4.interpolate(x/4.0,y/4.0,z/4.0); } if (idx2==0) val2=val1; // in case idx==0 if (idx1==1) { val1=vol4.interpolate(x/4.0,y/4.0,z/4.0); val2=vol8.interpolate(x/8.0,y/8.0,z/8.0); } if (idx1==2) { val1=vol8.interpolate(x/8.0,y/8.0,z/8.0); val2=vol16.interpolate(x/16.0,y/16.0,z/16.0); } if (idx1==3) { val1=vol16.interpolate(x/16.0,y/16.0,z/16.0); val2=vol32.interpolate(x/32.0,y/32.0,z/32.0); } if (idx1>=4) { val1=vol32.interpolate(x/32.0,y/32.0,z/32.0); val2=val1; } invol(x,y,z) = val1 + (idx-idx1)*(val2-val1); } } } } cost = calc_cost(invol); cout << "Cost is " << cost << endl; if (verbose.value()) { cout << "After gradient descent" << endl; } save_volume(invol,outname.value()); save_volume(mask,fslbasename(outname.value())+"_idxmask"); return(EXIT_SUCCESS); } int main(int argc, char *argv[]) { Tracer tr("main"); OptionParser options(title, examples); try { options.add(inputname); options.add(outname); options.add(maskname); options.add(n_iter); options.add(debug); options.add(verbose); options.add(help); int nparsed = options.parse_command_line(argc, argv); if (nparsed < argc) { for (; nparsed invol; try { read_volume_hdr_only(invol,inputname.value()); } catch(...) { cerr << "smoothfill: Problem reading input image " << inputname.value() << endl; exit(EXIT_FAILURE); } return do_work(); }