mlpack
iou_metric_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_METRICS_IOU_IMPL_HPP
13 #define MLPACK_CORE_METRICS_IOU_IMPL_HPP
14 
15 // In case it hasn't been included.
16 #include "iou_metric.hpp"
17 
18 namespace mlpack {
19 namespace metric {
20 
21 template<bool UseCoordinates>
22 template <typename VecTypeA, typename VecTypeB>
23 typename VecTypeA::elem_type IoU<UseCoordinates>::Evaluate(
24  const VecTypeA& a,
25  const VecTypeB& b)
26 {
27  Log::Assert(a.n_elem == b.n_elem && a.n_elem == 4, "Incorrect \
28  shape for bounding boxes. They must contain 4 elements either be \
29  {x0, y0, x1, y1} or {x0, y0, h, w}. Refer to the documentation \
30  for more information.");
31 
32  // Bounding boxes represented as {x0, y0, x1, y1}.
33  if (UseCoordinates)
34  {
35  // Check the correctness of bounding box.
36  if (a(0) >= a(2) || a(1) >= a(3) || b(0) >= b(2) || b(1) >= b(3))
37  {
38  Log::Fatal << "Check the correctness of bounding boxes i.e. " <<
39  "{x0, y0} must represent lower left coordinates and " <<
40  "{x1, y1} must represent upper right coordinates of bounding" <<
41  "box." << std::endl;
42  }
43 
44  typename VecTypeA::elem_type interSectionArea = std::max(0.0,
45  std::min(a(2), b(2)) - std::max(a(0), b(0)) + 1) * std::max(0.0,
46  std::min(a(3), b(3)) - std::max(a(1), b(1)) + 1);
47 
48  // Union of Area can be calculated using the following equation
49  // A union B = A + B - A intersection B.
50  return interSectionArea / (1.0 * ((a(2) - a(0) + 1) * (a(3) - a(1) + 1) +
51  (b(2) - b(0) + 1) * (b(3) - b(1) + 1) - interSectionArea));
52  }
53 
54  // Bounding boxes represented as {x0, y0, h, w}.
55  // Check correctness of bounding box.
56  Log::Assert(a(2) > 0 && b(2) > 0 && a(3) > 0 && b(3) > 0, "Height and width \
57  of bounding boxes must be greater than zero.");
58 
59  typename VecTypeA::elem_type interSectionArea = std::max(0.0,
60  std::min(a(0) + a(2), b(0) + b(2)) - std::max(a(0), b(0)) + 1)
61  * std::max(0.0, std::min(a(1) + a(3), b(1) + b(3)) - std::max(a(1),
62  b(1)) + 1);
63 
64  return interSectionArea / (1.0 * ((a(2) + 1) * (a(3) + 1) + (b(2) + 1) *
65  (b(3) + 1) - interSectionArea));
66 }
67 template<bool UseCoordinates>
68 template<typename Archive>
70  Archive& /* ar */,
71  const uint32_t /* version */)
72 {
73  // Nothing to do here.
74 }
75 
76 } // namespace metric
77 } // namespace mlpack
78 #endif
static VecTypeA::elem_type Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the Intersection over Union metric between of two bounding boxes having pattern bx...
Definition: iou_metric_impl.hpp:23
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void serialize(Archive &ar, const uint32_t)
Serialize the metric.
Definition: iou_metric_impl.hpp:69
static void Assert(bool condition, const std::string &message="Assert Failed.")
Checks if the specified condition is true.
Definition: log.cpp:38