PICCANTE  0.4
The hottest HDR imaging library!
image_alignment.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_COMPUTER_VISION_IMAGE_ALIGNMENT_HPP
19 #define PIC_COMPUTER_VISION_IMAGE_ALIGNMENT_HPP
20 
21 #include <vector>
22 
23 #include "../base.hpp"
24 #include "../image.hpp"
25 #include "../features_matching/orb_descriptor.hpp"
26 #include "../features_matching/harris_corner_detector.hpp"
27 #include "../features_matching/binary_feature_brute_force_matcher.hpp"
28 #include "../computer_vision/homography_matrix.hpp"
29 #include "../filtering/filter_gaussian_2d.hpp"
30 #include "../filtering/filter_warp_2d.hpp"
31 
32 #ifndef PIC_DISABLE_EIGEN
33 #ifndef PIC_EIGEN_NOT_BUNDLED
34  #include "../externals/Eigen/Dense"
35 #else
36  #include <Eigen/Dense>
37 #endif
38 #endif
39 
40 namespace pic {
41 
42 #ifndef PIC_DISABLE_EIGEN
43 
50 Eigen::Matrix3d getHomographyMatrixFromTwoImage(Image *img0, Image *img1)
51 {
52  //output corners
53  std::vector< Eigen::Vector2f > corners_from_img0;
54  std::vector< Eigen::Vector2f > corners_from_img1;
55 
56  //get corners
57  HarrisCornerDetector hcd(2.5f, 5);
58  hcd.execute(img0, &corners_from_img0);
59  hcd.execute(img1, &corners_from_img1);
60 
61  //compute ORB descriptors for each corner and image
62  ORBDescriptor b_desc(31, 512);
63 
64  std::vector< pic::uint* > descs0;
65  b_desc.getAll(img0, corners_from_img0, descs0);
66 
67  std::vector< pic::uint* > descs1;
68  b_desc.getAll(img1, corners_from_img1, descs1);
69 
70  //match descriptors
71  int n = b_desc.getDescriptorSize();
72  BinaryFeatureBruteForceMatcher bfm(&descs1, n);
73 
74  std::vector< Eigen::Vector3i > matches;
75  bfm.getAllMatches(descs0, matches);
76 
77  //filter matches
78  std::vector< Eigen::Vector2f > m0, m1;
79  FeatureMatcher<uint>::filterMatches(corners_from_img0, corners_from_img1, matches, m0, m1);
80 
81  //estimate a homography matrix H from the matches
82  std::vector< uint > inliers;
83  Eigen::Matrix3d H = estimateHomographyWithNonLinearRefinement(m0, m1, inliers, 10000, 2.5f, 1, 10000, 1e-5f);
84 
85  return H;
86 }
87 
95 Image* imageAlignmentWithORB(Image *img0, Image *img1, Image *imgOut)
96 {
97  auto H = getHomographyMatrixFromTwoImage(img0, img1);
98 
99  FilterWarp2D flt;
100  flt.update(pic::MatrixConvert(H), true, false);
101  imgOut = flt.Process(Single(img0), imgOut);
102 
103  return imgOut;
104 }
105 
106 #endif
107 
108 }
109 
110 #endif // PIC_COMPUTER_VISION_IMAGE_ALIGNMENT_HPP
PIC_INLINE Eigen::Matrix3d estimateHomographyWithNonLinearRefinement(std::vector< Eigen::Vector2f > &points0, std::vector< Eigen::Vector2f > &points1, std::vector< unsigned int > &inliers, unsigned int maxIterationsRansac=10000, double thresholdRansac=2.5, unsigned int seedRansac=1, unsigned int maxIterationsNonLinear=10000, float thresholdNonLinear=1e-5f)
estimateHomographyRansac computes the homography such that: points1 = H * points0 ...
Definition: homography_matrix.hpp:231
void execute(Image *img, std::vector< Eigen::Vector2f > *corners)
execute
Definition: harris_corner_detector.hpp:137
virtual Image * Process(ImageVec imgIn, Image *imgOut)
Process.
Definition: filter.hpp:390
The HarrisCornerDetector class.
Definition: harris_corner_detector.hpp:54
Image * imageAlignmentWithORB(Image *img0, Image *img1, Image *imgOut)
imageAlignmentWithORB
Definition: image_alignment.hpp:95
PIC_INLINE Matrix3x3 MatrixConvert(Eigen::Matrix3f &mat)
MatrixConvert converts a matrix from a Eigen::Matrix3f representation into a Matrix3x3 representation...
Definition: eigen_util.hpp:356
void getAllMatches(std::vector< unsigned int *> &descs0, std::vector< Eigen::Vector3i > &matches)
Definition: feature_matcher.hpp:81
The Image class stores an image as buffer of float.
Definition: image.hpp:60
void update(Matrix3x3 h, bool bSameSize, bool bCentroid=false)
update
Definition: filter_warp_2d.hpp:199
void getAll(Image *img, std::vector< Eigen::Vector2f > &corners, std::vector< uint * > &descs)
getAll
Definition: brief_descriptor.hpp:226
The ORBDescriptor class.
Definition: orb_descriptor.hpp:34
PIC_INLINE ImageVec Single(Image *img)
Single creates an std::vector which contains img; this is for filters input.
Definition: image_vec.hpp:36
static void filterMatches(std::vector< Eigen::Vector2f > &c0, std::vector< Eigen::Vector2f > &c1, std::vector< Eigen::Vector3i > &matches, std::vector< Eigen::Vector2f > &p0, std::vector< Eigen::Vector2f > &p1)
filterMatches
Definition: feature_matcher.hpp:103
Definition: bilateral_separation.hpp:25
int getDescriptorSize()
getDescriptorSize returns the descriptor size.
Definition: brief_descriptor.hpp:244
The BinaryFeatureBruteForceMatcher class.
Definition: binary_feature_brute_force_matcher.hpp:32
Eigen::Matrix3d getHomographyMatrixFromTwoImage(Image *img0, Image *img1)
getHomographyMatrixFromTwoImage
Definition: image_alignment.hpp:50
The FilterWarp2D class.
Definition: filter_warp_2d.hpp:31