Berkeley SfM
drawing_utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: Erik Nelson ( eanelson@eecs.berkeley.edu )
35  * David Fridovich-Keil ( dfk@eecs.berkeley.edu )
36  */
37 
38 #include <opencv2/core/core.hpp>
39 #include <opencv2/features2d/features2d.hpp>
40 #include <opencv2/highgui/highgui.hpp>
41 
42 #include "drawing_utils.h"
43 #include "../matching/feature.h"
44 #include "../math/random_generator.h"
45 
46 namespace bsfm {
47 namespace drawing {
48 
49 void AnnotateFeatures(const FeatureList& features, Image& image,
50  unsigned int radius, unsigned int line_thickness) {
51  // Create a random number generator for random colors.
53 
54  // Get OpenCV mat from the input image.
55  cv::Mat cv_image;
56  image.ToCV(cv_image);
57 
58  for (const auto& feature : features) {
59  cv::Point cv_feature;
60  cv_feature.x = feature.u_;
61  cv_feature.y = feature.v_;
62 
63  const cv::Scalar color(rng.Double(), rng.Double(), rng.Double());
64  cv::circle(cv_image, cv_feature, radius, color, line_thickness);
65  }
66 
67  // Store the OpenCV mat in the image.
68  image.FromCV(cv_image);
69 }
70 
71 void DrawImageFeatures(const FeatureList& features, const Image& image,
72  const std::string& window_name, unsigned int radius,
73  unsigned int line_thickness) {
74  // Annotate features on a copy of the image.
75  Image copy(image);
76  AnnotateFeatures(features, copy, radius, line_thickness);
77 
78  // Convert the copied image to OpenCV format.
79  cv::Mat cv_copy;
80  copy.ToCV(cv_copy);
81 
82  // Draw the copied and annotated image in a named window.
83  cv::namedWindow(window_name.c_str(), CV_WINDOW_AUTOSIZE);
84  cv::imshow(window_name.c_str(), cv_copy);
85  cv::waitKey(0);
86 }
87 
88 void DrawImageFeatureMatches(const Image& image1, const Image& image2,
89  const FeatureMatchList& feature_matches,
90  const std::string& window_name,
91  unsigned int line_thickness) {
92  // Create a random number generator for random colors.
94 
95  // Get OpenCV mats from the input images.
96  cv::Mat cv_image1, cv_image2;
97  image1.ToCV(cv_image1);
98  image2.ToCV(cv_image2);
99 
100  // Create an OpenCV mat of the two images side by side.
101  cv::Mat combined_image;
102  cv::hconcat(cv_image1, cv_image2, combined_image);
103 
104  // Draw lines between feature locations in the combined image.
105  for (const auto& feature_match : feature_matches) {
106  cv::Point feature1;
107  feature1.x = feature_match.feature1_.u_;
108  feature1.y = feature_match.feature1_.v_;
109 
110  cv::Point feature2;
111  feature2.x = feature_match.feature2_.u_;
112  feature2.y = feature_match.feature2_.v_;
113  feature2 += cv::Point(image2.Width(), 0);
114 
115  // Color channel values between [0, 1).
116  const cv::Scalar color(rng.Double(), rng.Double(), rng.Double());
117  cv::line(combined_image, feature1, feature2, color, line_thickness);
118 
119  // Draw circles around each feature.
120  cv::circle(combined_image, feature1, line_thickness * 3 /*radius*/, color,
121  line_thickness * 2);
122  cv::circle(combined_image, feature2, line_thickness * 3 /*radius*/, color,
123  line_thickness * 2);
124  }
125 
126  // Draw the new image in a named window.
127  cv::namedWindow(window_name.c_str(), CV_WINDOW_AUTOSIZE);
128  cv::imshow(window_name.c_str(), combined_image);
129  cv::waitKey(0);
130 }
131 
132 } //\namespace drawing
133 } //\namespace bsfm
void DrawImageFeatures(const FeatureList &features, const Image &image, const std::string &window_name, unsigned int radius, unsigned int line_thickness)
void AnnotateFeatures(const FeatureList &features, Image &image, unsigned int radius, unsigned int line_thickness)
std::vector< Feature > FeatureList
Definition: feature.h:65
void FromCV(const cv::Mat &in)
Definition: image.cpp:79
Definition: camera.cpp:50
std::vector< FeatureMatch > FeatureMatchList
Definition: feature_match.h:99
void DrawImageFeatureMatches(const Image &image1, const Image &image2, const FeatureMatchList &feature_matches, const std::string &window_name, unsigned int line_thickness)
void ToCV(cv::Mat &out) const
Definition: image.cpp:72
static unsigned long Seed()
size_t Width() const
Definition: image.cpp:113