ThreeB 1.1
multispot5_headless.cc
Go to the documentation of this file.
00001 #include <tag/printf.h>
00002 #undef make_tuple
00003 
00004 #include <tr1/tuple>
00005 #include <algorithm>
00006 #include <climits>
00007 #include <iomanip>
00008 #include <map>
00009 #include <cvd/image_io.h>
00010 #include <cvd/image_convert.h>
00011 #include <cvd/morphology.h>
00012 #include <cvd/connected_components.h>
00013 #include <cvd/draw.h>
00014 #include <cvd/vector_image_ref.h>
00015 
00016 #include <gvars3/instances.h>
00017 
00018 #include "storm_imagery.h"
00019 #include "multispot5.h"
00020 #include "multispot5_place_choice.h"
00021 #include "utility.h"
00022 
00023 using namespace std;
00024 using namespace std::tr1;
00025 using namespace CVD;
00026 using namespace GVars3;
00027 using namespace TooN;
00028 
00029 
00030 
00031 vector<vector<ImageRef> > get_regions(const SubImage<double>& log_ratios)
00032 {
00033     gvar3<double> radius("radius", 0, 1);
00034 
00035     //Set the liklihood ratio threshold/spot density prior
00036     //same thing.
00037     double threshold = GV3::get<double>("threshold", 0, -1);
00038     int edge = GV3::get<int>("edge", 0, -1);
00039 
00040 
00041     //Threshold image
00042     Image<byte> thresholded(log_ratios.size(), 0);
00043     for(int r=0; r < thresholded.size().y; r++)
00044         for(int c=0; c < min(thresholded.size().x, edge); c++)
00045             thresholded[r][c] = 255 * (log_ratios[r][c] > threshold);
00046     
00047     //Dilate
00048     Image<byte> dilated = morphology(thresholded, getDisc(*radius), Morphology::BinaryDilate<byte>());
00049 
00050     transform(dilated.begin(), dilated.end(), dilated.begin(), bind1st(multiplies<int>(), 255));
00051     
00052     //Connected components of dilated image
00053     vector<ImageRef> fg;
00054     for(int r=0; r < thresholded.size().y; r++)
00055         for(int c=0; c < min(thresholded.size().x, edge); c++)
00056             if(dilated[r][c])
00057                 fg.push_back(ImageRef(c, r));
00058 
00059     vector<vector<ImageRef> > regions;
00060     connected_components(fg, regions);
00061 
00062     return regions;
00063 }
00064 
00065 void mmain(int argc, char** argv)
00066 {
00067     GUI.LoadFile("multispot5.cfg");
00068     int lastarg = GUI.parseArguments(argc, argv);
00069     if(lastarg >= argc)
00070     {   
00071         cerr << "Specify the images to load\n";
00072         exit(1);
00073     }
00074     vector<string> files(argv + lastarg, argv + argc);
00075     
00076     //Save this now since the de-checkpointing code will kl0bber it 
00077     //when it reloads the gvars
00078     string save_spots_file = GV3::get<string>("save_spots", "", -1);
00079 
00080     string checkpoint_file = GV3::get<string>("load_checkpoint", "", 1);
00081 
00082     if(checkpoint_file != "")
00083     {
00084         //Load and de-checkpointing
00085         ifstream chk;
00086         open_or_die(chk, checkpoint_file);
00087         
00088         StateParameters p;
00089 
00090         try{
00091             p = parse_log_file(chk);
00092         }
00093         catch(LogFileParseError e)
00094         {
00095             cerr << "SI TEH FUX0R11ONEone!oneleven: " << e.what << endl;
00096             exit(1);
00097         }
00098         
00099         vector<Image<float> > ims = load_and_normalize_images(files);
00100 
00101         //Restore kl0bbered variable
00102         GV3::get<string>("save_spots") = save_spots_file;
00103         
00104         ofstream save_spots;
00105         open_or_die(save_spots, save_spots_file);
00106         
00107         fit_spots_new(ims, p, save_spots, *null_graphics());
00108 
00109     }
00110      
00111     vector<Image<float> > ims = load_and_normalize_images(files);
00112 
00113     //Load the log_ratios image.
00114     //We will use this as a starting point for searching for spots.
00115     Image<double> log_ratios;
00116     try
00117     {
00118         log_ratios = img_load(GV3::get<string>("log_ratios", "", -1));
00119     }
00120     catch(Exceptions::All e)
00121     {
00122         cerr << "Error loading " << GV3::get<string>("log_ratios", "") << ": " << e.what << endl;
00123         exit(1);
00124     }
00125     
00126     
00127     gvar3<int> cluster_to_show("cluster_to_show", 0, -1);
00128     gvar3<int> use_largest("use_largest", 0, 1);
00129 
00130     vector<vector<ImageRef> > regions;
00131 
00132     regions = get_regions(log_ratios);
00133     if(regions.size() == 0)
00134     {
00135         cerr << "There are no regions!\n";
00136 
00137         ofstream save_spots;
00138         open_or_die(save_spots, save_spots_file);
00139         save_spots << "NOREGIONS\n";
00140 
00141         exit(1);
00142     }
00143     
00144     if(*use_largest && !regions.empty())
00145     {
00146         *cluster_to_show=0;
00147         for(unsigned int i=1; i < regions.size(); i++)
00148             if(regions[i].size() > regions[*cluster_to_show].size())
00149                 *cluster_to_show = i;
00150                 
00151     }
00152     else
00153         *cluster_to_show = max(min(*cluster_to_show, (int)regions.size() - 1), 0);
00154 
00155     
00156     auto_ptr<FitSpotsGraphics> gr = null_graphics();
00157     place_and_fit_spots(ims, regions[*cluster_to_show], log_ratios, save_spots_file, *gr);
00158 }
00159 
00160     
00161 int main(int argc, char** argv)
00162 {
00163     try{
00164         mmain(argc, argv);
00165     }
00166     catch(Exceptions::All e)
00167     {
00168         cerr << "Fatal error: " << e.what << endl;
00169     }
00170 }