|
degate 0.1.1
|
This class implements the matching of gate representing images on a background image. More...
#include <TemplateMatching.h>

Classes | |
| struct | match_found |
| struct | prepared_template |
| struct | search_state |
Public Member Functions | |
| TemplateMatching () | |
| virtual | ~TemplateMatching () |
| double | get_threshold_hc () const |
| Get the correlation threshold for the hill climbing start phase. | |
| void | set_threshold_hc (double t) |
| Set the correlation threshold for the hill climbing. | |
| double | get_threshold_detection () const |
| Get the correlation threshold for acepting gate recognitions. | |
| void | set_threshold_detection (double t) |
| Set the correlation threshold for acepting gate recognitions. | |
| unsigned int | get_max_step_size () const |
| Get the pixel step size. | |
| void | set_max_step_size (unsigned int s) |
| Set the pixel step size. | |
| unsigned int | get_scaling_factor () const |
| Get the scaling factor. | |
| void | set_scaling_factor (unsigned int factor) |
| Set the scaling factor. | |
| virtual void | init (BoundingBox const &bounding_box, Project_shptr project) |
| Run the template matching. | |
| virtual void | run () |
| Run the template matching. | |
| void | set_templates (std::list< GateTemplate_shptr > tmpl_set) |
| Set templates that should be matched. | |
| void | set_orientations (std::list< Gate::ORIENTATION > tmpl_orientations) |
| Set orientations that should be tested. | |
| void | set_layers (Layer_shptr layer_matching, Layer_shptr layer_insert) |
| Set the layers. | |
| unsigned int | get_number_of_hits () const |
Protected Member Functions | |
| virtual bool | get_next_pos (struct search_state *state, struct prepared_template const &tmpl) const =0 |
| Calculate the next position for a template to background matching. | |
Protected Attributes | |
| struct TemplateMatchingStatistics | stats |
| Project_shptr | project |
| Layer_shptr | layer_matching |
| Layer_shptr | layer_insert |
Private Member Functions | |
| void | prepare_sum_tables (TileImage_GS_BYTE_shptr gs_img_normal, TileImage_GS_BYTE_shptr gs_img_scaled) |
| void | precalc_sum_tables (TileImage_GS_BYTE_shptr img, TileImage_GS_DOUBLE_shptr summation_table_single, TileImage_GS_DOUBLE_shptr summation_table_squared) |
| BoundingBox | get_scaled_bounding_box (BoundingBox const &bounding_box, double scale_down) const |
| void | prepare_background_images (ScalingManager_shptr sm, BoundingBox const &bounding_box, unsigned int scaling_factor) |
| struct prepared_template | prepare_template (GateTemplate_shptr tmpl, Gate::ORIENTATION orientation) |
| void | hill_climbing (unsigned int start_x, unsigned int start_y, double xcorr_val, unsigned int *max_corr_x_out, unsigned int *max_corr_y_out, double *max_xcorr_out, const TileImage_GS_BYTE_shptr master, const TempImage_GS_DOUBLE_shptr zero_mean_template, double sum_over_zero_mean_template) const |
| void | adjust_step_size (struct search_state &state, double corr_val) const |
| Adjust step size depending on correlation value. | |
| std::list< match_found > | match_single_template (struct prepared_template &tmpl, double threshold_hc, double threshold_detection) |
| double | subtract_mean (TempImage_GS_BYTE_shptr img, TempImage_GS_DOUBLE_shptr zero_mean_img) const |
| Calculate a zero mean image from an image and return the variance(?). | |
| double | calc_single_xcorr (const TileImage_GS_BYTE_shptr master, const TileImage_GS_DOUBLE_shptr summation_table_single, const TileImage_GS_DOUBLE_shptr summation_table_squared, const TempImage_GS_DOUBLE_shptr zero_mean_template, double sum_over_zero_mean_template, unsigned int local_x, unsigned int local_y) const |
| Calculate correlation between template and background. | |
| bool | add_gate (unsigned int x, unsigned int y, GateTemplate_shptr tmpl, Gate::ORIENTATION orientation, double corr_val=0, double t_hc=0) |
| match_found | keep_gate_match (unsigned int x, unsigned int y, struct prepared_template &tmpl, double corr_val=0, double t_hc=0) const |
Private Attributes | |
| double | threshold_hc |
| double | threshold_detection |
| unsigned int | max_step_size_search |
| unsigned int | scale_down |
| TileImage_GS_BYTE_shptr | gs_img_normal |
| TileImage_GS_BYTE_shptr | gs_img_scaled |
| TileImage_GS_DOUBLE_shptr | sum_table_single_normal |
| TileImage_GS_DOUBLE_shptr | sum_table_squared_normal |
| TileImage_GS_DOUBLE_shptr | sum_table_single_scaled |
| TileImage_GS_DOUBLE_shptr | sum_table_squared_scaled |
| BoundingBox | bounding_box |
| std::list< GateTemplate_shptr > | tmpl_set |
| std::list< Gate::ORIENTATION > | tmpl_orientations |
| clock_t | start |
| clock_t | finish |
| std::list< match_found > | matches |
This class implements the matching of gate representing images on a background image.
Definition at line 62 of file TemplateMatching.h.
| TemplateMatching::TemplateMatching | ( | ) |
Definition at line 45 of file TemplateMatching.cc.
References max_step_size_search, scale_down, threshold_detection, and threshold_hc.
{
threshold_hc = 0.40;
threshold_detection = 0.70;
max_step_size_search = 3;
scale_down = 1;
}
| TemplateMatching::~TemplateMatching | ( | ) | [virtual] |
Definition at line 52 of file TemplateMatching.cc.
{
}
| bool TemplateMatching::add_gate | ( | unsigned int | x, |
| unsigned int | y, | ||
| GateTemplate_shptr | tmpl, | ||
| Gate::ORIENTATION | orientation, | ||
| double | corr_val = 0, |
||
| double | t_hc = 0 |
||
| ) | [private] |
Definition at line 435 of file TemplateMatching.cc.
References degate::TemplateMatchingStatistics::hits, layer_insert, project, and stats.
Referenced by run().
{
if(!layer_insert->exists_type_in_region<Gate>(x, x + tmpl->get_width(),
y, y + tmpl->get_height())) {
Gate_shptr gate(new Gate(x, x + tmpl->get_width(),
y, y + tmpl->get_height(),
orientation));
char dsc[100];
snprintf(dsc, sizeof(dsc), "matched with corr=%.2f t_hc=%.2f", corr_val, threshold_hc);
gate->set_description(dsc);
gate->set_gate_template(tmpl);
LogicModel_shptr lmodel = project->get_logic_model();
lmodel->add_object(layer_insert, gate);
lmodel->update_ports(gate);
stats.hits++;
return true;
}
return false;
}

| void TemplateMatching::adjust_step_size | ( | struct search_state & | state, |
| double | corr_val | ||
| ) | const [private] |
Adjust step size depending on correlation value.
Definition at line 412 of file TemplateMatching.cc.
References get_max_step_size(), and degate::TemplateMatching::search_state::step_size_search.
Referenced by match_single_template().
{
if(corr_val > 0) {
state.step_size_search = std::max(1.0, rint((1.0 - (double)get_max_step_size()) *
corr_val + get_max_step_size()));
}
else state.step_size_search = get_max_step_size();
}


| double TemplateMatching::calc_single_xcorr | ( | const TileImage_GS_BYTE_shptr | master, |
| const TileImage_GS_DOUBLE_shptr | summation_table_single, | ||
| const TileImage_GS_DOUBLE_shptr | summation_table_squared, | ||
| const TempImage_GS_DOUBLE_shptr | zero_mean_template, | ||
| double | sum_over_zero_mean_template, | ||
| unsigned int | local_x, | ||
| unsigned int | local_y | ||
| ) | const [private] |
Calculate correlation between template and background.
| master | The image where we look for matchings. |
| summation_table_single | |
| summation_table_squared | |
| zero_mean_template | |
| sum_over_zero_mean_template | |
| local_x | Coordinate within master. |
| local_y | Coordinate within master. |
Definition at line 600 of file TemplateMatching.cc.
Referenced by hill_climbing(), and match_single_template().
{
double template_size = zero_mean_template->get_width() * zero_mean_template->get_height();
assert(zero_mean_template->get_width() > 0 && zero_mean_template->get_height() > 0);
unsigned int
x_plus_w = local_x + zero_mean_template->get_width() -1,
y_plus_h = local_y + zero_mean_template->get_height() -1,
lxm1 = local_x - 1, // can wrap, it's checked later
lym1 = local_y - 1;
// calculate denominator
double
f1 = summation_table_single->get_pixel(x_plus_w, y_plus_h),
f2 = summation_table_squared->get_pixel(x_plus_w, y_plus_h);
if(local_x > 0) {
f1 -= summation_table_single->get_pixel(lxm1, y_plus_h);
f2 -= summation_table_squared->get_pixel(lxm1, y_plus_h);
}
if(local_y > 0) {
f1 -= summation_table_single->get_pixel(x_plus_w, lym1);
f2 -= summation_table_squared->get_pixel(x_plus_w, lym1);
}
if(local_x > 0 && local_y > 0) {
f1 += summation_table_single->get_pixel(lxm1, lym1);
f2 += summation_table_squared->get_pixel(lxm1, lym1);
}
double denominator = sqrt((f2 - f1*f1/template_size) * sum_over_zero_mean_template);
// calculate nummerator
if(std::isinf(denominator) || std::isnan(denominator) || denominator == 0) {
debug(TM,
"ERROR: The denominator is not a valid number: f1=%f f2=%f template_size=%f sum=%f "
"local_x=%d local_y=%d x_plus_w=%d y_plus_h=%d lxm1=%d lym1=%d",
f1, f2, template_size, sum_over_zero_mean_template,
local_x, local_y, x_plus_w, y_plus_h, lxm1, lym1);
return -1.0;
}
unsigned int _x, _y;
double nummerator = 0;
for(_y = 0; _y < zero_mean_template->get_height(); _y ++) {
for(_x = 0; _x < zero_mean_template->get_width(); _x ++) {
double f_xy = master->get_pixel(_x + local_x, _y + local_y);
double t_xy = zero_mean_template->get_pixel(_x, _y);
nummerator += f_xy * t_xy;
}
}
double q = nummerator/denominator;
//debug(TM, "x=%d,y=%d a=%f, nummerator=%f, denominator=%f, q=%f", local_x, local_y, a, nummerator, denominator, q);
if(!(q >= -1.000001 && q <= 1.000001)) {
debug(TM, "nummerator = %f / denominator = %f", nummerator, denominator);
}
assert(q >= -1.1 && q <= 1.1);
return q;
}


| unsigned int degate::TemplateMatching::get_max_step_size | ( | ) | const [inline] |
Get the pixel step size.
The correlation is not calculated for all (x,y). Instead the algorithm skips pixel. The skipping depends on the correalation values. In areas of higher correlation the step size is decremented. Else in correlation valleys the step size is incremented. It is incremented up to a limit. With this method you can set the limit.
The image might be scaled before. This step size limit is in the scale of the background image after(!) scaling.
Definition at line 272 of file TemplateMatching.h.
References max_step_size_search.
Referenced by adjust_step_size(), hill_climbing(), and match_single_template().
{ return max_step_size_search; }

| virtual bool degate::TemplateMatching::get_next_pos | ( | struct search_state * | state, |
| struct prepared_template const & | tmpl | ||
| ) | const [protected, pure virtual] |
Calculate the next position for a template to background matching.
Implemented in degate::TemplateMatchingNormal, degate::TemplateMatchingAlongGrid, degate::TemplateMatchingInRows, and degate::TemplateMatchingInCols.
Referenced by match_single_template().

| unsigned int degate::TemplateMatching::get_number_of_hits | ( | ) | const [inline] |
Definition at line 330 of file TemplateMatching.h.
References degate::TemplateMatchingStatistics::hits, and stats.
{
return stats.hits;
}
| BoundingBox TemplateMatching::get_scaled_bounding_box | ( | BoundingBox const & | bounding_box, |
| double | scale_down | ||
| ) | const [private] |
Definition at line 117 of file TemplateMatching.cc.
References degate::BoundingBox::get_max_x(), degate::BoundingBox::get_max_y(), degate::BoundingBox::get_min_x(), degate::BoundingBox::get_min_y(), and scale_down.
Referenced by prepare_background_images().
{
return BoundingBox(lrint(bounding_box.get_min_x() / scale_down),
lrint(bounding_box.get_max_x() / scale_down),
lrint(bounding_box.get_min_y() / scale_down),
lrint(bounding_box.get_max_y() / scale_down));
}


| unsigned int degate::TemplateMatching::get_scaling_factor | ( | ) | const [inline] |
Get the scaling factor.
Definition at line 285 of file TemplateMatching.h.
References scale_down.
Referenced by init(), match_single_template(), prepare_background_images(), and prepare_template().
{ return scale_down; }

| double degate::TemplateMatching::get_threshold_detection | ( | ) | const [inline] |
Get the correlation threshold for acepting gate recognitions.
Definition at line 251 of file TemplateMatching.h.
References threshold_detection.
{ return threshold_detection; }
| double degate::TemplateMatching::get_threshold_hc | ( | ) | const [inline] |
Get the correlation threshold for the hill climbing start phase.
Definition at line 240 of file TemplateMatching.h.
References threshold_hc.
{ return threshold_hc; }
| void TemplateMatching::hill_climbing | ( | unsigned int | start_x, |
| unsigned int | start_y, | ||
| double | xcorr_val, | ||
| unsigned int * | max_corr_x_out, | ||
| unsigned int * | max_corr_y_out, | ||
| double * | max_xcorr_out, | ||
| const TileImage_GS_BYTE_shptr | master, | ||
| const TempImage_GS_DOUBLE_shptr | zero_mean_template, | ||
| double | sum_over_zero_mean_template | ||
| ) | const [private] |
Definition at line 525 of file TemplateMatching.cc.
References calc_single_xcorr(), get_max_step_size(), sum_table_single_normal, and sum_table_squared_normal.
Referenced by match_single_template().
{
unsigned int max_corr_x = start_x;
unsigned int max_corr_y = start_y;
double max_corr = xcorr_val;
bool running = true;
//std::list<std::pair<unsigned int, unsigned int> > positions;
const unsigned int radius = get_max_step_size();
const unsigned int size = (2 * radius + 1) * (2 * radius + 1);
unsigned int positions_x[size], positions_y[size];
while(running) {
running = false;
// first generate positions
unsigned int
from_x = max_corr_x >= radius ? max_corr_x - radius : 0,
from_y = max_corr_y >= radius ? max_corr_y - radius : 0,
to_x = max_corr_x + radius < master->get_width() ? max_corr_x + radius : master->get_width(),
to_y = max_corr_y + radius < master->get_height() ? max_corr_y + radius : master->get_height();
unsigned int i = 0;
for(unsigned int _y = from_y; _y < to_y; _y++)
for(unsigned int _x = from_x; _x < to_x; _x++) {
if(max_corr_x != _x && max_corr_y != _y) {
//positions.push_back(std::pair<unsigned int, unsigned int>(_x, _y));
positions_x[i] = _x;
positions_y[i] = _y;
i++;
}
}
// check for position with highest correlation value
for(unsigned int i2 = 0; i2 < i; i2++) {
unsigned int x = positions_x[i2], y = positions_y[i2];
//debug(TM, "hill climbing step at (%d,%d)", x, y);
double curr_corr_val = calc_single_xcorr(master,
sum_table_single_normal,
sum_table_squared_normal,
zero_mean_template,
sum_over_zero_mean_template,
x, y);
if(curr_corr_val > max_corr) {
max_corr_x = x;
max_corr_y = y;
max_corr = curr_corr_val;
running = true;
//positions.clear();
}
}
}
*max_corr_x_out = max_corr_x;
*max_corr_y_out = max_corr_y;
*max_xcorr_out = max_corr;
}


| void TemplateMatching::init | ( | BoundingBox const & | bounding_box, |
| Project_shptr | project | ||
| ) | [virtual] |
Run the template matching.
Implements degate::Matching.
Definition at line 92 of file TemplateMatching.cc.
References bounding_box, debug(), degate::BoundingBox::get_max_x(), degate::BoundingBox::get_max_y(), get_scaling_factor(), gs_img_normal, gs_img_scaled, layer_matching, LENGTH_TO_MAX, prepare_background_images(), prepare_sum_tables(), project, degate::ProgressControl::reset_progress(), degate::BoundingBox::set_max_x(), degate::BoundingBox::set_max_y(), and TM.
{
assert(project != NULL);
this->project = project;
this->bounding_box = bounding_box;
// limit bounding box
if(this->bounding_box.get_max_x() + 1 > (int)project->get_width())
this->bounding_box.set_max_x(LENGTH_TO_MAX(project->get_width()));
if(this->bounding_box.get_max_y() + 1 > (int)project->get_height())
this->bounding_box.set_max_y(LENGTH_TO_MAX(project->get_height()));
ScalingManager_shptr sm = layer_matching->get_scaling_manager();
debug(TM, "Prepare background.");
prepare_background_images(sm, bounding_box, get_scaling_factor());
debug(TM, "Prepare sum tabes.");
prepare_sum_tables(gs_img_normal, gs_img_scaled);
reset_progress();
}

| TemplateMatching::match_found TemplateMatching::keep_gate_match | ( | unsigned int | x, |
| unsigned int | y, | ||
| struct prepared_template & | tmpl, | ||
| double | corr_val = 0, |
||
| double | t_hc = 0 |
||
| ) | const [private] |
Definition at line 422 of file TemplateMatching.cc.
References degate::TemplateMatching::match_found::correlation, degate::TemplateMatching::prepared_template::gate_template, degate::TemplateMatching::prepared_template::orientation, degate::TemplateMatching::match_found::orientation, degate::TemplateMatching::match_found::t_hc, threshold_hc, degate::TemplateMatching::match_found::tmpl, degate::TemplateMatching::match_found::x, and degate::TemplateMatching::match_found::y.
Referenced by match_single_template().
{
match_found hit;
hit.x = x;
hit.y = y;
hit.tmpl = tmpl.gate_template;
hit.correlation = corr_val;
hit.t_hc = threshold_hc;
hit.orientation = tmpl.orientation;
return hit;
}

| std::list< TemplateMatching::match_found > TemplateMatching::match_single_template | ( | struct prepared_template & | tmpl, |
| double | threshold_hc, | ||
| double | threshold_detection | ||
| ) | [private] |
Definition at line 464 of file TemplateMatching.cc.
References adjust_step_size(), bounding_box, calc_single_xcorr(), debug(), get_max_step_size(), degate::BoundingBox::get_min_x(), degate::BoundingBox::get_min_y(), get_next_pos(), get_scaling_factor(), gs_img_normal, gs_img_scaled, hill_climbing(), degate::ProgressControl::is_canceled(), keep_gate_match(), matches, degate::TemplateMatching::search_state::search_area, degate::TemplateMatching::search_state::step_size_search, degate::TemplateMatching::prepared_template::sum_over_zero_mean_template_normal, degate::TemplateMatching::prepared_template::sum_over_zero_mean_template_scaled, sum_table_single_scaled, sum_table_squared_scaled, threshold_hc, TM, degate::TemplateMatching::search_state::x, degate::TemplateMatching::search_state::y, degate::TemplateMatching::prepared_template::zero_mean_template_normal, and degate::TemplateMatching::prepared_template::zero_mean_template_scaled.
Referenced by run().
{
debug(TM, "match_single_template(): start iterating over background image");
search_state state;
memset(&state, 0, sizeof(search_state));
state.x = 1;
state.y = 1;
state.step_size_search = get_max_step_size();
state.search_area = bounding_box;
std::list<match_found> matches;
double max_corr_for_search = -1;
do { // works on unscaled, but cropped image
double corr_val = calc_single_xcorr(gs_img_scaled,
sum_table_single_scaled,
sum_table_squared_scaled,
tmpl.zero_mean_template_scaled,
tmpl.sum_over_zero_mean_template_scaled,
lrint((double)state.x / get_scaling_factor()),
lrint((double)state.y / get_scaling_factor()));
/*
debug(TM, "%d,%d == %d,%d -> %f", state.x, state.y,
lrint((double)state.x / get_scaling_factor()),
lrint((double)state.y / get_scaling_factor()),
corr_val);
*/
if(corr_val > max_corr_for_search) max_corr_for_search = corr_val;
adjust_step_size(state, corr_val);
if(corr_val >= threshold_hc) {
//debug(TM, "start hill climbing at(%d,%d), corr=%f", state.x, state.y, corr_val);
unsigned int max_corr_x, max_corr_y;
double curr_max_val;
hill_climbing(state.x, state.y, corr_val,
&max_corr_x, &max_corr_y, &curr_max_val,
gs_img_normal, tmpl.zero_mean_template_normal,
tmpl.sum_over_zero_mean_template_normal);
//debug(TM, "hill climbing returned for (%d,%d) corr=%f", max_corr_x, max_corr_y, curr_max_val);
if(curr_max_val >= threshold_detection) {
matches.push_back(keep_gate_match(max_corr_x + bounding_box.get_min_x(),
max_corr_y + bounding_box.get_min_y(),
tmpl, curr_max_val, threshold_hc));
}
}
} while(get_next_pos(&state, tmpl) && !is_canceled());
std::cout << "The maximum correlation value for the current template and orientation is " << max_corr_for_search << std::endl;
return matches;
}


| void TemplateMatching::precalc_sum_tables | ( | TileImage_GS_BYTE_shptr | img, |
| TileImage_GS_DOUBLE_shptr | summation_table_single, | ||
| TileImage_GS_DOUBLE_shptr | summation_table_squared | ||
| ) | [private] |
Definition at line 55 of file TemplateMatching.cc.
Referenced by prepare_sum_tables().
{
unsigned int x, y;
for(y = 0; y < img->get_height(); y++)
for(x = 0; x < img->get_width(); x++) {
summation_table_single->set_pixel(x, y, 0);
summation_table_squared->set_pixel(x, y, 0);
}
for(y = 0; y < img->get_height(); y++)
for(x = 0; x < img->get_width(); x++) {
double f = img->get_pixel_as<gs_double_pixel_t>(x, y);
double s_l = x > 0 ? summation_table_single->get_pixel(x - 1, y) : 0;
double s_o = y > 0 ? summation_table_single->get_pixel(x, y - 1) : 0;
double s_lo = x > 0 && y > 0 ? summation_table_single->get_pixel(x - 1, y - 1) : 0;
double s2_l = x > 0 ? summation_table_squared->get_pixel(x - 1, y) : 0;
double s2_o = y > 0 ? summation_table_squared->get_pixel(x, y - 1) : 0;
double s2_lo = x > 0 && y > 0 ? summation_table_squared->get_pixel(x - 1, y - 1) : 0;
double f1 = f + s_l + s_o - s_lo;
double f2 = f*f + s2_l + s2_o - s2_lo;
summation_table_single->set_pixel(x, y, f1);
summation_table_squared->set_pixel(x, y, f2);
}
}

| void TemplateMatching::prepare_background_images | ( | ScalingManager_shptr | sm, |
| BoundingBox const & | bounding_box, | ||
| unsigned int | scaling_factor | ||
| ) | [private] |
Definition at line 125 of file TemplateMatching.cc.
References degate::convolve(), degate::extract_partial_image(), degate::BoundingBox::get_height(), get_scaled_bounding_box(), get_scaling_factor(), degate::BoundingBox::get_width(), gs_img_normal, gs_img_scaled, and degate::median_filter().
Referenced by init().
{
// Get the normal background image and the scaled background image
// These images are in RGBA format.
const ScalingManager<BackgroundImage>::image_map_element i1 =
sm->get_image(1);
const ScalingManager<BackgroundImage>::image_map_element i2 =
sm->get_image(scaling_factor);
assert(i1.second != NULL);
assert(i2.second != NULL);
assert(i2.first == get_scaling_factor());
BackgroundImage_shptr img_normal = i1.second;
BackgroundImage_shptr img_scaled = i2.second;
// Create a greyscaled image for the normal
// unscaled background image and the scaled version.
BoundingBox scaled_bounding_box =
get_scaled_bounding_box(bounding_box, scaling_factor);
gs_img_normal = TileImage_GS_BYTE_shptr(new TileImage_GS_BYTE(bounding_box.get_width(),
bounding_box.get_height()));
#ifdef USE_FILTER
TileImage_GS_BYTE_shptr tmp;
tmp = TileImage_GS_BYTE_shptr(new TileImage_GS_BYTE(bounding_box.get_width(),
bounding_box.get_height()));;
extract_partial_image(tmp, img_normal, bounding_box);
#ifdef USE_MEDIAN_FILTER
median_filter(gs_img_normal, tmp, USE_MEDIAN_FILTER);
#elif defined(USE_GAUSS_FILTER)
int blur_kernel_size = USE_GAUSS_FILTER;
std::tr1::shared_ptr<GaussianBlur>
gaussian_blur_kernel(new GaussianBlur(blur_kernel_size, blur_kernel_size, 1.1));
gaussian_blur_kernel->print();
convolve(gs_img_normal, tmp, gaussian_blur_kernel);
#endif
#else
extract_partial_image(gs_img_normal, img_normal, bounding_box);
#endif
if(scaling_factor == 1)
gs_img_scaled = gs_img_normal;
else {
gs_img_scaled = TileImage_GS_BYTE_shptr(new TileImage_GS_BYTE(scaled_bounding_box.get_width(),
scaled_bounding_box.get_height()));
#ifdef USE_MEDIAN_FILTER
tmp = TileImage_GS_BYTE_shptr(new TileImage_GS_BYTE(bounding_box.get_width(),
bounding_box.get_height()));;
extract_partial_image(tmp, img_scaled, scaled_bounding_box);
median_filter(gs_img_scaled, tmp, USE_MEDIAN_FILTER);
#else
extract_partial_image(gs_img_scaled, img_scaled, scaled_bounding_box);
#endif
}
//save_image("/tmp/xxx1.tif", gs_img_normal);
//save_image("/tmp/xxx2.tif", gs_img_scaled);
}


| void TemplateMatching::prepare_sum_tables | ( | TileImage_GS_BYTE_shptr | gs_img_normal, |
| TileImage_GS_BYTE_shptr | gs_img_scaled | ||
| ) | [private] |
Definition at line 210 of file TemplateMatching.cc.
References precalc_sum_tables(), sum_table_single_normal, sum_table_single_scaled, sum_table_squared_normal, and sum_table_squared_scaled.
Referenced by init().
{
unsigned int
w_n = gs_img_normal->get_width(),
h_n = gs_img_normal->get_height(),
w_s = gs_img_scaled->get_width(),
h_s = gs_img_scaled->get_height();
sum_table_single_normal = TileImage_GS_DOUBLE_shptr(new TileImage_GS_DOUBLE(w_n, h_n));
sum_table_squared_normal = TileImage_GS_DOUBLE_shptr(new TileImage_GS_DOUBLE(w_n, h_n));
sum_table_single_scaled = TileImage_GS_DOUBLE_shptr(new TileImage_GS_DOUBLE(w_s, h_s));
sum_table_squared_scaled = TileImage_GS_DOUBLE_shptr(new TileImage_GS_DOUBLE(w_s, h_s));
precalc_sum_tables(gs_img_normal, sum_table_single_normal, sum_table_squared_normal);
precalc_sum_tables(gs_img_scaled, sum_table_single_scaled, sum_table_squared_scaled);
}


| TemplateMatching::prepared_template TemplateMatching::prepare_template | ( | GateTemplate_shptr | tmpl, |
| Gate::ORIENTATION | orientation | ||
| ) | [read, private] |
Definition at line 330 of file TemplateMatching.cc.
References degate::copy_image(), degate::flip_both(), degate::flip_left_right(), degate::flip_up_down(), degate::TemplateMatching::prepared_template::gate_template, get_scaling_factor(), layer_matching, degate::TemplateMatching::prepared_template::orientation, degate::Gate::ORIENTATION_FLIPPED_BOTH, degate::Gate::ORIENTATION_FLIPPED_LEFT_RIGHT, degate::Gate::ORIENTATION_FLIPPED_UP_DOWN, degate::Gate::ORIENTATION_NORMAL, degate::Gate::ORIENTATION_UNDEFINED, degate::scale_down_by_power_of_2(), subtract_mean(), degate::TemplateMatching::prepared_template::sum_over_zero_mean_template_normal, degate::TemplateMatching::prepared_template::sum_over_zero_mean_template_scaled, degate::TemplateMatching::prepared_template::tmpl_img_normal, degate::TemplateMatching::prepared_template::tmpl_img_scaled, degate::Layer::UNDEFINED, degate::TemplateMatching::prepared_template::zero_mean_template_normal, and degate::TemplateMatching::prepared_template::zero_mean_template_scaled.
Referenced by run().
{
prepared_template prep;
assert(layer_matching->get_layer_type() != Layer::UNDEFINED);
assert(tmpl->has_image(layer_matching->get_layer_type()));
prep.gate_template = tmpl;
prep.orientation = orientation;
// get image from template
GateTemplateImage_shptr tmpl_img_orig = tmpl->get_image(layer_matching->get_layer_type());
unsigned int
w = tmpl_img_orig->get_width(),
h = tmpl_img_orig->get_height();
// get image according to orientation
GateTemplateImage_shptr tmpl_img(new GateTemplateImage(w, h));
//#ifdef USE_MEDIAN_FILERX
// median_filter(tmpl_img, tmpl_img_orig, 3);
//#else
copy_image(tmpl_img, tmpl_img_orig);
//#endif
//save_image("/tmp/xxx3.tif", tmpl_img);
switch(orientation) {
case Gate::ORIENTATION_FLIPPED_UP_DOWN:
flip_up_down(tmpl_img);
break;
case Gate::ORIENTATION_FLIPPED_LEFT_RIGHT:
flip_left_right(tmpl_img);
break;
case Gate::ORIENTATION_FLIPPED_BOTH:
flip_both(tmpl_img);
break;
case Gate::ORIENTATION_NORMAL:
break;
case Gate::ORIENTATION_UNDEFINED:
assert(1 == 0); // it is already checked
}
// create a scaled version of the template image
unsigned int
scaled_tmpl_width = (double)w / get_scaling_factor(),
scaled_tmpl_height = (double)h / get_scaling_factor();
prep.tmpl_img_normal = TempImage_GS_BYTE_shptr(new TempImage_GS_BYTE(w, h));
copy_image(prep.tmpl_img_normal, tmpl_img);
prep.tmpl_img_scaled = TempImage_GS_BYTE_shptr(new TempImage_GS_BYTE(scaled_tmpl_width,
scaled_tmpl_height));
scale_down_by_power_of_2(prep.tmpl_img_scaled, tmpl_img);
// create zero-mean templates
prep.zero_mean_template_normal = TempImage_GS_DOUBLE_shptr(new TempImage_GS_DOUBLE(w, h));
prep.zero_mean_template_scaled = TempImage_GS_DOUBLE_shptr(new TempImage_GS_DOUBLE(scaled_tmpl_width,
scaled_tmpl_height));
// subtract mean
prep.sum_over_zero_mean_template_normal = subtract_mean(prep.tmpl_img_normal,
prep.zero_mean_template_normal);
prep.sum_over_zero_mean_template_scaled = subtract_mean(prep.tmpl_img_scaled,
prep.zero_mean_template_scaled);
assert(prep.sum_over_zero_mean_template_normal > 0);
assert(prep.sum_over_zero_mean_template_scaled > 0);
return prep;
}


| void TemplateMatching::run | ( | ) | [virtual] |
Run the template matching.
Implements degate::Matching.
Definition at line 251 of file TemplateMatching.cc.
References add_gate(), compare_correlation(), degate::TemplateMatching::match_found::correlation, debug(), degate::ProgressControl::is_canceled(), match_single_template(), matches, degate::TemplateMatching::match_found::orientation, prepare_template(), degate::ProgressControl::progress_step_done(), degate::TemplateMatchingStatistics::reset(), degate::ProgressControl::reset_progress(), degate::ProgressControl::set_log_message(), degate::ProgressControl::set_progress_step_size(), stats, degate::TemplateMatching::match_found::t_hc, threshold_detection, threshold_hc, TM, degate::TemplateMatching::match_found::tmpl, tmpl_orientations, tmpl_set, degate::TemplateMatching::match_found::x, and degate::TemplateMatching::match_found::y.
{
debug(TM, "run template matching");
std::list<match_found> matches;
stats.reset();
set_progress_step_size(1.0/(tmpl_set.size() * tmpl_orientations.size()));
/*
BOOST_FOREACH(GateTemplate_shptr tmpl, tmpl_set) {
BOOST_FOREACH(Gate::ORIENTATION orientation, tmpl_orientations) {
add_task( run2, parameter)
}
}
wait;
*/
BOOST_FOREACH(GateTemplate_shptr tmpl, tmpl_set) {
BOOST_FOREACH(Gate::ORIENTATION orientation, tmpl_orientations) {
boost::format f("Check cell \"%1%\"");
f % tmpl->get_name();
set_log_message(f.str());
prepared_template prep_tmpl_img = prepare_template(tmpl, orientation);
//match_single_template(prep_tmpl_img, t_hc, t_det);
std::list<match_found> m = match_single_template(prep_tmpl_img,
threshold_hc,
threshold_detection);
matches.insert(matches.end(), m.begin(), m.end());
progress_step_done();
if(is_canceled()) {
reset_progress();
return;
}
}
}
matches.sort(compare_correlation);
BOOST_FOREACH(match_found const& m, matches) {
std::cout << "Try to insert gate of type " << m.tmpl->get_name() << " with corr="
<< m.correlation << " at " << m.x << "," << m.y << std::endl;
if(add_gate(m.x, m.y, m.tmpl, m.orientation, m.correlation, m.t_hc))
std::cout << "\tInserted gate of type " << m.tmpl->get_name() << std::endl;
}
reset_progress();
}

| void degate::TemplateMatching::set_layers | ( | Layer_shptr | layer_matching, |
| Layer_shptr | layer_insert | ||
| ) | [inline] |
Set the layers.
Definition at line 325 of file TemplateMatching.h.
References layer_insert, and layer_matching.
{
this->layer_matching = layer_matching;
this->layer_insert = layer_insert;
}
| void degate::TemplateMatching::set_max_step_size | ( | unsigned int | s | ) | [inline] |
Set the pixel step size.
Definition at line 278 of file TemplateMatching.h.
References max_step_size_search.
{ max_step_size_search = s; }
| void TemplateMatching::set_orientations | ( | std::list< Gate::ORIENTATION > | tmpl_orientations | ) |
Set orientations that should be tested.
Definition at line 243 of file TemplateMatching.cc.
References debug(), TM, and tmpl_orientations.
{
this->tmpl_orientations = tmpl_orientations;
for(std::list<Gate::ORIENTATION>::const_iterator iter = tmpl_orientations.begin();
iter != tmpl_orientations.end(); ++iter) {
debug(TM, "Template orientation to match: %d", *iter);
}
}

| void degate::TemplateMatching::set_scaling_factor | ( | unsigned int | factor | ) | [inline] |
Set the scaling factor.
Definition at line 291 of file TemplateMatching.h.
References scale_down.
{ scale_down = factor; }
| void TemplateMatching::set_templates | ( | std::list< GateTemplate_shptr > | tmpl_set | ) |
Set templates that should be matched.
Templates become sorted, so that larger templates are matched first.
Definition at line 238 of file TemplateMatching.cc.
References compare_template_size(), and tmpl_set.
{
this->tmpl_set = tmpl_set;
this->tmpl_set.sort(compare_template_size);
}

| void degate::TemplateMatching::set_threshold_detection | ( | double | t | ) | [inline] |
Set the correlation threshold for acepting gate recognitions.
Definition at line 257 of file TemplateMatching.h.
References threshold_detection.
{ threshold_detection = t; }
| void degate::TemplateMatching::set_threshold_hc | ( | double | t | ) | [inline] |
Set the correlation threshold for the hill climbing.
Definition at line 246 of file TemplateMatching.h.
References threshold_hc.
{ threshold_hc = t; }
| double TemplateMatching::subtract_mean | ( | TempImage_GS_BYTE_shptr | img, |
| TempImage_GS_DOUBLE_shptr | zero_mean_img | ||
| ) | const [private] |
Calculate a zero mean image from an image and return the variance(?).
Definition at line 311 of file TemplateMatching.cc.
References degate::average().
Referenced by prepare_template().
{
double mean = average(img);
double sum_over_zero_mean_img = 0;
unsigned int x, y;
for(y = 0; y < img->get_height(); y++)
for(x = 0; x < img->get_width(); x++) {
double tmp = img->get_pixel_as<gs_double_pixel_t>(x, y) - mean;
zero_mean_img->set_pixel(x, y, tmp);
sum_over_zero_mean_img += tmp * tmp;
}
return sum_over_zero_mean_img;
}


Definition at line 126 of file TemplateMatching.h.
Referenced by init(), and match_single_template().
clock_t degate::TemplateMatching::finish [private] |
Definition at line 131 of file TemplateMatching.h.
Definition at line 117 of file TemplateMatching.h.
Referenced by init(), match_single_template(), and prepare_background_images().
Definition at line 118 of file TemplateMatching.h.
Referenced by init(), match_single_template(), and prepare_background_images().
Layer_shptr degate::TemplateMatching::layer_insert [protected] |
Definition at line 140 of file TemplateMatching.h.
Referenced by add_gate(), degate::TemplateMatchingInCols::get_next_pos(), degate::TemplateMatchingInRows::get_next_pos(), degate::TemplateMatchingNormal::get_next_pos(), and set_layers().
Layer_shptr degate::TemplateMatching::layer_matching [protected] |
Definition at line 139 of file TemplateMatching.h.
Referenced by init(), prepare_template(), and set_layers().
std::list<match_found> degate::TemplateMatching::matches [private] |
Definition at line 133 of file TemplateMatching.h.
Referenced by match_single_template(), and run().
unsigned int degate::TemplateMatching::max_step_size_search [private] |
Definition at line 113 of file TemplateMatching.h.
Referenced by get_max_step_size(), set_max_step_size(), and TemplateMatching().
Project_shptr degate::TemplateMatching::project [protected] |
Definition at line 137 of file TemplateMatching.h.
Referenced by add_gate(), init(), and degate::TemplateMatchingAlongGrid::initialize_state_struct().
unsigned int degate::TemplateMatching::scale_down [private] |
Definition at line 114 of file TemplateMatching.h.
Referenced by get_scaled_bounding_box(), get_scaling_factor(), set_scaling_factor(), and TemplateMatching().
clock_t degate::TemplateMatching::start [private] |
Definition at line 131 of file TemplateMatching.h.
struct TemplateMatchingStatistics degate::TemplateMatching::stats [protected] |
Definition at line 96 of file TemplateMatching.h.
Referenced by add_gate(), get_number_of_hits(), and run().
Definition at line 121 of file TemplateMatching.h.
Referenced by hill_climbing(), and prepare_sum_tables().
Definition at line 123 of file TemplateMatching.h.
Referenced by match_single_template(), and prepare_sum_tables().
Definition at line 122 of file TemplateMatching.h.
Referenced by hill_climbing(), and prepare_sum_tables().
Definition at line 124 of file TemplateMatching.h.
Referenced by match_single_template(), and prepare_sum_tables().
double degate::TemplateMatching::threshold_detection [private] |
Definition at line 112 of file TemplateMatching.h.
Referenced by get_threshold_detection(), run(), set_threshold_detection(), and TemplateMatching().
double degate::TemplateMatching::threshold_hc [private] |
Definition at line 111 of file TemplateMatching.h.
Referenced by get_threshold_hc(), keep_gate_match(), match_single_template(), run(), set_threshold_hc(), and TemplateMatching().
std::list<Gate::ORIENTATION> degate::TemplateMatching::tmpl_orientations [private] |
Definition at line 129 of file TemplateMatching.h.
Referenced by run(), and set_orientations().
std::list<GateTemplate_shptr> degate::TemplateMatching::tmpl_set [private] |
Definition at line 128 of file TemplateMatching.h.
Referenced by run(), and set_templates().
1.7.4