Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleConstantRegion.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 ExampleConstantRegion.cpp
27  @relates ConstantRegionDetector, LinearRegionDetector, BlobDetectorDOM
28  @brief Example for usage of ConstantRegionDetector, LinearRegionDetector, BlobDetectorDOM
29  Usage: ExampleConstantRegion <im1> [ <im2> <im3> ... ]
30  @ingroup g_examples
31  @author MIP
32 */
33 
34 #include <bias_config.h>
35 #include <FeatureDetector/ConstantRegionDetector.hh>
36 #include <FeatureDetector/LinearRegionDetector.hh>
37 #include <FeatureDetector/BlobDetectorDOM.hh>
38 #include <Base/ImageUtils/ImageDraw.hh>
39 #include <Base/Image/ImageConvert.hh>
40 #include <Base/Image/ImageIO.hh>
41 
42 
43 using namespace BIAS;
44 using namespace std;
45 
46 void Draw(Image<unsigned char>& im, vector<HomgPoint2D>& p)
47 {
48 
49  const int nump=p.size();
50  unsigned start[2];
51  unsigned radius=3;
52 
53  for (int l=0; l<nump; l++){
54  if (!p[l].IsAtInfinity()){
55  // circel around corner in actual image
56  start[0]=(unsigned)rint(p[l][0]);
57  start[1]=(unsigned)rint(p[l][1]);
58  ImageDraw<unsigned char>::Circle(im, start[0], start[1], radius);
59  }
60  }
61 }
62 
63 int main(int argc, char *argv[])
64 {
65 
66 
67 
68  Image<unsigned char> greyim;
70  Image<unsigned char> result;
71 
72 
73  int argind = 1;
74  if (argc-argind<1 || argind<1){
75  cerr << argv[0] << " <im1> [ <im2> <im3> ... ] \n";
76  return -2;
77  }
78 
79 
80  for (int imgCount=argind; imgCount<argc; imgCount++){
81 
82  // load image
83  if (ImageIO::Load(argv[imgCount], ucim)!=0){
84  BIASERR("error loading image "<<argv[imgCount] << " is storage type unsigned char?" << endl);
85  return -1;
86  } else {
87  cerr << "read "<<argv[imgCount]<<endl;
88  }
89 
90  // linear region detector works only on grey value images
91  if(ImageConvert::ToGrey(ucim, greyim)!=0){
92  BIASERR("could not convert to grey value image");
93  }
94 
95 
96 
97  ////////////////////////////////////// start constant region detector////////////////////////////////////
98  cout << "Constant Region Detection..."<<endl;
99  vector<HomgPoint2D> points2d;
100  vector<Vector3<unsigned char> > colors;
102 
103  regionDetector.SetThreshold(3.0);
104  regionDetector.SetHalfWinSize(3);
105  regionDetector.SetMaxNumFeatures(-1);
106  regionDetector.SetMinDistance(10);
107 
109  ImageIO::Load("segmentWater0000.mip", mask);
110  regionDetector.SetMaskImage(mask);
111 
112  // detect and draw
113  regionDetector.Detect(ucim, points2d, colors, true);
114  cout << "found " << points2d.size() << " constant regions " << endl;
115  // fill result image
116  Image<unsigned char> result = ucim;
117  Draw(result, points2d);
118 
119  ostringstream name;
120  name << "constantRegions-"<<setw(4)<<setfill('0')<<imgCount-argind<<".mip";
121  cout << "name " << name.str() << endl << flush;
122  ImageIO::Save(name.str(), result);
123 
124 
125  ///////////////////////////////// start linear region detector//////////////////////////////////////////
126  cout << "Linear Region Detection..."<<endl;
128  linDet.SetSubpixelAccuracy(false);
129  vector<QUAL> quality;
130  points2d.clear();
131  linDet.Detect(greyim, points2d, quality, NULL);
132  result = ucim;
133  Draw(result, points2d);
134  ostringstream name2;
135  name2 << "linearRegions-"<<setw(4)<<setfill('0')<<imgCount-argind<<".mip";
136  cout << "name " << name2.str() << endl << flush;
137  ImageIO::Save(name2.str(), result);
138 
139 
140  //////////////////////////////// start DOM blob detector//////////////////////////////////////////////
141  cout << "Blob Region Detection..."<<endl;
142  BlobDetectorDOM<unsigned char> blobDetector;
143  vector<BIAS::BIASBlob> blobs;
144  blobDetector.SetMaxSize(20);
145  blobDetector.Detect(ucim, blobs);
146  unsigned char* color = new unsigned char[3];
147  color[0] = 255;
148  color[1] = 255;
149  color[2] = 255;
150  result = ucim;
151  blobDetector.DrawInImage(result, color);
152  delete color;
153 
154  ostringstream name3;
155  name3 << "blobRegions-"<<setw(4)<<setfill('0')<<imgCount-argind<<".mip";
156  cout << "name3 " << name3.str() << endl << flush;
157  ImageIO::Save(name3.str(), result);
158 
159  ImageIO::Save("blobimg.mip", result);
160 
161  }
162 
163  return 0;
164 }
int DrawInImage(BIAS::Image< StorageType > &image, StorageType *color)
draws the detected blobs in image
void SetMaskImage(BIAS::Image< unsigned char > &maskImg)
void SetSubpixelAccuracy(bool subpixel_accuracy)
void SetThreshold(CalculationType threshold)
static void Circle(BIAS::Image< StorageType > &dstImg, const int &centerX, const int &centerY, const int &radius=3, const BIAS::ColourRGB< StorageType > &colorRGB=ColourRGB< StorageType >(255, 255, 255), const int &thickness=1, const int &linetype=8, const int &shift=0)
OpenCV: Draws a circle.
Definition: ImageDraw.cpp:1512
void SetMinDistance(int minDistance)
try to detect linear regions (const gradient) in images, experimental code for testing ...
void SetMaxSize(unsigned int maxSize)
Set maximum size of blobs.
void SetMaxNumFeatures(int maxNumFeatures)
restrict maximum number of features -1 is default and all features are returned.
virtual int Detect(Image< StorageType > &src, std::vector< HomgPoint2D > &points2d, std::vector< Vector3< StorageType > > &colors, bool useMedian=false)
Detects constant regions in image src.
void SetHalfWinSize(int halfWinSize)
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
detects regions with close to zero gradients in images, works on color images only ...
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
int Detect(BIAS::Image< StorageType > &image, std::vector< BIAS::BIASBlob > &blobs)
Detect and return blobs in an image.
Blob detector for &#39;Difference Of Means&#39;-blobs (so this is not a binary blob detector).
virtual int Detect(const Image< StorageType > &image, std::vector< HomgPoint2D > &p, std::vector< QUAL > &quality, std::vector< Matrix2x2< double > > *cov=NULL)
detect corners in a grey image
static int ToGrey(const ImageBase &source, ImageBase &dest)
wrapper for the templated function ToGrey