Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biascornermatcher.cpp
1 #include <FeatureDetector/CornerDetectorKLT.hh>
2 #include <Matcher2D/RegionMatcher.hh>
3 #include <Base/Image/Image.hh>
4 #include <Base/Image/ImageIO.hh>
5 #include <Base/Image/ImageConvert.hh>
6 #include <Base/ImageUtils/ImageDraw.hh>
7 
8 using namespace BIAS;
9 using namespace std;
10 /**
11  @file
12  @ingroup g_tools
13  @brief Match KLT corners, see biascornermatcher.cpp
14  @author MIP
15 */
16 int main(int argc, char *argv[])
17 {
18  Image<unsigned char> img[2], grey[2], result;
19  if (argc!=3){
20  cerr << argv[0] << " <img 1> <img 2>\n";
21  return -1;
22  }
23 
24  vector<vector<HomgPoint2D> > p(2);
25  vector<vector<QUAL> > qual(2);
27  cd.SetMinCornerness(50);
28  cd.SetMinDistance(5);
29  cd.SetMaxNumFeatures(1000);
30 
31  for (int i=0; i<2; i++){
32  int res = ImageIO::Load(argv[1+i], img[i]);
33  if (res !=0){
34  cerr << "error loading image \""<<argv[1+i]<<"\"\n";
35  return -2;
36  }
37  res = ImageConvert::Convert(img[i], grey[i], ImageBase::CM_Grey);
38  if (res !=0){
39  cerr << "error converting image \""<<argv[1+i]<<"\"\n";
40  return -3;
41  }
42  res = cd.Detect(grey[i], p[i], qual[i]);
43  if (res !=0){
44  cerr << "error detecting corners in image \""<<argv[1+i]<<"\"\n";
45  return -4;
46  }
47  }
48 
49  // prepare result image
50  int height = max(img[0].GetHeight(), img[1].GetHeight());
51  int width = img[0].GetWidth() + img[1].GetWidth();
52  Vector2<int> offset(img[0].GetWidth(), 0);
53  result.Init(width, height, img[0].GetChannelCount());
54  result.SetColorModel(img[0].GetColorModel());
55  result.FillImageWithConstValue((unsigned char)0);
56  result.GetROI()->SetCorners(0, 0, img[0].GetWidth(), img[0].GetHeight());
57  result.Paste2ROI(img[0]);
58  result.GetROI()->SetCorners(img[0].GetWidth(), 0, width, img[1].GetHeight());
59  result.Paste2ROI(img[1]);
60 
61  // now match every corner with every corner from te other image
62  unsigned p0[2], p1[2], p1s[2];
63  unsigned width0 = img[0].GetWidth();
64  unsigned width1 = img[1].GetWidth();
65  unsigned height0 = img[0].GetHeight();
66  unsigned height1 = img[1].GetHeight();
67  unsigned hws = 5;
68  double ncc_val;
69  const double min_ncc_val = 0.95;
70  RegionMatcher rm;
71  const unsigned char **ida0 =
72  (const unsigned char **) grey[0].GetImageDataArray();
73  const unsigned char **ida1 =
74  (const unsigned char **) grey[1].GetImageDataArray();
75  for (int idx0=0; idx0<(int)p[0].size(); idx0++){
76  p0[0] = (unsigned)rint(p[0][idx0][0]);
77  p0[1] = (unsigned)rint(p[0][idx0][1]);
78  if (p0[0]<hws || p0[0]+hws>=width0 ||
79  p0[1]<hws || p0[1]+hws>=height0){
80  continue;
81  }
82 
83  unsigned char col[] = { 255, 255, 255};
84  unsigned radius = 5;
85  ImageDraw<unsigned char>::CircleCenter(result, p0[0], p0[1], radius, col);
86 
87  for (int idx1=0; idx1<(int)p[1].size(); idx1++){
88  p1[0] = (unsigned)rint(p[1][idx1][0]);
89  p1[1] = (unsigned)rint(p[1][idx1][1]);
90  if (p1[0]<hws || p1[0]+hws>=width1 ||
91  p1[1]<hws || p1[1]+hws>=height1){
92  continue;
93  }
94  if (true || idx1==0){
95  ImageDraw<unsigned char>::CircleCenter(result, p1[0]+offset[0],
96  p1[1]+offset[1], radius, col);
97  }
98  int search_region = 1;
99  for (int y=-search_region; y<=search_region; y++){
100  p1s[1] = (int)(p1[1]) + y;
101  for (int x=-search_region; x<=search_region; x++){
102  p1s[0] = (int)(p1[0]) + x;
103  if (p1s[0]<hws || p1s[0]+hws>=width1 ||
104  p1s[1]<hws || p1s[1]+hws>=height1){
105  continue;
106  }
107  rm.NCC(p0, p1s, ida0, ida1, hws, ncc_val);
108 
109  if (ncc_val>min_ncc_val){
110  unsigned p1offs[2] = { p1[0]+offset[0], p1[1]+offset[1] };
111  ImageDraw<unsigned char>::Line(result, p0, p1offs, col);
112  break;
113  break;
114  }
115  }
116  }
117  }
118  cout << "."<<flush;
119  }
120  cout << endl;
121  ImageIO::Save("matches.mip", result);
122 
123  return 0;
124 }
void SetMinDistance(int mindist)
sets the min distance of features (simple parameter access function - does nothing else) ...
gray values, 1 channel
Definition: ImageBase.hh:130
int SetCorners(unsigned UpperLeftX, unsigned UpperLeftY, unsigned LowerRightX, unsigned LowerRightY)
Sets a rectangular region of interest.
Definition: ROI.cpp:287
void SetMaxNumFeatures(const int maxnum)
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
unsigned int GetWidth() const
Definition: ImageBase.hh:312
virtual void SetMinCornerness(double min_cornerness)
sets minimum cornernes of detectable features
static int CircleCenter(Image< StorageType > &im, unsigned int CenterX, unsigned int CenterY, unsigned int Radius, const StorageType Value[]=NULL)
draws a circular line, either using Value or a good contrast value
Definition: ImageDraw.cpp:977
ROI * GetROI()
Returns a pointer to the roi object.
Definition: ImageBase.hh:615
static int Line(Image< StorageType > &im, const unsigned int start[2], const unsigned int end[2], const StorageType value[])
lines
Definition: ImageDraw.cpp:404
unsigned int GetHeight() const
Definition: ImageBase.hh:319
Computes the cornerness as the smaller eigenvalue of the structure tensor matrix. ...
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
Basic functions for CornerMatcher.
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
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
static int Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false)
main general conversion function, calls desired specialized functions, always initializes the destIma...
int Paste2ROI(const ImageBase &Image)
paste Image to current ROI
Definition: ImageBase.cpp:603
void NCC(const unsigned int p1[2], const unsigned int p2[2], const StorageType **ida1, const StorageType **ida2, const unsigned int halfwinsize, double &result) const
fast ncc between p1 and p2 without boundary checking Faster specialisation for unsigned char exists b...
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