Beispiel #1
0
int main(int argc, char* argv[]) {


  std::string image_dir;
  if(argc!=2)
    {
      std::cout << "Usage :" << std::endl
                << "  " << argv[0] << " <directory>" << std::endl
                << "ex : " << argv[0] << " /usr/share/mirage-images" << std::endl;
      return 1;
    }
  image_dir = argv[1];

  try {

    ImageRGB24 source,result;
    ImageInt   filter;

    mirage::Init();

    // Here, we play with border effects.
    
    // We load an image from filename.
    mirage::img::JPEG::read(source,image_dir+"/spiderman.jpg");
    std::cout << "Generating spiderman.jpg" << std::endl;
    mirage::img::JPEG::write(source,"spiderman.jpg",75);

    // We create a bigger image
    result.resize(source._dimension*2.5);

    // We create a filter
    filter.resize(source._dimension/2);
    filter = 0; // All values are 0.
    *(filter.begin()) = 1; // upper left is 1.

    // Let us try the default out frame policy.
    source.setPolicy(new mirage::ZeroOutFramePolicy<ImageRGB24>());
    // This will generates an error message (and is inefficient),
    // since optimized convolution needs source and result images
    // having the same size.
    OptimizedConvolution(source,filter,result);  
    // This will generates an error message (and is inefficient),
    // since optimized convolution (involved by the template) needs
    // source and result images having the same size.
    mirage::colorspace::Convolution<ImageRGB24,ImageInt,ImageRGB24>::RGB::Process(source,filter,result);
    // And last, the good one for this border-effect example
    Convolution(source,filter,result);  
    std::cout << "Generating spiderman-zero.jpg" << std::endl;
    mirage::img::JPEG::write(result,"spiderman-zero.jpg",75);
    
    // Let us try other out frame policies.

    source.setPolicy(new mirage::ClosePointOutFramePolicy<ImageRGB24>());
    Convolution(source,filter,result);  
    std::cout << "Generating spiderman-close-point.jpg" << std::endl;
    mirage::img::JPEG::write(result,"spiderman-close-point.jpg",75);

    source.setPolicy(new mirage::PeriodicOutFramePolicy<ImageRGB24>());
    Convolution(source,filter,result);  
    std::cout << "Generating spiderman-periodic.jpg" << std::endl;
    mirage::img::JPEG::write(result,"spiderman-periodic.jpg",75);
    

    source.setPolicy(new mirage::SymetricOutFramePolicy<ImageRGB24>());
    Convolution(source,filter,result);  
    std::cout << "Generating spiderman-symetric.jpg" << std::endl;
    mirage::img::JPEG::write(result,"spiderman-symetric.jpg",75);
    

    // Now, we know how to filter images. Let us try to apply
    // ready-to-use mirage filters.

    mirage::img::JPEG::read(source,image_dir+"/shrek.jpg");
    std::cout << "Generating shrek.jpg" << std::endl;
    mirage::img::JPEG::write(source,"shrek.jpg",75);
    

    // We can get the vertical sobel filter in an ImageInt data.

    ImageInt sobel_vert;
    ImageInt::iterator sobel_vert_iter, sobel_vert_iter_end;
    int w,sobel_vert_width;

    mirage::img::Filter<int>::Sobel::Vertical(sobel_vert);
    sobel_vert_width = sobel_vert._dimension[0];
    std::cout << "Sobel vertical filter :" << std::endl;
    for(sobel_vert_iter=sobel_vert.begin(),sobel_vert_iter_end=sobel_vert.end();
        sobel_vert_iter != sobel_vert_iter_end;
        std::cout << std::endl)
      for(w=0;
          w<sobel_vert_width;
          ++sobel_vert_iter,++w)
        std::cout << std::setw(3) << *sobel_vert_iter << ' ';

    // Nevertheless, we can set up the sobel filtering with a set of
    // predifine functions, that allocates the sobel filter obtenied
    // here internally. These are available for RGB and Gray, on 2D
    // frames.
    
    mirage::SubFrame<ImageRGB24> subimage(source,mirage::img::Coordinate(0,0),source._dimension/2);
    ImageRGBDouble sobel_result;


    // Template parameters are static-flag, source type, destination
    // type. No buffering would have has some strange
    // effects since source image would have changed during the
    // filtering process.
    //
    // The first parameter, the static flag, is set to 1 since we want
    // to use static data for intermediate computations (this avoids
    // many reallocations). Be carful with that, is is not thread
    // safe. In a multi-thread context, set this flag to 0.
    sobel_result.resize(subimage._dimension);
    mirage::img::Filtering<1,
      mirage::SubFrame<ImageRGB24>,
      ImageRGBDouble>::RGB::Sobel::Horizontal(subimage,sobel_result);
    
    // sobel_result has values in [-255*4,255*4], so we rescale them
    // and convert in RGB24.
    mirage::algo::UnaryOp<ImageRGBDouble,mirage::SubFrame<ImageRGB24>,SobelRGBDoubleToRGB24Op>(sobel_result,subimage);
    
    // It is to be notices here that filtering the sub-image deals
    // with its border... but the outframe policy is involved only for
    // the border of the sub-image corresponding to actual borders of
    // parent image. So for right and bottom border, out of bound
    // during filtering consist in picking existing pixels in the
    // parent image.

    // Now, let us use the full sobel filter on another subpart of the
    // image.  Note that passing 1 for buffer mode (last template
    // parameter) is required, since source and destination are the
    // same.
    subimage.resize(source._dimension/3,source._dimension/2);
    sobel_result.resize(subimage._dimension);
    mirage::img::Filtering<1,
      mirage::SubFrame<ImageRGB24>,
      mirage::SubFrame<ImageRGB24>,
      1>::RGB::Sobel::Norm(subimage,subimage);

    std::cout << "Generating shrek-sobel.ppm" << std::endl;
    mirage::img::PPM::write(source,"shrek-sobel.ppm");

    // Actually, sobel filtering extracts lines from grayscaled images.
    
    ImageGray8 gray_result;

    // As source is altered, we reload it from file.
    mirage::img::JPEG::read(source,image_dir+"/shrek.jpg");
    
    gray_result.resize(source._dimension);
    // There is an unary operator pre-define for color to gray transforms.
    mirage::algo::UnaryOp<ImageRGB24,ImageGray8,
      mirage::colorspace::RGBToGray<ImageRGB24::value_type,ImageGray8::value_type> >(source,gray_result);

    std::cout << "Generating shrek-gray.jpg" << std::endl;
    mirage::img::JPEG::write(gray_result,"shrek-gray.jpg",10); // Low compression quality for fun...
    
    mirage::img::Filtering<1,
      ImageGray8,
      ImageGray8,
      1>::Gray::Sobel::Norm(gray_result,gray_result);

    std::cout << "Generating shrek-gray-sobel.ppm" << std::endl;
    mirage::img::PPM::write(gray_result,"shrek-gray-sobel.ppm"); 
    
    
  }
  catch(mirage::Exception::Any& e) {
    std::cerr << "Error : " <<  e.what() << std::endl;
  }
  catch(...) {
    std::cerr << "Unknown error" << std::endl;
  }

  return 0;
}
Beispiel #2
0
int main(int argc, char** argv)
{

if(argc != 4)
{
	cout << argc << endl;
	cout << "to use this program you have to enter this command line ./main path/of/the/picture connexity threshold " << endl;
	cout << "Threshold should be in 0-255" << endl;
	cout << "connexity should be 4connexity or 8connexity or chamfer5711" << endl;
}
else 
{
	int threshold = atoi(argv[2]);

	if(threshold <= 255 && threshold >= 0 && ( (strcmp(argv[3],"4connexity") == 0 ) || (strcmp(argv[3],"8connexity") == 0 ) || strcmp(argv[3],"chamfer5711") == 0) )
	{
		vector<point2dWeighting> myWeightingVector;

	   if(strcmp(argv[3],"4connexity") == 0 )
	   {
		   make4Connexity(myWeightingVector);
	   }
	   else if(strcmp(argv[3],"8connexity") == 0)
	   {
		   make8Connexity(myWeightingVector);
	   }
	   else
	   {
		   makeSimpleChamfrein(myWeightingVector);
	   }


      // mask creation
      SymmetricMask<point2dWeighting> myMask;
      SymmetricMaskGenerator<point2dWeighting> generateur;
      myMask = generateur.generateMask(myWeightingVector);



      // CMETRIC and distance Transform instanciation
      CChamferMetric<int,point2d> myMetric(myMask);
      ChamferDistanceTransform<int,point2d> myDistance(myMetric);



      Image image = DGtal::PNMReader<Image>::importPGM(argv[1], true); 


      /** distance transformation **/
      ImageInt output = myDistance.applyAlgorithm(image,threshold,true);


      /** output colorisation **/
      DGtal::Board2D aBoard;
      ImageInt::Value maxDT = (*std::max_element(output.begin(), 
                                                      output.end()));
      typedef DGtal::HueShadeColorMap<ImageInt::Value,2> HueTwice;
      aBoard.clear();
      Display2DFactory::drawImage<HueTwice>(aBoard, output, (ImageInt::Value)0, (ImageInt::Value)maxDT);
      aBoard.saveEPS(outputNameFile);

      /** distance transformation numeric output **/
      /*
      typename Image::Domain::ConstIterator dit= image.domain().begin();	
      for(;dit != image.domain().end();++dit)
      {
		      cout << (*dit) << " : " << (int)output(*dit) << endl;			
      }*/
	}
}
return 0;
}