Commit 5abe3b59 authored by lluis's avatar lluis

Class-specific Extremal Region Filter algorithm as proposed in :

Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012.

High-level C++ interface and implementation of algorithm is in the objdetect module.
C++ example, a test image, and the default classifiers in xml files.
parent d81d3fc8
set(the_description "Object Detection") set(the_description "Object Detection")
ocv_define_module(objdetect opencv_core opencv_imgproc OPTIONAL opencv_highgui) ocv_define_module(objdetect opencv_core opencv_imgproc opencv_ml OPTIONAL opencv_highgui)
...@@ -394,5 +394,6 @@ CV_EXPORTS_W void drawDataMatrixCodes(InputOutputArray image, ...@@ -394,5 +394,6 @@ CV_EXPORTS_W void drawDataMatrixCodes(InputOutputArray image,
} }
#include "opencv2/objdetect/linemod.hpp" #include "opencv2/objdetect/linemod.hpp"
#include "opencv2/objdetect/erfilter.hpp"
#endif #endif
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __OPENCV_OBJDETECT_ERFILTER_HPP__
#define __OPENCV_OBJDETECT_ERFILTER_HPP__
#include "opencv2/core.hpp"
#include <vector>
#include <deque>
namespace cv
{
/*!
Extremal Region Stat structure
The ERStat structure represents a class-specific Extremal Region (ER).
An ER is a 4-connected set of pixels with all its grey-level values smaller than the values
in its outer boundary. A class-specific ER is selected (using a classifier) from all the ER's
in the component tree of the image.
*/
struct CV_EXPORTS ERStat
{
public:
//! Constructor
ERStat(int level = 256, int pixel = 0, int x = 0, int y = 0);
//! Destructor
~ERStat(){};
//! seed point and the threshold (max grey-level value)
int pixel;
int level;
//! incrementally computable features
int area;
int perimeter;
int euler; //!< euler number
int bbox[4];
double raw_moments[2]; //!< order 1 raw moments to derive the centroid
double central_moments[3]; //!< order 2 central moments to construct the covariance matrix
std::deque<int> *crossings;//!< horizontal crossings
//! 1st stage features
float aspect_ratio;
float compactness;
float num_holes;
float med_crossings;
//! 2nd stage features
float hole_area_ratio;
float convex_hull_ratio;
float num_inflexion_points;
// TODO Other features can be added (average color, standard deviation, and such)
// TODO shall we include the pixel list whenever available (i.e. after 2nd stage) ?
std::vector<int> *pixels;
//! probability that the ER belongs to the class we are looking for
double probability;
//! pointers preserving the tree structure of the component tree
ERStat* parent;
ERStat* child;
ERStat* next;
ERStat* prev;
//! wenever the regions is a local maxima of the probability
bool local_maxima;
ERStat* max_probability_ancestor;
ERStat* min_probability_ancestor;
};
/*!
Base class for 1st and 2nd stages of Neumann and Matas scene text detection algorithms
Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012
Extracts the component tree (if needed) and filter the extremal regions (ER's) by using a given classifier.
*/
class CV_EXPORTS ERFilter : public cv::Algorithm
{
public:
//! callback with the classifier is made a class. By doing it we hide SVM, Boost etc.
class CV_EXPORTS Callback
{
public:
virtual ~Callback(){};
//! The classifier must return probability measure for the region.
virtual double eval(const ERStat& stat) = 0; //const = 0; //TODO why cannot use const = 0 here?
};
/*!
the key method. Takes image on input and returns the selected regions in a vector of ERStat
only distinctive ERs which correspond to characters are selected by a sequential classifier
\param image is the input image
\param regions is output for the first stage, input/output for the second one.
*/
virtual void run( cv::InputArray image, std::vector<ERStat>& regions ) = 0;
//! set/get methods to set the algorithm properties,
virtual void setCallback(const cv::Ptr<ERFilter::Callback>& cb) = 0;
virtual void setThresholdDelta(int thresholdDelta) = 0;
virtual void setMinArea(float minArea) = 0;
virtual void setMaxArea(float maxArea) = 0;
virtual void setMinProbability(float minProbability) = 0;
virtual void setMinProbabilityDiff(float minProbabilityDiff) = 0;
virtual void setNonMaxSuppression(bool nonMaxSuppression) = 0;
virtual int getNumRejected() = 0;
};
/*!
Create an Extremal Region Filter for the 1st stage classifier of N&M algorithm
Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012
The component tree of the image is extracted by a threshold increased step by step
from 0 to 255, incrementally computable descriptors (aspect_ratio, compactness,
number of holes, and number of horizontal crossings) are computed for each ER
and used as features for a classifier which estimates the class-conditional
probability P(er|character). The value of P(er|character) is tracked using the inclusion
relation of ER across all thresholds and only the ERs which correspond to local maximum
of the probability P(er|character) are selected (if the local maximum of the
probability is above a global limit pmin and the difference between local maximum and
local minimum is greater than minProbabilityDiff).
\param cb Callback with the classifier.
if omitted tries to load a default classifier from file trained_classifierNM1.xml
\param thresholdDelta Threshold step in subsequent thresholds when extracting the component tree
\param minArea The minimum area (% of image size) allowed for retreived ER's
\param minArea The maximum area (% of image size) allowed for retreived ER's
\param minProbability The minimum probability P(er|character) allowed for retreived ER's
\param nonMaxSuppression Whenever non-maximum suppression is done over the branch probabilities
\param minProbability The minimum probability difference between local maxima and local minima ERs
*/
CV_EXPORTS cv::Ptr<ERFilter> createERFilterNM1(const cv::Ptr<ERFilter::Callback>& cb = NULL,
int thresholdDelta = 1, float minArea = 0.000025,
float maxArea = 0.13, float minProbability = 0.2,
bool nonMaxSuppression = true,
float minProbabilityDiff = 0.1);
/*!
Create an Extremal Region Filter for the 2nd stage classifier of N&M algorithm
Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012
In the second stage, the ERs that passed the first stage are classified into character
and non-character classes using more informative but also more computationally expensive
features. The classifier uses all the features calculated in the first stage and the following
additional features: hole area ratio, convex hull ratio, and number of outer inflexion points.
\param cb Callback with the classifier
if omitted tries to load a default classifier from file trained_classifierNM2.xml
\param minProbability The minimum probability P(er|character) allowed for retreived ER's
*/
CV_EXPORTS cv::Ptr<ERFilter> createERFilterNM2(const cv::Ptr<ERFilter::Callback>& cb = NULL,
float minProbability = 0.85);
}
#endif // _OPENCV_ERFILTER_HPP_
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include <fstream>
using namespace std;
namespace cv
{
ERStat::ERStat(int init_level, int init_pixel, int init_x, int init_y) : pixel(init_pixel),
level(init_level), area(0), perimeter(0), euler(0), probability(1.0),
parent(0), child(0), next(0), prev(0), local_maxima(0),
max_probability_ancestor(0), min_probability_ancestor(0)
{
bbox[0] = init_x;
bbox[1] = init_y;
bbox[2] = init_x;
bbox[3] = init_y;
raw_moments[0] = 0.0;
raw_moments[1] = 0.0;
central_moments[0] = 0.0;
central_moments[1] = 0.0;
central_moments[2] = 0.0;
crossings = new std::deque<int>();
crossings->push_back(0);
}
// derivative classes
// the classe implementing the interface for the 1st and 2nd stages of Neumann and Matas algorithm
class CV_EXPORTS ERFilterNM : public ERFilter
{
public:
//Constructor
ERFilterNM();
//Destructor
~ERFilterNM() {};
float minProbability;
bool nonMaxSuppression;
float minProbabilityDiff;
// the key method. Takes image on input, vector of ERStat is output for the first stage,
// input/output - for the second one.
void run( cv::InputArray image, std::vector<ERStat>& regions );
protected:
int thresholdDelta;
float maxArea;
float minArea;
cv::Ptr<ERFilter::Callback> classifier;
// count of the rejected/accepted regions
int num_rejected_regions;
int num_accepted_regions;
public:
// set/get methods to set the algorithm properties,
void setCallback(const cv::Ptr<ERFilter::Callback>& cb);
void setThresholdDelta(int thresholdDelta);
void setMinArea(float minArea);
void setMaxArea(float maxArea);
void setMinProbability(float minProbability);
void setMinProbabilityDiff(float minProbabilityDiff);
void setNonMaxSuppression(bool nonMaxSuppression);
int getNumRejected();
private:
// pointer to the input/output regions vector
std::vector<ERStat> *regions;
// image mask used for feature calculations
cv::Mat region_mask;
// extract the component tree and store all the ER regions
void er_tree_extract( cv::InputArray image );
// accumulate a pixel into an ER
void er_add_pixel( ERStat *parent, int x, int y, int non_boundary_neighbours,
int non_boundary_neighbours_horiz,
int d_C1, int d_C2, int d_C3 );
// merge an ER with its nested parent
void er_merge( ERStat *parent, ERStat *child );
// recursively walk the tree and clean memory
void er_tree_clean( ERStat *er );
// copy extracted regions into the output vector
ERStat* er_save( ERStat *er, ERStat *parent, ERStat *prev );
// recursively walk the tree and filter (remove) regions using the callback classifier
ERStat* er_tree_filter( cv::InputArray image, ERStat *stat, ERStat *parent, ERStat *prev );
// recursively walk the tree selecting only regions with local maxima probability
ERStat* er_tree_nonmax_suppression( ERStat *er, ERStat *parent, ERStat *prev );
};
// default 1st stage classifier
class CV_EXPORTS ERClassifierNM1 : public ERFilter::Callback
{
public:
//Constructor
ERClassifierNM1();
// Destructor
~ERClassifierNM1() {};
// The classifier must return probability measure for the region.
double eval(const ERStat& stat);
private:
CvBoost boost;
};
// default 2nd stage classifier
class CV_EXPORTS ERClassifierNM2 : public ERFilter::Callback
{
public:
//constructor
ERClassifierNM2();
// Destructor
~ERClassifierNM2() {};
// The classifier must return probability measure for the region.
double eval(const ERStat& stat);
private:
CvBoost boost;
};
// default constructor
ERFilterNM::ERFilterNM()
{
thresholdDelta = 1;
minArea = 0.;
maxArea = 1.;
minProbability = 0.;
nonMaxSuppression = false;
minProbabilityDiff = 1.;
num_accepted_regions = 0;
num_rejected_regions = 0;
classifier = NULL;
}
// the key method. Takes image on input, vector of ERStat is output for the first stage,
// input/output for the second one.
void ERFilterNM::run( cv::InputArray image, std::vector<ERStat>& _regions )
{
// assert correct image type
CV_Assert( image.getMat().type() == CV_8UC1 );
regions = &_regions;
region_mask = Mat::zeros(image.getMat().rows+2, image.getMat().cols+2, CV_8UC1);
// if regions vector is empty we must extract the entire component tree
if ( regions->size() == 0 )
{
er_tree_extract( image );
if (nonMaxSuppression)
{
vector<ERStat> aux_regions;
regions->swap(aux_regions);
regions->reserve(aux_regions.size());
er_tree_nonmax_suppression( &aux_regions.front(), NULL, NULL );
aux_regions.clear();
}
}
else // if regions vector is already filled we'll just filter the current regions
{
// the tree root must have no parent
CV_Assert( regions->front().parent == NULL );
vector<ERStat> aux_regions;
regions->swap(aux_regions);
regions->reserve(aux_regions.size());
er_tree_filter( image, &aux_regions.front(), NULL, NULL );
aux_regions.clear();
}
}
// extract the component tree and store all the ER regions
// uses the algorithm described in
// Linear time maximally stable extremal regions, D Nistér, H Stewénius – ECCV 2008
void ERFilterNM::er_tree_extract( cv::InputArray image )
{
Mat src = image.getMat();
// assert correct image type
CV_Assert( src.type() == CV_8UC1 );
if (thresholdDelta > 1)
{
Mat tmp;
src.copyTo(tmp);
src.release();
src = (image.getMat() / thresholdDelta) -1;
}
const unsigned char * image_data = src.data;
int width = src.cols, height = src.rows;
// the component stack
vector<ERStat*> er_stack;
//the quads for euler number calculation
unsigned char quads[3][4];
quads[0][0] = 1 << 3;
quads[0][1] = 1 << 2;
quads[0][2] = 1 << 1;
quads[0][3] = 1;
quads[1][0] = (1<<2)|(1<<1)|(1);
quads[1][1] = (1<<3)|(1<<1)|(1);
quads[1][2] = (1<<3)|(1<<2)|(1);
quads[1][3] = (1<<3)|(1<<2)|(1<<1);
quads[2][0] = (1<<2)|(1<<1);
quads[2][1] = (1<<3)|(1);
quads[2][3] = 255;
quads[2][4] = 255;
// masks to know if a pixel is accessible and if it has been already added to some region
vector<bool> accessible_pixel_mask(width * height);
vector<bool> accumulated_pixel_mask(width * height);
// heap of boundary pixels
vector<int> boundary_pixes[(255/thresholdDelta)+1];
vector<int> boundary_edges[(255/thresholdDelta)+1];
// add a dummy-component before start
er_stack.push_back(new ERStat);
// we'll look initially for all pixels with grey-level lower than a grey-level higher than any allowed in the image
int threshold_level = (255/thresholdDelta)+1;
// starting from the first pixel (0,0)
int current_pixel = 0;
int current_edge = 0;
int current_level = image_data[0];
accessible_pixel_mask[0] = true;
bool push_new_component = true;
for (;;) {
int x = current_pixel % width;
int y = current_pixel / width;
// push a component with current level in the component stack
if (push_new_component)
er_stack.push_back(new ERStat(current_level, current_pixel, x, y));
push_new_component = false;
// explore the (remaining) edges to the neighbors to the current pixel
for (current_edge = current_edge; current_edge < 4; current_edge++)
{
int neighbour_pixel = current_pixel;
switch (current_edge)
{
case 0: if (x < width - 1) neighbour_pixel = current_pixel + 1; break;
case 1: if (y < height - 1) neighbour_pixel = current_pixel + width; break;
case 2: if (x > 0) neighbour_pixel = current_pixel - 1; break;
default: if (y > 0) neighbour_pixel = current_pixel - width; break;
}
// if neighbour is not accessible, mark it accessible and retreive its grey-level value
if ( !accessible_pixel_mask[neighbour_pixel] && (neighbour_pixel != current_pixel) )
{
int neighbour_level = image_data[neighbour_pixel];
accessible_pixel_mask[neighbour_pixel] = true;
// if neighbour level is not lower than current level add neighbour to the boundary heap
if (neighbour_level >= current_level)
{
boundary_pixes[neighbour_level].push_back(neighbour_pixel);
boundary_edges[neighbour_level].push_back(0);
// if neighbour level is lower than our threshold_level set threshold_level to neighbour level
if (neighbour_level < threshold_level)
threshold_level = neighbour_level;
}
else // if neighbour level is lower than current add current_pixel (and next edge)
// to the boundary heap for later processing
{
boundary_pixes[current_level].push_back(current_pixel);
boundary_edges[current_level].push_back(current_edge + 1);
// if neighbour level is lower than threshold_level set threshold_level to neighbour level
if (current_level < threshold_level)
threshold_level = current_level;
// consider the new pixel and its grey-level as current pixel
current_pixel = neighbour_pixel;
current_edge = 0;
current_level = neighbour_level;
// and push a new component
push_new_component = true;
break;
}
}
} // else neigbor was already accessible
if (push_new_component) continue;
// once here we can add the current pixel to the component at the top of the stack
// but first we find how many of its neighbours are part of the region boundary (needed for
// perimeter and crossings calc.) and the increment in quads counts for euler number calc.
int non_boundary_neighbours = 0;
int non_boundary_neighbours_horiz = 0;
unsigned char quad_before[4] = {0,0,0,0};
unsigned char quad_after[4] = {0,0,0,0};
quad_after[0] = 1<<1;
quad_after[1] = 1<<3;
quad_after[2] = 1<<2;
quad_after[3] = 1;
for (int edge = 0; edge < 8; edge++)
{
int neighbour4 = -1;
int neighbour8 = -1;
int cell = 0;
switch (edge)
{
case 0: if (x < width - 1) { neighbour4 = neighbour8 = current_pixel + 1;} cell = 5; break;
case 1: if ((x < width - 1)&&(y < height - 1)) { neighbour8 = current_pixel + 1 + width;} cell = 8; break;
case 2: if (y < height - 1) { neighbour4 = neighbour8 = current_pixel + width;} cell = 7; break;
case 3: if ((x > 0)&&(y < height - 1)) { neighbour8 = current_pixel - 1 + width;} cell = 6; break;
case 4: if (x > 0) { neighbour4 = neighbour8 = current_pixel - 1;} cell = 3; break;
case 5: if ((x > 0)&&(y > 0)) { neighbour8 = current_pixel - 1 - width;} cell = 0; break;
case 6: if (y > 0) { neighbour4 = neighbour8 = current_pixel - width;} cell = 1; break;
default: if ((x < width - 1)&&(y > 0)) { neighbour8 = current_pixel + 1 - width;} cell = 2; break;
}
if ((neighbour4 != -1)&&(accumulated_pixel_mask[neighbour4])&&(image_data[neighbour4]<=image_data[current_pixel]))
{
non_boundary_neighbours++;
if ((edge == 0) || (edge == 4))
non_boundary_neighbours_horiz++;
}
int pix_value = image_data[current_pixel] + 1;
if (neighbour8 != -1)
{
if (accumulated_pixel_mask[neighbour8])
pix_value = image_data[neighbour8];
}
if (pix_value<=image_data[current_pixel])
{
switch(cell)
{
case 0:
quad_before[3] = quad_before[3] | (1<<3);
quad_after[3] = quad_after[3] | (1<<3);
break;
case 1:
quad_before[3] = quad_before[3] | (1<<2);
quad_after[3] = quad_after[3] | (1<<2);
quad_before[0] = quad_before[0] | (1<<3);
quad_after[0] = quad_after[0] | (1<<3);
break;
case 2:
quad_before[0] = quad_before[0] | (1<<2);
quad_after[0] = quad_after[0] | (1<<2);
break;
case 3:
quad_before[3] = quad_before[3] | (1<<1);
quad_after[3] = quad_after[3] | (1<<1);
quad_before[2] = quad_before[2] | (1<<3);
quad_after[2] = quad_after[2] | (1<<3);
break;
case 5:
quad_before[0] = quad_before[0] | (1);
quad_after[0] = quad_after[0] | (1);
quad_before[1] = quad_before[1] | (1<<2);
quad_after[1] = quad_after[1] | (1<<2);
break;
case 6:
quad_before[2] = quad_before[2] | (1<<1);
quad_after[2] = quad_after[2] | (1<<1);
break;
case 7:
quad_before[2] = quad_before[2] | (1);
quad_after[2] = quad_after[2] | (1);
quad_before[1] = quad_before[1] | (1<<1);
quad_after[1] = quad_after[1] | (1<<1);
break;
default:
quad_before[1] = quad_before[1] | (1);
quad_after[1] = quad_after[1] | (1);
break;
}
}
}
int C_before[3] = {0, 0, 0};
int C_after[3] = {0, 0, 0};
for (int p=0; p<3; p++)
{
for (int q=0; q<4; q++)
{
if ( (quad_before[0] == quads[p][q]) )
if ((p<2)||(q<2)) C_before[p]++;
if ( (quad_before[1] == quads[p][q]) )
if ((p<2)||(q<2)) C_before[p]++;
if ( (quad_before[2] == quads[p][q]) )
if ((p<2)||(q<2)) C_before[p]++;
if ( (quad_before[3] == quads[p][q]) )
if ((p<2)||(q<2)) C_before[p]++;
if ( (quad_after[0] == quads[p][q]) )
if ((p<2)||(q<2)) C_after[p]++;
if ( (quad_after[1] == quads[p][q]) )
if ((p<2)||(q<2)) C_after[p]++;
if ( (quad_after[2] == quads[p][q]) )
if ((p<2)||(q<2)) C_after[p]++;
if ( (quad_after[3] == quads[p][q]) )
if ((p<2)||(q<2)) C_after[p]++;
}
}
int d_C1 = C_after[0]-C_before[0];
int d_C2 = C_after[1]-C_before[1];
int d_C3 = C_after[2]-C_before[2];
er_add_pixel(er_stack.back(), x, y, non_boundary_neighbours, non_boundary_neighbours_horiz, d_C1, d_C2, d_C3);
accumulated_pixel_mask[current_pixel] = true;
// if we have processed all the possible threshold levels (the hea is empty) we are done!
if (threshold_level == (255/thresholdDelta)+1)
{
// save the extracted regions into the output vector
regions->reserve(num_accepted_regions+1);
er_save(er_stack.back(), NULL, NULL);
// clean memory
er_tree_clean(er_stack.back());
er_stack.clear();
return;
}
// pop the heap of boundary pixels
current_pixel = boundary_pixes[threshold_level].back();
boundary_pixes[threshold_level].erase(boundary_pixes[threshold_level].end()-1);
current_edge = boundary_edges[threshold_level].back();
boundary_edges[threshold_level].erase(boundary_edges[threshold_level].end()-1);
while (boundary_pixes[threshold_level].empty() && (threshold_level < (255/thresholdDelta)+1))
threshold_level++;
int new_level = image_data[current_pixel];
// if the new pixel has higher grey value than the current one
if (new_level != current_level) {
current_level = new_level;
// process components on the top of the stack until we reach the higher grey-level
while (er_stack.back()->level < new_level)
{
ERStat* er = er_stack.back();
er_stack.erase(er_stack.end()-1);
if (new_level < er_stack.back()->level)
{
er_stack.push_back(new ERStat(new_level, current_pixel, current_pixel%width, current_pixel/width));
er_merge(er_stack.back(), er);
break;
}
er_merge(er_stack.back(), er);
}
}
}
}
// accumulate a pixel into an ER
void ERFilterNM::er_add_pixel(ERStat *parent, int x, int y, int non_border_neighbours,
int non_border_neighbours_horiz,
int d_C1, int d_C2, int d_C3)
{
parent->area++;
parent->perimeter += 4 - 2*non_border_neighbours;
if (parent->crossings->size()>0)
{
if (y<parent->bbox[1]) parent->crossings->push_front(2);
else if (y>parent->bbox[3]) parent->crossings->push_back(2);
else {
parent->crossings->at(y - parent->bbox[1]) += 2-2*non_border_neighbours_horiz;
}
} else {
parent->crossings->push_back(2);
}
parent->euler += (d_C1 - d_C2 + 2*d_C3) / 4;
parent->bbox[0] = min(parent->bbox[0],x);
parent->bbox[1] = min(parent->bbox[1],y);
parent->bbox[2] = max(parent->bbox[2],x);
parent->bbox[3] = max(parent->bbox[3],y);
parent->raw_moments[0] += x;
parent->raw_moments[1] += y;
parent->central_moments[0] += x * x;
parent->central_moments[1] += x * y;
parent->central_moments[2] += y * y;
}
// merge an ER with its nested parent
void ERFilterNM::er_merge(ERStat *parent, ERStat *child)
{
parent->area += child->area;
parent->perimeter += child->perimeter;
for (int i=parent->bbox[1]; i<=min(parent->bbox[3],child->bbox[3]); i++)
if (i-child->bbox[1] >= 0)
parent->crossings->at(i-parent->bbox[1]) += child->crossings->at(i-child->bbox[1]);
for (int i=parent->bbox[1]-1; i>=child->bbox[1]; i--)
if (i-child->bbox[1] < (int)child->crossings->size())
parent->crossings->push_front(child->crossings->at(i-child->bbox[1]));
else
parent->crossings->push_front(0);
for (int i=parent->bbox[3]+1; i<child->bbox[1]; i++)
parent->crossings->push_back(0);
for (int i=max(parent->bbox[3]+1,child->bbox[1]); i<=child->bbox[3]; i++)
parent->crossings->push_back(child->crossings->at(i-child->bbox[1]));
parent->euler += child->euler;
parent->bbox[0] = min(parent->bbox[0],child->bbox[0]);
parent->bbox[1] = min(parent->bbox[1],child->bbox[1]);
parent->bbox[2] = max(parent->bbox[2],child->bbox[2]);
parent->bbox[3] = max(parent->bbox[3],child->bbox[3]);
parent->raw_moments[0] += child->raw_moments[0];
parent->raw_moments[1] += child->raw_moments[1];
parent->central_moments[0] += child->central_moments[0];
parent->central_moments[1] += child->central_moments[1];
parent->central_moments[2] += child->central_moments[2];
// child region done, we can calculate 1st stage features from the incrementally computable descriptors
child->aspect_ratio = (float)(child->bbox[2]-child->bbox[0]+1)/(child->bbox[3]-child->bbox[1]+1);
child->compactness = sqrt(child->area)/child->perimeter;
child->num_holes = (float)(1-child->euler);
vector<int> m_crossings;
m_crossings.push_back(child->crossings->at((int)(child->bbox[3]-child->bbox[1]+1)/6));
m_crossings.push_back(child->crossings->at((int)3*(child->bbox[3]-child->bbox[1]+1)/6));
m_crossings.push_back(child->crossings->at((int)5*(child->bbox[3]-child->bbox[1]+1)/6));
sort(m_crossings.begin(), m_crossings.end());
child->med_crossings = (float)m_crossings.at(1);
// free unnecessary mem
child->crossings->clear();
delete(child->crossings);
child->crossings = NULL;
// recover the original grey-level
child->level = child->level*thresholdDelta;
// before saving calculate P(child|character) and filter if possible
if (classifier != NULL)
{
child->probability = classifier->eval(*child);
}
if ( ((classifier!=NULL)?(child->probability >= minProbability):true) &&
((child->area >= (minArea*region_mask.rows*region_mask.cols)) &&
(child->area <= (maxArea*region_mask.rows*region_mask.cols))) )
{
num_accepted_regions++;
child->next = parent->child;
if (parent->child)
parent->child->prev = child;
parent->child = child;
child->parent = parent;
} else {
num_rejected_regions++;
if (child->prev !=NULL)
child->prev->next = child->next;
ERStat *new_child = child->child;
if (new_child != NULL)
{
while (new_child->next != NULL)
new_child = new_child->next;
new_child->next = parent->child;
if (parent->child)
parent->child->prev = new_child;
parent->child = child->child;
child->child->parent = parent;
}
// free mem
if(child->crossings)
{
child->crossings->clear();
delete(child->crossings);
child->crossings = NULL;
}
delete(child);
}
}
// recursively walk the tree and clean memory
void ERFilterNM::er_tree_clean( ERStat *stat )
{
for (ERStat * child = stat->child; child; child = child->next)
{
er_tree_clean(child);
}
if (stat->crossings)
{
stat->crossings->clear();
delete(stat->crossings);
stat->crossings = NULL;
}
delete stat;
}
// copy extracted regions into the output vector
ERStat* ERFilterNM::er_save( ERStat *er, ERStat *parent, ERStat *prev )
{
regions->push_back(*er);
regions->back().parent = parent;
if (prev != NULL)
prev->next = &(regions->back());
else if (parent != NULL)
parent->child = &(regions->back());
ERStat *old_prev = NULL;
ERStat *this_er = &regions->back();
if (nonMaxSuppression)
{
if (this_er->parent == NULL)
{
this_er->probability = 0; //TODO this makes sense in order to select at least one region in short tree's but is it really necessary?
this_er->max_probability_ancestor = this_er;
this_er->min_probability_ancestor = this_er;
}
else
{
this_er->max_probability_ancestor = (this_er->probability > parent->max_probability_ancestor->probability)? this_er : parent->max_probability_ancestor;
this_er->min_probability_ancestor = (this_er->probability < parent->min_probability_ancestor->probability)? this_er : parent->min_probability_ancestor;
if ( (this_er->max_probability_ancestor->probability > minProbability) && (this_er->max_probability_ancestor->probability - this_er->min_probability_ancestor->probability > minProbabilityDiff))
{
this_er->max_probability_ancestor->local_maxima = true;
//TODO check here if the last local_maxima can be also suppressed, is the following correct?
//if (this_er->min_probability_ancestor->local_maxima)
// this_er->min_probability_ancestor->local_maxima = false;
this_er->max_probability_ancestor = this_er;
this_er->min_probability_ancestor = this_er;
}
}
}
for (ERStat * child = er->child; child; child = child->next)
{
old_prev = er_save(child, this_er, old_prev);
}
return this_er;
}
// recursively walk the tree and filter (remove) regions using the callback classifier
ERStat* ERFilterNM::er_tree_filter ( cv::InputArray image, ERStat * stat, ERStat *parent, ERStat *prev )
{
Mat src = image.getMat();
// assert correct image type
CV_Assert( src.type() == CV_8UC1 );
//Fill the region and calculate 2nd stage features
Mat region = region_mask(Rect(Point(stat->bbox[0],stat->bbox[1]),Point(stat->bbox[2]+3,stat->bbox[3]+3)));
region = Scalar(0);
int newMaskVal = 255;
int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
Rect rect;
floodFill( src(Rect(Point(stat->bbox[0],stat->bbox[1]),Point(stat->bbox[2]+1,stat->bbox[3]+1))),
region, Point(stat->pixel%src.cols - stat->bbox[0], stat->pixel/src.cols - stat->bbox[1]),
Scalar(255), &rect, Scalar(stat->level), Scalar(0), flags );
rect.width += 2;
rect.height += 2;
region = region(rect);
vector<vector<Point> > contours;
vector<Point> contour_poly;
vector<Vec4i> hierarchy;
findContours( region, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE, Point(0, 0) );
//TODO check epsilon parameter of approxPolyDP (set empirically) : we want more precission
// if the region is very small because otherwise we'll loose all the convexities
approxPolyDP( Mat(contours[0]), contour_poly, max(rect.width,rect.height)/25, true );
bool was_convex = false;
int num_inflexion_points = 0;
for (int p = 0 ; p<(int)contour_poly.size(); p++)
{
int p_prev = p-1;
int p_next = p+1;
if (p_prev == -1)
p_prev = contour_poly.size()-1;
if (p_next == (int)contour_poly.size())
p_next = 0;
double angle_next = atan2((contour_poly[p_next].y-contour_poly[p].y),(contour_poly[p_next].x-contour_poly[p].x));
double angle_prev = atan2((contour_poly[p_prev].y-contour_poly[p].y),(contour_poly[p_prev].x-contour_poly[p].x));
if ( angle_next < 0 )
angle_next = 2.*CV_PI + angle_next;
double angle = (angle_next - angle_prev);
if (angle > 2.*CV_PI)
angle = angle - 2.*CV_PI;
else if (angle < 0)
angle = 2.*CV_PI + std::abs(angle);
if (p>0)
{
if ( ((angle > CV_PI)&&(!was_convex)) || ((angle < CV_PI)&&(was_convex)) )
num_inflexion_points++;
}
was_convex = (angle > CV_PI);
}
floodFill(region, Point(0,0), Scalar(255), 0);
int holes_area = region.cols*region.rows-countNonZero(region);
int hull_area = 0;
{
vector<Point> hull;
cv::convexHull(contours[0], hull, false);
hull_area = contourArea(hull);
}
stat->hole_area_ratio = (float)holes_area / stat->area;
stat->convex_hull_ratio = (float)hull_area / contourArea(contours[0]);
stat->num_inflexion_points = (float)num_inflexion_points;
// calculate P(child|character) and filter if possible
if ( (classifier != NULL) && (stat->parent != NULL) )
{
stat->probability = classifier->eval(*stat);
}
if ( ( ((classifier != NULL)?(stat->probability >= minProbability):true) &&
((stat->area >= minArea*region_mask.rows*region_mask.cols) &&
(stat->area <= maxArea*region_mask.rows*region_mask.cols)) ) ||
(stat->parent == NULL) )
{
num_accepted_regions++;
regions->push_back(*stat);
regions->back().parent = parent;
regions->back().next = NULL;
regions->back().child = NULL;
if (prev != NULL)
prev->next = &(regions->back());
else if (parent != NULL)
parent->child = &(regions->back());
ERStat *old_prev = NULL;
ERStat *this_er = &regions->back();
for (ERStat * child = stat->child; child; child = child->next)
{
old_prev = er_tree_filter(image, child, this_er, old_prev);
}
return this_er;
} else {
num_rejected_regions++;
ERStat *old_prev = prev;
for (ERStat * child = stat->child; child; child = child->next)
{
old_prev = er_tree_filter(image, child, parent, old_prev);
}
return old_prev;
}
}
// recursively walk the tree selecting only regions with local maxima probability
ERStat* ERFilterNM::er_tree_nonmax_suppression ( ERStat * stat, ERStat *parent, ERStat *prev )
{
if ( ( stat->local_maxima ) || ( stat->parent == NULL ) )
{
regions->push_back(*stat);
regions->back().parent = parent;
regions->back().next = NULL;
regions->back().child = NULL;
if (prev != NULL)
prev->next = &(regions->back());
else if (parent != NULL)
parent->child = &(regions->back());
ERStat *old_prev = NULL;
ERStat *this_er = &regions->back();
for (ERStat * child = stat->child; child; child = child->next)
{
old_prev = er_tree_nonmax_suppression( child, this_er, old_prev );
}
return this_er;
} else {
num_rejected_regions++;
num_accepted_regions--;
ERStat *old_prev = prev;
for (ERStat * child = stat->child; child; child = child->next)
{
old_prev = er_tree_nonmax_suppression( child, parent, old_prev );
}
return old_prev;
}
}
void ERFilterNM::setCallback(const Ptr<ERFilter::Callback>& cb)
{
classifier = cb;
};
void ERFilterNM::setMinArea(float _minArea)
{
CV_Assert( (_minArea >= 0) && (_minArea < maxArea) );
minArea = _minArea;
return;
};
void ERFilterNM::setMaxArea(float _maxArea)
{
CV_Assert(_maxArea <= 1);
CV_Assert(minArea < _maxArea);
maxArea = _maxArea;
return;
};
void ERFilterNM::setThresholdDelta(int _thresholdDelta)
{
CV_Assert( (_thresholdDelta > 0) && (_thresholdDelta <= 128) );
thresholdDelta = _thresholdDelta;
return;
};
void ERFilterNM::setMinProbability(float _minProbability)
{
CV_Assert( (_minProbability >= 0.0) && (_minProbability <= 1.0) );
minProbability = _minProbability;
return;
};
void ERFilterNM::setMinProbabilityDiff(float _minProbabilityDiff)
{
CV_Assert( (_minProbabilityDiff >= 0.0) && (_minProbabilityDiff <= 1.0) );
minProbabilityDiff = _minProbabilityDiff;
return;
};
void ERFilterNM::setNonMaxSuppression(bool _nonMaxSuppression)
{
nonMaxSuppression = _nonMaxSuppression;
return;
};
int ERFilterNM::getNumRejected()
{
return num_rejected_regions;
};
// load default 1st stage classifier if found
ERClassifierNM1::ERClassifierNM1()
{
if (ifstream("./trained_classifierNM1.xml"))
{
// The file with default classifier exists
boost.load("./trained_classifierNM1.xml", "boost");
}
else if (ifstream("./training/trained_classifierNM1.xml"))
{
// The file with default classifier exists
boost.load("./training/trained_classifierNM1.xml", "boost");
}
else
{
// File not found
CV_Error(CV_StsBadArg, "Default classifier ./trained_classifierNM1.xml not found!");
}
};
double ERClassifierNM1::eval(const ERStat& stat)
{
//Classify
float arr[] = {0,stat.aspect_ratio, stat.compactness, stat.num_holes, stat.med_crossings};
vector<float> sample (arr, arr + sizeof(arr) / sizeof(arr[0]) );
float votes = boost.predict( Mat(sample), Mat(), Range::all(), false, true );
// Logistic Correction returns a probability value (in the range(0,1))
return (double)1-(double)1/(1+exp(-2*votes));
};
// load default 2nd stage classifier if found
ERClassifierNM2::ERClassifierNM2()
{
if (ifstream("./trained_classifierNM2.xml"))
{
// The file with default classifier exists
boost.load("./trained_classifierNM2.xml", "boost");
}
else if (ifstream("./training/trained_classifierNM2.xml"))
{
// The file with default classifier exists
boost.load("./training/trained_classifierNM2.xml", "boost");
}
else
{
// File not found
CV_Error(CV_StsBadArg, "Default classifier ./trained_classifierNM2.xml not found!");
}
};
double ERClassifierNM2::eval(const ERStat& stat)
{
//Classify
float arr[] = {0,stat.aspect_ratio, stat.compactness, stat.num_holes, stat.med_crossings,
stat.hole_area_ratio, stat.convex_hull_ratio, stat.num_inflexion_points};
vector<float> sample (arr, arr + sizeof(arr) / sizeof(arr[0]) );
float votes = boost.predict( Mat(sample), Mat(), Range::all(), false, true );
// Logistic Correction returns a probability value (in the range(0,1))
return (double)1-(double)1/(1+exp(-2*votes));
};
/*!
Create an Extremal Region Filter for the 1st stage classifier of N&M algorithm
Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012
The component tree of the image is extracted by a threshold increased step by step
from 0 to 255, incrementally computable descriptors (aspect_ratio, compactness,
number of holes, and number of horizontal crossings) are computed for each ER
and used as features for a classifier which estimates the class-conditional
probability P(er|character). The value of P(er|character) is tracked using the inclusion
relation of ER across all thresholds and only the ERs which correspond to local maximum
of the probability P(er|character) are selected (if the local maximum of the
probability is above a global limit pmin and the difference between local maximum and
local minimum is greater than minProbabilityDiff).
\param cb Callback with the classifier.
if omitted tries to load a default classifier from file trained_classifierNM1.xml
\param thresholdDelta Threshold step in subsequent thresholds when extracting the component tree
\param minArea The minimum area (% of image size) allowed for retreived ER's
\param minArea The maximum area (% of image size) allowed for retreived ER's
\param minProbability The minimum probability P(er|character) allowed for retreived ER's
\param nonMaxSuppression Whenever non-maximum suppression is done over the branch probabilities
\param minProbability The minimum probability difference between local maxima and local minima ERs
*/
Ptr<ERFilter> createERFilterNM1(const cv::Ptr<ERFilter::Callback>& cb, int thresholdDelta,
float minArea, float maxArea, float minProbability,
bool nonMaxSuppression, float minProbabilityDiff)
{
CV_Assert( (minProbability >= 0.) && (minProbability <= 1.) );
CV_Assert( (minArea < maxArea) && (minArea >=0.) && (maxArea <= 1.) );
CV_Assert( (thresholdDelta >= 0) && (thresholdDelta <= 128) );
CV_Assert( (minProbabilityDiff >= 0.) && (minProbabilityDiff <= 1.) );
Ptr<ERFilterNM> filter = new ERFilterNM();
if (cb == NULL)
filter->setCallback(new ERClassifierNM1());
else
filter->setCallback(cb);
filter->setThresholdDelta(thresholdDelta);
filter->setMinArea(minArea);
filter->setMaxArea(maxArea);
filter->setMinProbability(minProbability);
filter->setNonMaxSuppression(nonMaxSuppression);
filter->setMinProbabilityDiff(minProbabilityDiff);
return (Ptr<ERFilter>)filter;
}
/*!
Create an Extremal Region Filter for the 2nd stage classifier of N&M algorithm
Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012
In the second stage, the ERs that passed the first stage are classified into character
and non-character classes using more informative but also more computationally expensive
features. The classifier uses all the features calculated in the first stage and the following
additional features: hole area ratio, convex hull ratio, and number of outer inflexion points.
\param cb Callback with the classifier
if omitted tries to load a default classifier from file trained_classifierNM2.xml
\param minProbability The minimum probability P(er|character) allowed for retreived ER's
*/
Ptr<ERFilter> createERFilterNM2(const cv::Ptr<ERFilter::Callback>& cb, float minProbability)
{
CV_Assert( (minProbability >= 0.) && (minProbability <= 1.) );
Ptr<ERFilterNM> filter = new ERFilterNM();
if (cb == NULL)
filter->setCallback(new ERClassifierNM2());
else
filter->setCallback(cb);
filter->setMinProbability(minProbability);
return (Ptr<ERFilter>)filter;
}
}
//--------------------------------------------------------------------------------------------------
// A demo program of the Extremal Region Filter algorithm described in
// Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012
//--------------------------------------------------------------------------------------------------
#include "opencv2/opencv.hpp"
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <vector>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace cv;
void er_draw(Mat &src, Mat &dst, ERStat& er);
void er_draw(Mat &src, Mat &dst, ERStat& er)
{
if (er.parent != NULL) // deprecate the root region
{
int newMaskVal = 255;
int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
floodFill(src,dst,Point(er.pixel%src.cols,er.pixel/src.cols),Scalar(255),0,Scalar(er.level),Scalar(0),flags);
}
}
int main(int argc, const char * argv[])
{
vector<ERStat> regions;
if (argc < 2) {
cout << "Demo program of the Extremal Region Filter algorithm described in " << endl;
cout << "Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012" << endl << endl;
cout << " Usage: " << argv[0] << " input_image <optional_groundtruth_image>" << endl;
cout << " Default classifier files (trained_classifierNM*.xml) should be in ./" << endl;
return -1;
}
Mat original = imread(argv[1]);
Mat gt;
if (argc > 2)
{
gt = imread(argv[2]);
cvtColor(gt, gt, COLOR_RGB2GRAY);
threshold(gt, gt, 254, 255, THRESH_BINARY);
}
Mat grey(original.size(),CV_8UC1);
cvtColor(original,grey,COLOR_RGB2GRAY);
double t = (double)getTickCount();
// Build ER tree and filter with the 1st stage default classifier
Ptr<ERFilter> er_filter1 = createERFilterNM1();
er_filter1->run(grey, regions);
t = (double)getTickCount() - t;
cout << " --------------------------------------------------------------------------------------------------" << endl;
cout << "\t FIRST STAGE CLASSIFIER done in " << t * 1000. / getTickFrequency() << " ms." << endl;
cout << " --------------------------------------------------------------------------------------------------" << endl;
cout << setw(9) << regions.size()+er_filter1->getNumRejected() << "\t Extremal Regions extracted " << endl;
cout << setw(9) << regions.size() << "\t Extremal Regions selected by the first stage of the sequential classifier." << endl;
cout << "\t \t (saving into out_second_stage.jpg)" << endl;
cout << " --------------------------------------------------------------------------------------------------" << endl;
er_filter1.release();
// draw regions
Mat mask = Mat::zeros(grey.rows+2,grey.cols+2,CV_8UC1);
for (int r=0; r<(int)regions.size(); r++)
er_draw(grey, mask, regions.at(r));
mask = 255-mask;
imwrite("out_first_stage.jpg", mask);
if (argc > 2)
{
Mat tmp_mask = (255-gt) & (255-mask(Rect(Point(1,1),Size(mask.cols-2,mask.rows-2))));
cout << "Recall for the 1st stage filter = " << (float)countNonZero(tmp_mask) / countNonZero(255-gt) << endl;
}
t = (double)getTickCount();
// Default second stage classifier
Ptr<ERFilter> er_filter2 = createERFilterNM2();
er_filter2->run(grey, regions);
t = (double)getTickCount() - t;
cout << " --------------------------------------------------------------------------------------------------" << endl;
cout << "\t SECOND STAGE CLASSIFIER done in " << t * 1000. / getTickFrequency() << " ms." << endl;
cout << " --------------------------------------------------------------------------------------------------" << endl;
cout << setw(9) << regions.size() << "\t Extremal Regions selected by the second stage of the sequential classifier." << endl;
cout << "\t \t (saving into out_second_stage.jpg)" << endl;
cout << " --------------------------------------------------------------------------------------------------" << endl;
er_filter2.release();
// draw regions
mask = mask*0;
for (int r=0; r<(int)regions.size(); r++)
er_draw(grey, mask, regions.at(r));
mask = 255-mask;
imwrite("out_second_stage.jpg", mask);
if (argc > 2)
{
Mat tmp_mask = (255-gt) & (255-mask(Rect(Point(1,1),Size(mask.cols-2,mask.rows-2))));
cout << "Recall for the 2nd stage filter = " << (float)countNonZero(tmp_mask) / countNonZero(255-gt) << endl;
}
regions.clear();
}
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
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