Commit 290de9b4 authored by Davis King's avatar Davis King

Added some functions for creating detection templates.

parent 51c0c148
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "image_processing/scan_image.h" #include "image_processing/scan_image.h"
#include "image_processing/scan_image_pyramid.h" #include "image_processing/scan_image_pyramid.h"
#include "image_processing/detection_template_tools.h"
#endif // DLIB_IMAGE_PROCESSInG_H___ #endif // DLIB_IMAGE_PROCESSInG_H___
......
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_DETECTION_TEMPlATE_TOOLS_H__
#define DLIB_DETECTION_TEMPlATE_TOOLS_H__
#include "detection_template_tools_abstract.h"
#include "../geometry.h"
#include "../matrix.h"
#include <utility>
#include <vector>
#include <cmath>
namespace dlib
{
// ----------------------------------------------------------------------------------------
inline rectangle compute_box_dimensions (
const double width_to_height_ratio,
const double area
)
{
// make sure requires clause is not broken
DLIB_ASSERT(width_to_height_ratio > 0 && area > 0,
"\t rectangle compute_box_dimensions()"
<< "\n\t Invalid arguments were given to this function. "
<< "\n\t width_to_height_ratio: " << width_to_height_ratio
<< "\n\t area: " << area
);
/*
width*height == area
width/height == width_to_height_ratio
*/
using namespace std;
const int height = std::floor(std::sqrt(area/width_to_height_ratio) + 0.5);
const int width = std::floor(area/height + 0.5);
return centered_rect(0,0,width,height);
}
// ----------------------------------------------------------------------------------------
inline std::vector<rectangle> create_single_box_detection_template (
const rectangle& object_box
)
{
std::vector<rectangle> temp;
temp.push_back(object_box);
return temp;
}
// ----------------------------------------------------------------------------------------
inline std::vector<rectangle> create_overlapped_2x2_detection_template (
const rectangle& object_box
)
{
std::vector<rectangle> result;
const point c = center(object_box);
result.push_back(rectangle() + c + object_box.tl_corner() + object_box.tr_corner());
result.push_back(rectangle() + c + object_box.bl_corner() + object_box.br_corner());
result.push_back(rectangle() + c + object_box.tl_corner() + object_box.bl_corner());
result.push_back(rectangle() + c + object_box.tr_corner() + object_box.br_corner());
return result;
}
// ----------------------------------------------------------------------------------------
inline std::vector<rectangle> create_grid_detection_template (
const rectangle& object_box,
unsigned int cells_x,
unsigned int cells_y
)
{
// make sure requires clause is not broken
DLIB_ASSERT(cells_x > 0 && cells_y > 0,
"\t std::vector<rectangle> create_grid_detection_template()"
<< "\n\t The number of cells along a dimension can't be zero. "
<< "\n\t cells_x: " << cells_x
<< "\n\t cells_y: " << cells_y
);
std::vector<rectangle> result;
const matrix<double,1> x = linspace(object_box.left(), object_box.right(), cells_x+1);
const matrix<double,1> y = linspace(object_box.top(), object_box.bottom(), cells_y+1);
for (long j = 0; j+1 < y.size(); ++j)
{
for (long i = 0; i+1 < x.size(); ++i)
{
const dlib::vector<double,2> tl(x(i),y(j));
const dlib::vector<double,2> br(x(i+1),y(j+1));
result.push_back(rectangle(tl,br));
}
}
return result;
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_DETECTION_TEMPlATE_TOOLS_H__
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_DETECTION_TEMPlATE_TOOLS_ABSTRACT_H__
#ifdef DLIB_DETECTION_TEMPlATE_TOOLS_ABSTRACT_H__
#include "../geometry.h"
#include <utility>
#include <vector>
namespace dlib
{
// ----------------------------------------------------------------------------------------
rectangle compute_box_dimensions (
const double width_to_height_ratio,
const double area
);
/*!
requires
- area > 0
- width_to_height_ratio > 0
ensures
- returns a rectangle with the given area and width_to_height_ratio.
- In particular, returns a rectangle R such that:
- R.area() == area (to within integer precision)
- R.width()/R.height() == width_to_height_ratio (to within integer precision)
- center(R) == point(0,0)
!*/
// ----------------------------------------------------------------------------------------
std::vector<rectangle> create_single_box_detection_template (
const rectangle& object_box
);
/*!
ensures
- returns a vector that contains only object_box.
- In particular, returns a vector V such that:
- V.size() == 1
- V[0] == object_box
!*/
// ----------------------------------------------------------------------------------------
std::vector<rectangle> create_overlapped_2x2_detection_template (
const rectangle& object_box
);
/*!
ensures
- Divides object_box up into four overlapping regions, the
top half, bottom half, left half, and right half. These
four rectangles are returned inside a std::vector.
- In particular, returns a vector V such that:
- V.size() == 4
- V[0] == top half of object_box
- V[1] == bottom half of object_box
- V[2] == left half of object_box
- V[3] == right half of object_box
- for all valid i: object_box.contains(V[i]) == true
!*/
// ----------------------------------------------------------------------------------------
std::vector<rectangle> create_grid_detection_template (
const rectangle& object_box,
unsigned int cells_x,
unsigned int cells_y
);
/*!
requires
- cells_x > 0
- cells_y > 0
ensures
- Divides object_box up into a grid and returns a vector
containing all the rectangles corresponding to elements
of the grid. Moreover, the grid will be cells_x elements
wide and cells_y elements tall.
- In particular, returns a vector V such that:
- V.size() == cells_x*cells_y
- for all valid i:
- object_box.contains(V[i]) == true
- V[i] == The rectangle corresponding to the ith grid
element.
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_DETECTION_TEMPlATE_TOOLS_ABSTRACT_H__
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment