Esempio n. 1
0
   BSONObj qgmMerge( const BSONObj &left, const BSONObj &right )
   {
      PD_TRACE_ENTRY( SDB__QGMMERGE ) ;
      BSONObj obj ;
      BSONObjBuilder builder ;
      try
      {
         BSONObjIterator itr1( left ) ;
         while ( itr1.more() )
         {
            builder.append( itr1.next() ) ;
         }

         BSONObjIterator itr2( right ) ;
         while ( itr2.more() )
         {
            builder.append( itr2.next() ) ;
         }

         obj = builder.obj() ;
      }
      catch ( std::exception &e )
      {
         PD_LOG( PDERROR, "unexpected err happened:%s",
                 e.what() ) ;
      }

      PD_TRACE_EXIT( SDB__QGMMERGE ) ;
      return obj ;
   }
void VolumeProcess::SetInput(ImageType::Pointer inImage)
{
	m_inputImage = inImage;
    //m_outputImage = inImage;	//THIS IS NOT CORRECT IT WILL CAUSE INPUT IMAGE TO CHANGE

	//Init the output image to the input image:
	m_outputImage = ImageType::New();
	m_outputImage->SetRegions( m_inputImage->GetLargestPossibleRegion() );
	m_outputImage->Allocate();
	m_outputImage->FillBuffer(0);
	itk::ImageRegionIterator< ImageType > itr1( m_inputImage, m_inputImage->GetLargestPossibleRegion() );
	itk::ImageRegionIterator< ImageType > itr2( m_outputImage, m_outputImage->GetLargestPossibleRegion() );
	for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2)
	{
		itr2.Set( itr1.Get() );
	}
}
Esempio n. 3
0
//This function removes all objects that are <= minObjSize from the foreground.
//The foreground remains grayscale after this filter
void Preprocess::RemoveConnectedComponents(int minObjSize)
{
	typedef itk::Image< unsigned short, 3 > ShortImageType;
	typedef itk::ConnectedComponentImageFilter< ImageType3D, ShortImageType > CCFilterType;
	typedef itk::RelabelComponentImageFilter< ShortImageType, ShortImageType > RelabelType;

	CCFilterType::Pointer ccfilter = CCFilterType::New();
	RelabelType::Pointer relabel = RelabelType::New();
	
	ccfilter->SetInput( myImg );
	ccfilter->FullyConnectedOn();

	relabel->SetInput( ccfilter->GetOutput() );
	relabel->SetMinimumObjectSize( minObjSize );
	relabel->InPlaceOn();

	try
    {
		relabel->Update();
    }
    catch( itk::ExceptionObject & excep )
    {
		std::cerr << "Relabel: exception caught !" << std::endl;
		std::cerr << excep << std::endl;
		return;
    }
	
	unsigned short initNumObjects = ccfilter->GetObjectCount();
	unsigned short finalNumObjects = relabel->GetNumberOfObjects();
	unsigned short removedNumObjects = initNumObjects - finalNumObjects;
	std::cout << "Removed " << removedNumObjects << " of " << initNumObjects << " objects...";

	ShortImageType::Pointer ccImage = relabel->GetOutput();

	//Use connected component image as a mask:
	itk::ImageRegionIterator< ShortImageType > itr1( ccImage, ccImage->GetLargestPossibleRegion() );
	itk::ImageRegionIterator< ImageType3D > itr2( myImg, myImg->GetLargestPossibleRegion() );
	for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2)
	{
		if(itr1.Get() == 0)
		{
			itr2.Set( 0 );
		}
	}
}
//This function removes all objects that are <= minObjSize from the foreground.
//The foreground remains grayscale after this filter
bool VolumeProcess::MaskSmallConnComp(int minObjSize)
{
	typedef itk::Image< unsigned short, Dimension > ShortImageType;
	typedef itk::ConnectedComponentImageFilter< ImageType, ShortImageType > CCFilterType;
	typedef itk::RelabelComponentImageFilter< ShortImageType, ShortImageType > RelabelType;

	CCFilterType::Pointer ccfilter = CCFilterType::New();
	RelabelType::Pointer relabel = RelabelType::New();
	
	ccfilter->SetInput (m_outputImage);
	ccfilter->FullyConnectedOn();

	relabel->SetInput( ccfilter->GetOutput() );
	relabel->SetMinimumObjectSize( minObjSize );
	relabel->InPlaceOn();

	try
    {
		relabel->Update();
    }
    catch( itk::ExceptionObject & excep )
    {
		std::cerr << "Relabel: exception caught !" << std::endl;
		std::cerr << excep << std::endl;
		return false;
    }
	
	unsigned short numObjects = relabel->GetNumberOfObjects();
	if(debug)
		std::cerr << "Connected components = " << numObjects << std::endl;

	ShortImageType::Pointer ccImage = relabel->GetOutput();

	//Use connected component image as a mask:
	itk::ImageRegionIterator< ShortImageType > itr1( ccImage, ccImage->GetLargestPossibleRegion() );
	itk::ImageRegionIterator< ImageType > itr2( m_outputImage, m_outputImage->GetLargestPossibleRegion() );
	for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2)
	{
		if(itr1.Get() == 0)
		{
			itr2.Set( 0 );
		}
	}
	return true;
}
bool VolumeProcess:: RunFillingZeroOnBouandary(int Bx,int By,int Bz)
{
	ImageType::Pointer tempImg = ImageType::New();
	tempImg->SetRegions( m_outputImage->GetLargestPossibleRegion() );
	tempImg->Allocate();
	tempImg->FillBuffer(0);

	ImageType::RegionType fullRegion = m_outputImage->GetBufferedRegion();

	int numColumns = fullRegion.GetSize(0);
	int numRows = fullRegion.GetSize(1);
	int numStacks = fullRegion.GetSize(2);
	int i, j,k; 

	itk::ImageRegionIteratorWithIndex< ImageType > itr( m_outputImage, fullRegion );

	for(itr.GoToBegin(); !itr.IsAtEnd(); ++itr)
	{
		
		ImageType::IndexType index = itr.GetIndex();
	    ImageType::PixelType pix = m_outputImage->GetPixel(index);
        i = index[0];
		j = index[1];
		k = index[2];
		if (i < Bx || i > numColumns -Bx || j < By || j > numRows-By ||k <Bz ||k > numStacks-Bz )
		    tempImg->SetPixel(itr.GetIndex(), 0);
		else 
			tempImg->SetPixel(itr.GetIndex(), pix);
     }
		//Copy temp img back to image 
	itk::ImageRegionIterator< ImageType > itr1( tempImg, tempImg->GetLargestPossibleRegion());
	itk::ImageRegionIterator< ImageType > itr2( m_outputImage, m_outputImage->GetLargestPossibleRegion());
	for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2)
	{
	  itr2.Set( itr1.Get() );
	}

	if(debug)
		std::cerr << "RunFillingZero Done" << std::endl;

	return true;
  
}
Esempio n. 6
0
int main(int argc, char* argv[])
{
    std::vector<std::string> args;
    bool appendMode = false;
    int retval = 0;
    for (int i = 0; i < argc; i++)
    {
    	if (strcmp(argv[i], "-a") == 0)
            appendMode = true;
        else
            args.push_back(argv[i]);
    }

    if ((args.size() < 3) || (args.size() > 4))
    {
        std::cout << "Usage: processEventLog [-a] <path> <out> [tz-offset-hours]" << std::endl;
        return 1;
    }
    try
    {

        boost::filesystem::path path(args[1]);
        if (!boost::filesystem::exists(path))
        {
            std::cout << args[1] << " does not exist." << std::endl;
            return 2;
        }

        if (!appendMode && !boost::filesystem::is_directory(path))
        {
            std::cout << args[1] << " must be a directory." << std::endl;
            return 3;
        }

        std::ofstream out(args[2].c_str(), appendMode ? std::ofstream::app : std::ofstream::trunc );
        if (!out.is_open())
        {
            std::cout << "failed to open output file " << args[2] << std::endl;
            return 4;
        }

        int hrsOffset = 0; 
        if (args.size() > 3)
            hrsOffset = atoi(args[3].c_str());

        result_t results;

        if (!appendMode)
        {
            boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end
            for ( boost::filesystem::directory_iterator itr( path );
                itr != end_itr;
                ++itr )
            {
                if ( boost::filesystem::is_directory(itr->status()) )
                {
                    for ( boost::filesystem::directory_iterator itr2( *itr );
                        itr2 != end_itr;
                        ++itr2 )
                    {
                        if (!boost::filesystem::is_regular_file(*itr2))
                            continue;
                        processFile(itr2->path(), hrsOffset, results);
                    }
                }
            }
            out << "DATE\t" << "TIME\t" << "relay" << '\t' << "target"
                 << '\t' << "inside" << '\t' << "humidity" << '\t' << "coded\t" 
                 << "FILE" << std::endl;
        }
        else
            processFile(path, hrsOffset, results);

        boost::posix_time::time_facet *facet =
		new boost::posix_time::time_facet("%Y/%m/%d %H:%M:%S");
        std::ostringstream date_ss;
        date_ss.imbue(std::locale(date_ss.getloc(), facet));
        for (
            result_t::const_iterator resItor = results.begin();
            resItor != results.end();
            resItor++)
        {
            date_ss.str("");
            date_ss << resItor->first;
            const Event &e = resItor->second;
            out << date_ss.str() << "\t" <<
                e.relay() << '\t' <<
                e.target() << '\t' <<
                e.temperature() << '\t' <<
                e.humidity() << '\t' <<
                e.relayCoded() << '\t' <<
                e.filename() <<
                std::endl;
            int insideTemp = (int)(e.temperature() + 0.5);
            if ((insideTemp >= 45) && (insideTemp <= 90))
                retval = insideTemp;
        }

    }
    catch (const std::exception &e)
    {
        std::cout << "Didn't work because " << e.what() << std::endl;
    }
    return retval;
}
int
fix_omap(int argc, char **argv)
{
	typedef boost::filesystem3::directory_iterator directory_iterator;
	// Get file paths
	if(argc < 4)
		throw std::runtime_error("Path to second occluded oMap directory is required as third arg\n");
			
	if(argc < 3)
		throw std::runtime_error("Path to occluded oMap directory is required as second arg\n");
		
	if(argc < 2)
		throw std::runtime_error("Path to oMap directory is required as first arg\n");
	
	std::string dir_omap(argv[1]);
	std::string dir_omap_occ(argv[2]);
	std::string dir_omap_occ_2(argv[3]);
	
	for (directory_iterator itr(dir_omap); itr!=directory_iterator(); ++itr)
   {
   	// Get first txt file
   	std::string curr_ext(itr->path().extension().string());
   	if( curr_ext.compare(".txt") == 0 )
   	{
   		std::string curr_stem( itr->path().stem().string() );
   		std::string curr_name( itr->path().filename().string() );
   		
   		for (directory_iterator itr2(dir_omap_occ); itr2!=directory_iterator(); ++itr2)
   		{
   			std::string curr_occ_name( itr2->path().filename().string() );
   			if( curr_occ_name.compare(curr_name) == 0)
   			{
   				for (directory_iterator itr3(dir_omap_occ_2); itr3!=directory_iterator(); ++itr3)
   				{
   					std::string curr_occ_2_name( itr3->path().filename().string() );
   					if( curr_occ_2_name.compare(curr_name) == 0)
   					{
   					
   					   std::cout << curr_occ_name << std::endl;
   				
							// Open files
							std::ifstream f1, f2, f3;
							std::ofstream f4;
							io_utils::open_in_file( f1, itr->path().string() );
							io_utils::open_in_file( f2, itr2->path().string() );
							io_utils::open_in_file( f3, itr3->path().string() );
							io_utils::open_out_file( f4, "./" + curr_stem + "_combined.txt" );
						
							if( !f1 )
								throw std::runtime_error("Cannot open oMap file " + curr_stem + "\n");
							if( !f2 )
								throw std::runtime_error("Cannot open occ file " + curr_stem + "\n");
							if( !f2 )
								throw std::runtime_error("Cannot open occ2 file " + curr_stem + "\n");								
							if( !f4 )
								throw std::runtime_error("Cannot open out file...\n");
							
							// Combine them
							int cntr = 0;
							int vp1 = -1, vp2 = -1, vp3 = -1;
							std::string st1, st2, st3;
							double sc1, sc2, sc3;
					
							while( (f1 >> vp1) && (f1 >> st1) && (f1 >> sc1) )
							{
								if( vp1 == cntr )
									f4 << vp1 << " " << st1 << " " << sc1 << std::endl;
								else
								{
									if(vp2 != -1)
									{
										f4 << vp2 << " " << st2 << " " << sc2 << std::endl;
									}

									while( (f2 >> vp2) && (f2 >> st2) && (f2 >> sc2) )
									{
									
										if(vp2 == cntr)
											f4 << vp2 << " " << st2 << " " << sc2 << std::endl;
										else
										{
											if(vp3 != -1)
											{
												f4 << vp3 << " " << st3 << " " << sc3 << std::endl;
											}
											
											while( (f3 >> vp3) && (f3 >> st3) && (f3 >> sc3) )
											{
												if(vp3 == cntr)
													f4 << vp3 << " " << st3 << " " << sc3 << std::endl;
												else
													break;
											}
											
											break;
										}
									}
							
									f4 << vp1 << " " << st1 << " " << sc1 << std::endl;					
									++cntr;
								}
							}
					
					
							f4 << vp2 << " " << st2 << " " << sc2 << std::endl;

							while( (f2 >> vp2) && (f2 >> st2) && (f2 >> sc2) )
							{						
								f4 << vp2 << " " << st2 << " " << sc2 << std::endl;
							}
							
							f4 << vp3 << " " << st3 << " " << sc3 << std::endl;
							while( (f3 >> vp3) && (f3 >> st3) && (f3 >> sc3) )
							{						
								f4 << vp3 << " " << st3 << " " << sc3 << std::endl;
							}							
					
							// close the files
							f1.close();
							f2.close();
							f3.close();
							f4.close();
   						
   					}
   					
   				}
   				

   			}
   		}
   	}
bool VolumeProcess::DialateImage(int iterations)
{
	int border = 2;

	ImageType::Pointer tempImg = ImageType::New();
	tempImg->SetRegions( m_outputImage->GetLargestPossibleRegion() );
	tempImg->Allocate();
	tempImg->FillBuffer(0);

	ImageType::RegionType fullRegion = m_outputImage->GetBufferedRegion();
	ImageType::RegionType borderRegion;
	borderRegion.SetIndex(0, fullRegion.GetIndex(0)+border);
	borderRegion.SetIndex(1, fullRegion.GetIndex(1)+border);
	borderRegion.SetIndex(2, fullRegion.GetIndex(2)+border);
	borderRegion.SetSize(0, fullRegion.GetSize(0)-(2*border));
	borderRegion.SetSize(1, fullRegion.GetSize(1)-(2*border));
	borderRegion.SetSize(2, fullRegion.GetSize(2)-(2*border));
	itk::ImageRegionIteratorWithIndex< ImageType > itr( m_outputImage, borderRegion );
	while(iterations > 0)
	{
		for(itr.GoToBegin(); !itr.IsAtEnd(); ++itr)
		{
			double blockMax = itr.Get();
			for(int k=-1; k<=1; k++)
			{
				for(int j=-1; j<=1; j++)
				{
					for(int i=-1; i<=1; i++)
					{
						ImageType::IndexType index = itr.GetIndex();
						index[0] += i;
						index[1] += j;
						index[2] += k;
						ImageType::PixelType pix = m_outputImage->GetPixel(index);
						if((double)pix > blockMax) 
						{
							blockMax = (double)pix;
						}
					}
				}
			}
			// Keep the peak of the original intensity
			if (blockMax == itr.Get() && blockMax != 0)
            {
				blockMax = blockMax + 1;
            }
			tempImg->SetPixel(itr.GetIndex(), blockMax);
		}

		//Copy temp img back to image for next dialation
		itk::ImageRegionIterator< ImageType > itr1( tempImg, tempImg->GetLargestPossibleRegion() );
		itk::ImageRegionIterator< ImageType > itr2( m_outputImage, m_outputImage->GetLargestPossibleRegion() );
		for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2)
		{
			itr2.Set( itr1.Get() );
		}

		iterations--;
	}
	
	if(debug)
		std::cerr << "Dialation Done" << std::endl;
	return true;
}