Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

thresholding.cpp

Go to the documentation of this file.
00001 // $Id: thresholding.cpp,v 1.18 2003/01/16 18:36:52 sarang Exp $
00002 //
00003 // OpenVL - A Library for working with volumetric datasets.
00004 //          http://openvl.sf.net
00005 //
00006 // Copyright (C) 2000-2002  Sarang Lakare <sarang#users.sf.net>
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems to openvl-devel@lists.sf.net or
00024 // at the OpenVL website.
00025 //
00026 
00027 #include <iostream>
00028 
00029 #include "vlvarlist.h"
00030 #include "vlvolume.h"
00031 #include "vlgenericfactory.h"
00032 #include "vlvoliterator.h"
00033 
00034 #include "thresholding.h"
00035 
00036 
00037 typedef vlGenericFactory<vlVolProcessorThresh> vlVolProcessorThreshFactory;
00038 VL_EXPORT_COMPONENT_FACTORY( thresholder, vlVolProcessorThreshFactory );
00039 
00043 vlVolProcessorThresh::vlVolProcessorThresh()
00044 {
00045   infoRef().setVersion(0, 1, 0);
00046   infoRef().setName("Thresholder");
00047   infoRef().setService("Thresholder", "Performs thresholding.");
00048   infoRef().addAuthor("Sarang Lakare");
00049   infoRef().setCopyrightText("2000-2002 Sarang Lakare");
00050 }
00051 
00052 
00056 vlVolProcessorThresh::~vlVolProcessorThresh()
00057 {
00058 }
00059 
00060 
00061 void vlVolProcessorThresh::init()
00062 {
00063   configRef().clear();
00064   configRef().add(vol()->dataType(), "thresholdHigh", "Threshold Value - High");
00065   configRef().add(vol()->dataType(), "thresholdLow", "Threshold Value - Low");
00066   vlVolume *vol;
00067   configRef().add(vol, "maskVolume", "Mask volume.");
00068   uint8 inMask(0xFF), outMask(0x00);
00069   configRef().add(inMask, "insideMask", "Mask for voxels inside the region.", true);
00070   configRef().add(outMask, "outsideMask", "Mask for voxels outside the region.", true);
00071 }
00072 
00073 template <>
00074 bool vlVolProcessorThresh::runT<uint8>(uint8 & dummy)
00075 {
00076   return (thresholding(dummy));
00077 }
00078 
00079 template <>
00080 bool vlVolProcessorThresh::runT<int8>(int8 & dummy)
00081 {
00082   return (thresholding(dummy));
00083 }
00084 
00085 template <>
00086 bool vlVolProcessorThresh::runT<uint16>(uint16 & dummy)
00087 {
00088   return (thresholding(dummy));
00089 }
00090 
00091 template <>
00092 bool vlVolProcessorThresh::runT<int16>(int16 & dummy)
00093 {
00094   return (thresholding(dummy));
00095 }
00096 
00097 
00098 template <>
00099 bool vlVolProcessorThresh::runT<uint32>(uint32 & dummy)
00100 {
00101   return (thresholding(dummy));
00102 }
00103 
00104 
00105 template <>
00106 bool vlVolProcessorThresh::runT<int32>(int32 & dummy)
00107 {
00108   return (thresholding(dummy));
00109 }
00110 
00111 //template <>
00112 //bool vlVolProcessorThresh::runT<float>(float & dummy)
00113 //{
00114 //  return (thresholding(dummy));
00115 //}
00116 //
00117 //template <>
00118 //bool vlVolProcessorThresh::runT<double>(double & dummy)
00119 //{
00120 //  return (thresholding(dummy));
00121 //}
00122 
00123 
00124 bool vlVolProcessorThresh::run()
00125 {
00126   if(!vol())
00127     return (false);
00128   
00129   bool retValue(false);
00130 
00131   callFunctionOnDataTypeNoArgs(vol()->dataType(), retValue, runT);
00132 
00133   return (retValue);
00134 }
00135 
00136 
00137 template <class DataType>
00138 bool vlVolProcessorThresh::runT(DataType & dummy)
00139 {
00140   std::cout << "Cannot apply thresholding on this datatype [" << typeid(DataType).name() << "]" << std::endl;
00141   return (false);
00142 }
00143 
00144 
00145 template <class DataType>
00146 bool vlVolProcessorThresh::thresholding(DataType & dummy)
00147 {
00148   std::cout << "About to run thresholding.. " << std::endl;
00149 
00150   vlVolume *maskVol;
00151 
00152   if(!configRef().get("maskVolume", maskVol)) {
00153     std::cout << "ERROR : No mask volume specified." << std::endl;
00154     return (false);
00155   }
00156 
00157   if(vol()->dim() != maskVol->dim()) {
00158     std::cout << "ERROR : Mask volume dimensions not same as the volume dimension." << std::endl;
00159     return (false);
00160   }
00161 
00162   if(maskVol->dataType() != UnsignedInt8) {
00163     std::cout << "ERROR : Mask volume has to be of unsigned int8 data type." << std::endl;
00164     return (false);
00165   }
00166   
00167   DataType threshLow, threshHigh;
00168   uint8 inVoxelMask(0x00);
00169   uint8 outVoxelMask(0x00);
00170 
00171   uint64 regionVoxelCount(0); // count the number of voxels which will belong to te region
00172 
00173   if(!configRef().get("thresholdHigh", threshHigh)) {
00174     std::cout << "ERROR : Higher threshold not specified." << std::endl;
00175     return (false);
00176   }
00177 
00178   if(!configRef().get("thresholdLow", threshLow)) {
00179     std::cout << "ERROR : Lower threshold not specified." << std::endl;
00180     return (false);
00181   }
00182 
00183   configRef().get("insideMask", inVoxelMask);
00184   configRef().get("outsideMask", outVoxelMask);
00185 
00186   // make sure thresholds are proper
00187   if(threshHigh < threshLow) {
00188     std::cout << "ERROR : thresholdHigh < thresholdLow. This is not allowed." << std::endl;
00189     return (false);
00190   }
00191   
00192   std::cout << "Threshold value(s) set to : " << (int)threshLow << " -- " << (int)threshHigh << std::endl;
00193 
00194   vlVolIter<DataType> iter(vol());
00195   vlVolIter<uint8> maskIter(maskVol);
00196 
00197   do {
00198     DataType voxelValue = iter.get();
00199 
00200     if (threshLow <= voxelValue && voxelValue <= threshHigh) {
00201       // inside the region
00202       ++regionVoxelCount;
00203       maskIter.set(inVoxelMask);
00204     } else { // outside the region
00205       maskIter.set(outVoxelMask);
00206     }
00207   } while(iter.nextXYZ() && maskIter.nextXYZ());
00208 
00209   resultsRef().add(regionVoxelCount, "voxelCount", "Count of the voxels in the threshold interval", true);
00210 
00211   std::cout << "Total number of voxels in the region : " << regionVoxelCount << std::endl;
00212 
00213   std::cout << "\n---- End of Thresholding ---- " << std::endl;
00214 
00215   return (true);
00216 }
00217 

Generated on Fri Mar 18 11:33:13 2005 for OpenVL by doxygen 1.3.3