From c146920add5369b4407427e109d5caf78df08f1c Mon Sep 17 00:00:00 2001
From: Mark Jenkinson <mark@fmrib.ox.ac.uk>
Date: Fri, 30 Sep 2005 18:12:01 +0000
Subject: [PATCH] Added histogram feature

---
 fslstats.cc | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)

diff --git a/fslstats.cc b/fslstats.cc
index 25901f9..1e817a6 100644
--- a/fslstats.cc
+++ b/fslstats.cc
@@ -2,7 +2,7 @@
 
     Mark Jenkinson, FMRIB Image Analysis Group
 
-    Copyright (C) 2003 University of Oxford  */
+    Copyright (C) 2003-2005 University of Oxford  */
 
 /*  CCOPYRIGHT  */
 
@@ -44,6 +44,8 @@ void print_usage(const string& progname) {
   cout << "-P <n>       : output nth percentile (for nonzero voxels)" << endl;
   cout << "-a           : use absolute values of all image intensities"<< endl;
   cout << "-k <mask>    : use the specified image (filename) for masking - overrides lower and upper thresholds" << endl;
+  cout << "-h <nbins>   : output a histogram (for the thresholded/masked voxels only) with nbins" << endl; 
+  cout << "-H <nbins> <min> <max>   : output a histogram (for the thresholded/masked voxels only) with nbins and histogram limits of min and max" << endl; 
   cout << endl;
   cout << "Note - thresholds are not inclusive ie lthresh<allowed<uthresh" << endl;
 }
@@ -408,6 +410,60 @@ int fmrib_main_float(int argc, char* argv[])
 	vol = vin * mask; 
       }
       cout << vol.percentile((float) n/100.0,masknz) << " ";
+    } else if (sarg=="-h") {
+      float n;
+      narg++;
+      if (narg<argc) {
+        n = atof(argv[narg]);
+      } else {
+	cerr << "Must specify the number of bins" << endl;
+	exit(2);
+      }
+      int nbins = (int) n;
+      if (nbins<1) {
+    	cerr << "Must specify at least 1 bin" << endl;
+    	exit(1);
+      }
+      if (masks_used) {
+	cout << vol.histogram(nbins,vol.min(),vol.max(),mask) << " ";
+      } else {
+	cout << vol.histogram(nbins,vol.min(),vol.max()) << " ";
+      }
+   } else if (sarg=="-H") {
+      float n;
+      narg++;
+      if (narg<argc) {
+        n = atof(argv[narg]);
+      } else {
+	cerr << "Must specify the number of bins" << endl;
+	exit(2);
+      }
+      int nbins = (int) n;
+      if (nbins<1) {
+    	cerr << "Must specify at least 1 bin" << endl;
+    	exit(1);
+      }
+      float min=0;
+      narg++;
+      if (narg<argc) {
+        min = atof(argv[narg]);
+      } else {
+	cerr << "Must specify the histogram minimum intensity" << endl;
+	exit(2);
+      }
+      float max=0;
+      narg++;
+      if (narg<argc) {
+        max = atof(argv[narg]);
+      } else {
+	cerr << "Must specify the histogram maximum intensity" << endl;
+	exit(2);
+      }
+      if (masks_used) {
+	cout << vol.histogram(nbins,min,max,mask) << " ";
+      } else {
+	cout << vol.histogram(nbins,min,max) << " ";
+      }
     } else {
 	cerr << "Unrecognised option: " << sarg << endl;
 	exit(3);
-- 
GitLab