ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
line.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
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 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #include "base/line.h"
33 
34 #include "util/logging.h"
35 
36 extern "C" {
37 #include "LSD/lsd.h"
38 }
39 
40 namespace colmap {
41 
42 std::vector<LineSegment> DetectLineSegments(const Bitmap& bitmap,
43  const double min_length) {
44  const double min_length_squared = min_length * min_length;
45 
46  std::vector<uint8_t> bitmap_data;
47  if (bitmap.IsGrey()) {
48  bitmap_data = bitmap.ConvertToRowMajorArray();
49  } else {
50  const Bitmap bitmap_gray = bitmap.CloneAsGrey();
51  bitmap_data = bitmap_gray.ConvertToRowMajorArray();
52  }
53 
54  std::vector<double> bitmap_data_double(bitmap_data.begin(),
55  bitmap_data.end());
56 
57  int num_segments;
58  std::unique_ptr<double> segments_data(lsd(&num_segments,
59  bitmap_data_double.data(),
60  bitmap.Width(), bitmap.Height()));
61 
62  std::vector<LineSegment> segments;
63  segments.reserve(num_segments);
64  for (int i = 0; i < num_segments; ++i) {
65  const Eigen::Vector2d start(segments_data.get()[i * 7],
66  segments_data.get()[i * 7 + 1]);
67  const Eigen::Vector2d end(segments_data.get()[i * 7 + 2],
68  segments_data.get()[i * 7 + 3]);
69  if ((start - end).squaredNorm() >= min_length_squared) {
70  segments.emplace_back();
71  segments.back().start = start;
72  segments.back().end = end;
73  }
74  }
75 
76  return segments;
77 }
78 
79 std::vector<LineSegmentOrientation> ClassifyLineSegmentOrientations(
80  const std::vector<LineSegment>& segments, const double tolerance) {
81  CHECK_LE(tolerance, 0.5);
82 
83  std::vector<LineSegmentOrientation> orientations;
84  orientations.reserve(segments.size());
85 
86  for (const auto& segment : segments) {
87  const Eigen::Vector2d direction =
88  (segment.end - segment.start).normalized();
89  if (std::abs(direction.x()) + tolerance > 1) {
90  orientations.push_back(LineSegmentOrientation::HORIZONTAL);
91  } else if (std::abs(direction.y()) + tolerance > 1) {
92  orientations.push_back(LineSegmentOrientation::VERTICAL);
93  } else {
94  orientations.push_back(LineSegmentOrientation::UNDEFINED);
95  }
96  }
97 
98  return orientations;
99 }
100 
101 } // namespace colmap
std::vector< uint8_t > ConvertToRowMajorArray() const
Definition: bitmap.cc:147
int Width() const
Definition: bitmap.h:249
bool IsGrey() const
Definition: bitmap.h:263
int Height() const
Definition: bitmap.h:250
Bitmap CloneAsGrey() const
Definition: bitmap.cc:588
std::vector< LineSegment > DetectLineSegments(const Bitmap &bitmap, const double min_length)
Definition: line.cc:42
std::vector< LineSegmentOrientation > ClassifyLineSegmentOrientations(const std::vector< LineSegment > &segments, const double tolerance)
Definition: line.cc:79