image_warp_util.h
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #ifndef IMAGE_UTIL_IMAGE_WARP_UTIL_H_
31 #define IMAGE_UTIL_IMAGE_WARP_UTIL_H_
32 
33 #include <vector>
34 
35 // Boost Libraries
36 #include <boost/circular_buffer.hpp>
37 
38 // ROS Libraries
39 #include <ros/ros.h>
40 
41 // OpenCV Libraries
42 #include <opencv2/core/core.hpp>
43 #include <opencv2/highgui/highgui.hpp>
44 #include <opencv2/features2d/features2d.hpp>
45 #include <opencv2/calib3d/calib3d.hpp>
46 #include <opencv2/stitching/detail/warpers.hpp>
47 
48 // RANGER Libraries
50 
51 namespace swri_image_util
52 {
53  cv::Mat WarpImage(const cv::Mat& image, double roll, double pitch);
54 
64  void WarpPoints(
65  double pitch,
66  double roll,
67  const cv::Size& image_size,
68  const cv::Mat& pts_in,
69  cv::Mat& pts_out);
70 
80  void WarpPoints(
81  double pitch,
82  double roll,
83  const cv::Size& image_size,
84  const std::vector<cv::KeyPoint>& pts_in,
85  std::vector<cv::KeyPoint>& pts_out);
86 
97  cv::Mat GetR(double pitch, double roll, double yaw = 0.0);
98 
99 
106  {
107  public:
112 
113 
120  cv::Mat EstimateNominalAngle(double& nominal_pitch,
121  double& nominal_roll,
122  bool show_image_diff = false);
123 
124 
131  static cv::Mat EstimateNominalAngle(const cv::Mat& points1,
132  const cv::Mat& points2,
133  const cv::Size& image_size,
134  double& nominal_pitch,
135  double& nominal_roll);
136 
137  private:
138  cv::Mat im1_;
139  cv::Mat im2_;
140 
141  cv::Mat K_;
142  cv::Mat T_;
143 
144  std::vector<cv::KeyPoint> kp1_;
145  std::vector<cv::KeyPoint> kp2_;
146  cv::Mat descriptors1_;
147  cv::Mat descriptors2_;
148 
149  cv::Mat kp1_matched_;
150  cv::Mat kp2_matched_;
151 
152  cv::detail::PlaneWarper warper_;
153 
161 
162 
176  static bool EstimateTransforms(cv::Mat& pts1,
177  cv::Mat& pts2,
178  cv::Mat& T_affine,
179  cv::Mat& T_rigid,
180  double& rms_error);
181 
190  void WarpPoints(double pitch,
191  double roll,
192  const cv::Mat& pts_in,
193  cv::Mat& pts_out);
194 
202  void WarpAffinePoints(const cv::Mat& T,
203  const cv::Mat& pts_in,
204  cv::Mat& pts_out);
205  };
206 
213  {
214  public:
219 
221 
227  void SetBufferSize(int32_t buff_size = 50);
228 
232  void Clear();
233 
243  void WarpPoints(const cv::Mat& points_in,
244  cv::Mat& points_out,
245  const cv::Size& image_size,
246  bool use_median = true);
247 
256  void GenerateNewEstimate(const cv::Mat& points1,
257  const cv::Mat& points2,
258  const cv::Size& image_size);
259 
266  void LoadNewData(double new_pitch,
267  double new_roll);
268 
279  bool GetMeanPitchAndRoll(double& pitch,
280  double& roll);
281 
282 
293  bool GetMedianPitchAndRoll(double& pitch,
294  double& roll);
295 
296  private:
297  boost::circular_buffer<double> pitches_;
298  boost::circular_buffer<double> rolls_;
299 
300  double mean_pitch_;
301  double mean_roll_;
303  double median_roll_;
304 
308  void ComputeStats();
309  };
310 }
311 
312 #endif // IMAGE_UTIL_IMAGE_WARP_UTIL_H_
swri_image_util::PitchAndRollEstimatorQueue::GenerateNewEstimate
void GenerateNewEstimate(const cv::Mat &points1, const cv::Mat &points2, const cv::Size &image_size)
Estimates pitch and roll from corresponding points and loads the pitch and roll data onto the buffer.
Definition: image_warp_util.cpp:512
swri_image_util::PitchAndRollEstimator::im2_
cv::Mat im2_
Definition: image_warp_util.h:139
swri_image_util::PitchAndRollEstimator::im1_
cv::Mat im1_
Definition: image_warp_util.h:138
ros.h
swri_image_util::WarpImage
cv::Mat WarpImage(const cv::Mat &image, double roll, double pitch)
Definition: image_warp_util.cpp:37
swri_image_util::PitchAndRollEstimatorQueue::median_pitch_
double median_pitch_
Definition: image_warp_util.h:302
swri_image_util::PitchAndRollEstimator::kp2_matched_
cv::Mat kp2_matched_
Definition: image_warp_util.h:150
swri_image_util::PitchAndRollEstimatorQueue::ComputeStats
void ComputeStats()
Computes the statistics on the data in the buffers.
Definition: image_warp_util.cpp:579
swri_image_util::PitchAndRollEstimatorQueue::pitches_
boost::circular_buffer< double > pitches_
Definition: image_warp_util.h:297
swri_image_util
Definition: draw_util.h:37
swri_image_util::PitchAndRollEstimator::kp2_
std::vector< cv::KeyPoint > kp2_
Definition: image_warp_util.h:145
swri_image_util::PitchAndRollEstimatorQueue::median_roll_
double median_roll_
Definition: image_warp_util.h:303
swri_image_util::GetR
cv::Mat GetR(double pitch, double roll, double yaw=0.0)
Gets the rotation matrix associated with the specified pitch and roll values.
Definition: image_warp_util.cpp:116
image_matching.h
swri_image_util::PitchAndRollEstimator
A class for estimating image warping based on perspective distortion. Primarily intended for use with...
Definition: image_warp_util.h:105
swri_image_util::PitchAndRollEstimatorQueue::GetMeanPitchAndRoll
bool GetMeanPitchAndRoll(double &pitch, double &roll)
Computes the mean pitch and roll.
Definition: image_warp_util.cpp:537
swri_image_util::PitchAndRollEstimator::EstimateTransforms
static bool EstimateTransforms(cv::Mat &pts1, cv::Mat &pts2, cv::Mat &T_affine, cv::Mat &T_rigid, double &rms_error)
Estimates the "nearest" rigid, and corresponding full affine transformation for a set of matching poi...
Definition: image_warp_util.cpp:360
swri_image_util::PitchAndRollEstimatorQueue::Clear
void Clear()
Clears the buffer.
Definition: image_warp_util.cpp:499
swri_image_util::PitchAndRollEstimator::WarpPoints
void WarpPoints(double pitch, double roll, const cv::Mat &pts_in, cv::Mat &pts_out)
Warps a matrix of points (in the same form as the inliers)
Definition: image_warp_util.cpp:391
swri_image_util::PitchAndRollEstimator::K_
cv::Mat K_
Definition: image_warp_util.h:141
swri_image_util::PitchAndRollEstimator::ComputeGeometricMatches
bool ComputeGeometricMatches()
Matches keypoints using loose geometric constraints and stores them in kp1_matched_ and kp2_matched_.
Definition: image_warp_util.cpp:299
swri_image_util::PitchAndRollEstimator::PitchAndRollEstimator
PitchAndRollEstimator()
Constructor.
Definition: image_warp_util.h:111
swri_image_util::PitchAndRollEstimatorQueue::rolls_
boost::circular_buffer< double > rolls_
Definition: image_warp_util.h:298
swri_image_util::PitchAndRollEstimator::WarpAffinePoints
void WarpAffinePoints(const cv::Mat &T, const cv::Mat &pts_in, cv::Mat &pts_out)
Warps a matrix of points (in the same form as the inliers)
Definition: image_warp_util.cpp:411
swri_image_util::PitchAndRollEstimator::kp1_
std::vector< cv::KeyPoint > kp1_
Definition: image_warp_util.h:144
swri_image_util::PitchAndRollEstimator::EstimateNominalAngle
cv::Mat EstimateNominalAngle(double &nominal_pitch, double &nominal_roll, bool show_image_diff=false)
Estimates the nominal pitch and roll of the camera (from perfectly vertical) from two overlapping ima...
Definition: image_warp_util.cpp:145
swri_image_util::PitchAndRollEstimatorQueue::~PitchAndRollEstimatorQueue
~PitchAndRollEstimatorQueue()
Definition: image_warp_util.h:220
swri_image_util::PitchAndRollEstimatorQueue
A class for estimating image warping based on perspective distortion. Primarily intended for use with...
Definition: image_warp_util.h:212
swri_image_util::PitchAndRollEstimator::warper_
cv::detail::PlaneWarper warper_
Definition: image_warp_util.h:152
swri_image_util::PitchAndRollEstimatorQueue::mean_pitch_
double mean_pitch_
Definition: image_warp_util.h:300
swri_image_util::PitchAndRollEstimator::T_
cv::Mat T_
Definition: image_warp_util.h:142
swri_image_util::PitchAndRollEstimatorQueue::mean_roll_
double mean_roll_
Definition: image_warp_util.h:301
swri_image_util::PitchAndRollEstimator::kp1_matched_
cv::Mat kp1_matched_
Definition: image_warp_util.h:149
swri_image_util::WarpPoints
void WarpPoints(double pitch, double roll, const cv::Size &image_size, const cv::Mat &pts_in, cv::Mat &pts_out)
Definition: image_warp_util.cpp:60
swri_image_util::PitchAndRollEstimatorQueue::WarpPoints
void WarpPoints(const cv::Mat &points_in, cv::Mat &points_out, const cv::Size &image_size, bool use_median=true)
Warps points based on the stored estimated pitch and roll.
Definition: image_warp_util.cpp:475
swri_image_util::PitchAndRollEstimatorQueue::GetMedianPitchAndRoll
bool GetMedianPitchAndRoll(double &pitch, double &roll)
Computes the median pitch and roll.
Definition: image_warp_util.cpp:551
swri_image_util::PitchAndRollEstimatorQueue::SetBufferSize
void SetBufferSize(int32_t buff_size=50)
Sets the circular buffer capacity for computing statistics.
Definition: image_warp_util.cpp:463
swri_image_util::PitchAndRollEstimatorQueue::LoadNewData
void LoadNewData(double new_pitch, double new_roll)
Loads new pitch and roll data directly onto the buffer.
Definition: image_warp_util.cpp:566
swri_image_util::PitchAndRollEstimator::descriptors1_
cv::Mat descriptors1_
Definition: image_warp_util.h:146
swri_image_util::PitchAndRollEstimatorQueue::PitchAndRollEstimatorQueue
PitchAndRollEstimatorQueue()
Constructor.
Definition: image_warp_util.cpp:452
swri_image_util::PitchAndRollEstimator::descriptors2_
cv::Mat descriptors2_
Definition: image_warp_util.h:147


swri_image_util
Author(s): Kris Kozak
autogenerated on Fri Aug 2 2024 08:39:19