Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleContourDetectorSimple.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /**
26  @example ExampleContourDetectorSimple.cpp
27  @relates ContourDetectorSimple
28  @brief Example for usage of ContourDetectorSimple
29  Usage: ContourDetectorSimple segmentedImage distanceImage contourImage
30  @ingroup g_examples
31  @author MIP
32 */
33 #include "../ContourDetectorSimple.hh"
34 #include "Base/Image/ImageIO.hh"
35 #include "Base/Debug/TimeMeasure.hh"
36 #include "Base/Image/ImageConvert.hh"
37 
38 using namespace BIAS;
39 using namespace std;
40 
41 int main(int argc, char** argv)
42 {
43 
44  Image<float> image1;
45  // Image<unsigned char> image1;
46  //Image<unsigned char> out,contourImg;
47  Image<float> out,contourImg;
48  int res;
49  if (argc <4) {
50  cerr<<"\n*********************************************************************************************************************"<<endl;
51  cerr<<"This program detects single objects in an image with black (0) background and returns all pixel of the contour of that object.\n"<<endl;
52  cerr<<"usage: "<< argv[0] <<" segmentedImage distanceImage contourImage" <<endl;
53  cerr<<"*********************************************************************************************************************"<<endl;
54  exit(1);
55  }
56 
57  if ((res=ImageIO::Load(argv[1], image1)) != 0){
58  cerr << "error loading file " << argv[1] << " " << res << endl;
59  exit (-1);
60  }
61 
62  if (image1.GetChannelCount() != 1){
63  cerr << "wrong channel count in image"<< endl;
64  }
65 
66  //ImageConvert::ConvertST(image2,image1,ImageBase::ST_unsignedchar);
67 
68  //ImageIO::Save("testtest.mip",image1);
69 
70  //ContourDetectorSimple<unsigned char> cont;
72  cout << "Set Background" << endl;
73  cont.SetBackground(0);
74  // out=image1;
75  TimeMeasure timer;
76  timer.Start();
77  std::vector<BIASContour> contour;
78  cont.Detect(image1, contour);
79  timer.Stop();
80  std::cout<<"Finding the contour did take: ";
81  timer.PrintRealTime();
82  out.Init(image1.GetWidth(), image1.GetHeight(),1);
83  //out.FillImageWithConstValue((unsigned char)0);
84  out.FillImageWithConstValue((float)0);
85  cont.CreateDistanceImage(out,contour[0]);
86  if (ImageIO::Save(argv[2], out)!=0){
87  BIASERR("error writing");
88  }
89  cout<<"Written distance image: "<<argv[2]<<endl;
90 
91  cout<<"Contour:"<<endl;
92  contourImg.Init(image1.GetWidth(), image1.GetHeight(),3);
93 
94  for (unsigned int i=0;i<contour.size();i++){
95  for (unsigned int k=0;k<contour[i].length;k++){
96  cout<<contour[i].contourPixel[k]<<"\t ";
97 
98  contourImg.SetPixel(255,255,255,
99  (unsigned int)contour[i].contourPixel[k][0],
100  (unsigned int)contour[i].contourPixel[k][1]);
101 
102  }
103  cout<<endl;
104  }
105 
106 
107  if (ImageIO::Save(argv[3], contourImg)!=0){
108  BIASERR("error writing");
109  }
110  cout<<"Written contour image: "<<argv[3]<<endl;
111 }
void CreateDistanceImage(BIAS::Image< StorageType > &dist_img, const BIASContour &contour)
returns a Borgefor distance transformed image (returned in &#39;dist_img&#39;) of the pixel set given by the ...
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int Detect(Image< StorageType > &image, std::vector< BIAS::BIASContour > &contour)
detect function of ContourDetectorSimple
a class for calculating the contour of a segmented region
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void FillImageWithConstValue(StorageType Value)
fill grey images
Definition: Image.cpp:456
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
void SetBackground(StorageType backgroundColor)
Set the background color if it is different from 0.
void SetPixel(const StorageType &value, const unsigned int &x, const unsigned int &y, const unsigned short int channel=0)
Set the value of a given pixel (x,y) in channel to value.
Definition: Image.hh:171
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
void PrintRealTime(std::ostream &os=std::cout, const bool &verbose=true) const
print the real time (=wall time clok)
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111