vendor: OpenCV 5.0.0 snapshot at 755e50675d97db9b7d449d8bd6b09888646f6c6e

This commit is contained in:
Gitea Mirror Bot
2026-08-22 00:11:13 +08:00
commit 12022378a3
3872 changed files with 2513409 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
set(the_description "Text Detection and Recognition")
set(__extra_deps "")
if(DEBUG_opencv_text)
list(APPEND __extra_deps PRIVATE_REQUIRED opencv_highgui)
endif()
ocv_define_module(text
opencv_ml opencv_imgproc opencv_core opencv_features opencv_geometry opencv_dnn
${__extra_deps}
WRAP
python
java
objc
)
if(TARGET ocv.3rdparty.tesseract)
ocv_target_link_libraries(${the_module} LINK_PRIVATE ocv.3rdparty.tesseract)
endif()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/text_config.hpp.in
${CMAKE_BINARY_DIR}/text_config.hpp @ONLY)
ocv_include_directories(${CMAKE_CURRENT_BINARY_DIR})
ocv_add_testdata(samples/ contrib/text
FILES_MATCHING PATTERN "*.xml" PATTERN "*.xml.gz" REGEX "scenetext[0-9]+.jpg"
)
+58
View File
@@ -0,0 +1,58 @@
Scene Text Detection and Recognition in Natural Scene Images
============================================================
The module contains algorithms to detect text, segment words and recognise the text.
It's mainly intended for the "text in the wild", i.e. short phrases and separate words that occur on navigation signs and such. It's not an OCR tool for scanned documents, do not treat it as such.
The detection part can in theory handle different languages, but will likely fail on hieroglyphic texts.
The recognition part currently uses open-source Tesseract OCR (https://code.google.com/p/tesseract-ocr/). If Tesseract OCR is not installed on your system, the corresponding part of the functionality will be unavailable.
Here are instructions on how to install Tesseract on your machine (Linux or Mac; Windows users should look for precompiled binaries or try to adopt the instructions below):
Tesseract installation instruction (Linux, Mac)
-----------------------------------------------
0. Linux users may try to install tesseract-3.03-rc1 (or later) and leptonica-1.70 (or later) with the corresponding development packages using their package manager. Mac users may try brew. The instructions below are for those who wants to build tesseract from source.
1. download leptonica 1.70 tarball (helper image processing library, used by tesseract. Later versions might work too):
http://www.leptonica.com/download.html
unpack and build it:
cd leptonica-1.70
mkdir build && cd build && ../configure && make && sudo make install
leptonica will be installed to /usr/local.
2. download tesseract-3.03-rc1 tarball from https://drive.google.com/folderview?id=0B7l10Bj_LprhQnpSRkpGMGV2eE0&usp=sharing
unpack and build it:
# needed only to build tesseract
export LIBLEPT_HEADERSDIR=/usr/local/include/
cd tesseract-3.03
mkdir build && cd build
../configure --with-extra-includes=/usr/local --with-extra-libraries=/usr/local
make && sudo make install
Tesseract will be installed to /usr/local.
3. download the pre-trained classifier data for English language:
https://code.google.com/p/tesseract-ocr/downloads/detail?name=eng.traineddata.gz
unzip it (gzip -d eng.traineddata.gz) and copy to /usr/local/share/tessdata.
Notes
-----
1. Google announced that they close code.google.com, so at some moment in the future you may have to find Tesseract 3.03rc1 or later.
2. Tesseract configure script may fail to detect leptonica, so you may have to edit the configure script - comment off some if's around this message and retain only "then" branch.
3. You are encouraged to search the Net for some better pre-trained classifiers, as well as classifiers for other languages.
Text Detection CNN
=================
Intro
-----
The text module now have a text detection and recognition using deep CNN. The text detector deep CNN that takes an image which may contain multiple words. This outputs a list of Rects with bounding boxes and probability of text there. The text recognizer provides a probabillity over a given vocabulary for each of these rects.
+24
View File
@@ -0,0 +1,24 @@
# Tesseract OCR
ocv_check_modules(Tesseract tesseract) # lept is excluded (not a direct dependency)
if(NOT Tesseract_FOUND)
find_path(Tesseract_INCLUDE_DIR tesseract/baseapi.h
HINTS
/usr/include
/usr/local/include)
find_library(Tesseract_LIBRARY NAMES tesseract
HINTS
/usr/lib
/usr/local/lib)
find_library(Lept_LIBRARY NAMES lept
HINTS
/usr/lib
/usr/local/lib)
if(Tesseract_INCLUDE_DIR AND Tesseract_LIBRARY AND Lept_LIBRARY)
set(Tesseract_INCLUDE_DIRS ${Tesseract_INCLUDE_DIR})
set(Tesseract_LIBRARIES ${Tesseract_LIBRARY} ${Lept_LIBRARY})
set(Tesseract_FOUND 1)
endif()
endif()
@@ -0,0 +1,12 @@
#if !defined(USE_STD_NAMESPACE)
#define USE_STD_NAMESPACE
#endif
#include <tesseract/baseapi.h>
#include <tesseract/resultiterator.h>
static void test()
{
tesseract::TessBaseAPI tess;
}
int main() { test(); return 0; }
+59
View File
@@ -0,0 +1,59 @@
OCV_OPTION(WITH_TESSERACT "Include Tesseract OCR library support" (NOT CMAKE_CROSSCOMPILING)
VERIFY HAVE_TESSERACT)
if(NOT HAVE_TESSERACT
AND (WITH_TESSERACT OR OPENCV_FIND_TESSERACT)
)
if(NOT Tesseract_FOUND)
find_package(Tesseract QUIET) # Prefer CMake's standard locations (including Tesseract_DIR)
endif()
if(NOT Tesseract_FOUND)
include("${CMAKE_CURRENT_LIST_DIR}/FindTesseract.cmake") # OpenCV's fallback
endif()
if(Tesseract_FOUND)
if(Tesseract_VERSION)
message(STATUS "Tesseract: YES (ver ${Tesseract_VERSION})")
else()
message(STATUS "Tesseract: YES (ver unknown)")
endif()
if(NOT ENABLE_CXX11 AND NOT OPENCV_SKIP_TESSERACT_BUILD_CHECK)
try_compile(__VALID_TESSERACT
"${OpenCV_BINARY_DIR}/cmake_check/tesseract"
"${CMAKE_CURRENT_LIST_DIR}/checks/tesseract_test.cpp"
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${Tesseract_INCLUDE_DIRS}"
LINK_LIBRARIES ${Tesseract_LIBRARIES}
OUTPUT_VARIABLE TRY_OUT
)
if(NOT __VALID_TESSERACT)
if(OPENCV_DEBUG_TESSERACT_BUILD)
message(STATUS "${TRY_OUT}")
endif()
message(STATUS "Can't use Tesseract (details: https://github.com/opencv/opencv_contrib/pull/2220)")
return()
endif()
endif()
set(HAVE_TESSERACT 1)
set(HAVE_TESSERACT 1)
# TODO use ocv_add_external_target
set(name "tesseract")
set(inc "${Tesseract_INCLUDE_DIRS}")
set(link "${Tesseract_LIBRARIES}")
set(def "")
if(BUILD_SHARED_LIBS)
set(imp IMPORTED)
endif()
add_library(ocv.3rdparty.${name} INTERFACE ${imp})
set_target_properties(ocv.3rdparty.${name} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${inc}"
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${inc}"
INTERFACE_LINK_LIBRARIES "${link}"
INTERFACE_COMPILE_DEFINITIONS "${def}")
if(NOT BUILD_SHARED_LIBS)
install(TARGETS ocv.3rdparty.${name} EXPORT OpenCVModules)
endif()
else()
message(STATUS "Tesseract: NO")
endif()
endif()
Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

+52
View File
@@ -0,0 +1,52 @@
@inproceedings{Neumann12,
title={Scene Text Localization and Recognition},
author={Neumann and L., Matas and J.},
journal={ Computer Vision and Pattern Recognition (CVPR), 2012 IEEE Conference on},
pages={3538--3545},
year={2012},
organization={IEEE}
}
@inproceedings{Neumann11,
author = {Lukáš Neumann and Jiří Matas},
title = {Text localization in real-world images using efficiently pruned exhaustive search},
booktitle = {in Document Analysis and Recognition, 2011 International Conference on. IEEE, 2011},
year = {},
pages = {687--691}
}
@inproceedings{Gomez13,
author = {G\'{o}mez, Llu\'{\i}s and Karatzas, Dimosthenis},
title={Multi-script Text Extraction from Natural Scenes},
booktitle = {Proceedings of the 2013 12th International Conference on Document Analysis and Recognition},
series = {ICDAR '13},
year = {2013},
isbn = {978-0-7695-4999-6},
pages = {467--471},
publisher = {IEEE Computer Society}
}
@article{Gomez14,
author = {Lluis Gomez i Bigorda and
Dimosthenis Karatzas},
title = {A Fast Hierarchical Method for Multi-script and Arbitrary Oriented
Scene Text Extraction},
journal = {CoRR},
volume = {abs/1407.7504},
year = {2014},
}
@inproceedings{LiaoSBWL17,
author = {Minghui Liao and
Baoguang Shi and
Xiang Bai and
Xinggang Wang and
Wenyu Liu},
title = {TextBoxes: {A} Fast Text Detector with a Single Deep Neural Network},
booktitle = {AAAI},
year = {2017}
}
@inproceedings{LiaoSBWL17,
author = {Boris Epshtein and
Eyal Ofek and
Yonatan Wexler and},
title = {Detecting Text in Natural Scenes with Stroke Width Transform},
booktitle = {CVPR},
year = {2010}
}
+103
View File
@@ -0,0 +1,103 @@
/*
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
(3-clause BSD License)
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:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions 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.
* Neither the names of the copyright holders nor the names of the contributors
may 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 copyright holders 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.
*/
#ifndef __OPENCV_TEXT_HPP__
#define __OPENCV_TEXT_HPP__
#include "opencv2/text/erfilter.hpp"
#include "opencv2/text/ocr.hpp"
#include "opencv2/text/textDetector.hpp"
#include "opencv2/text/swt_text_detection.hpp"
/** @defgroup text Scene Text Detection and Recognition
The opencv_text module provides different algorithms for text detection and recognition in natural
scene images.
@{
@defgroup text_detect Scene Text Detection
Class-specific Extremal Regions for Scene Text Detection
--------------------------------------------------------
The scene text detection algorithm described below has been initially proposed by Lukás Neumann &
Jiri Matas @cite Neumann11. The main idea behind Class-specific Extremal Regions is similar to the MSER
in that suitable Extremal Regions (ERs) are selected from the whole component tree of the image.
However, this technique differs from MSER in that selection of suitable ERs is done by a sequential
classifier trained for character detection, i.e. dropping the stability requirement of MSERs and
selecting class-specific (not necessarily stable) regions.
The component tree of an image is constructed by thresholding by an increasing value step-by-step
from 0 to 255 and then linking the obtained connected components from successive levels in a
hierarchy by their inclusion relation:
![image](pics/component_tree.png)
The component tree may contain a huge number of regions even for a very simple image as shown in
the previous image. This number can easily reach the order of 1 x 10\^6 regions for an average 1
Megapixel image. In order to efficiently select suitable regions among all the ERs the algorithm
make use of a sequential classifier with two differentiated stages.
In the first stage incrementally computable descriptors (area, perimeter, bounding box, and Euler's
number) are computed (in O(1)) for each region r and used as features for a classifier which
estimates the class-conditional probability p(r|character). Only the ERs which correspond to local
maximum of the probability p(r|character) are selected (if their probability is above a global limit
p_min and the difference between local maximum and local minimum is greater than a delta_min
value).
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. (Hole
area ratio, convex hull ratio, and the number of outer boundary inflexion points).
This ER filtering process is done in different single-channel projections of the input image in
order to increase the character localization recall.
After the ER filtering is done on each input channel, character candidates must be grouped in
high-level text blocks (i.e. words, text lines, paragraphs, ...). The opencv_text module implements
two different grouping algorithms: the Exhaustive Search algorithm proposed in @cite Neumann12 for
grouping horizontally aligned text, and the method proposed by Lluis Gomez and Dimosthenis Karatzas
in @cite Gomez13 @cite Gomez14 for grouping arbitrary oriented text (see erGrouping).
To see the text detector at work, have a look at the textdetection demo:
<https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/textdetection.cpp>
@defgroup text_recognize Scene Text Recognition
@}
*/
#endif
@@ -0,0 +1,375 @@
/*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_TEXT_ERFILTER_HPP__
#define __OPENCV_TEXT_ERFILTER_HPP__
#include "opencv2/core.hpp"
#include <vector>
#include <deque>
#include <string>
namespace cv
{
namespace text
{
//! @addtogroup text_detect
//! @{
/** @brief 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
explicit 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's number
Rect rect;
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
Ptr<std::deque<int> > crossings;//!< horizontal crossings
float med_crossings; //!< median of the crossings at three different height levels
//! 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;
//! whenever the regions is a local maxima of the probability
bool local_maxima;
ERStat* max_probability_ancestor;
ERStat* min_probability_ancestor;
};
/** @brief Base class for 1st and 2nd stages of Neumann and Matas scene text detection algorithm @cite Neumann12. :
Extracts the component tree (if needed) and filter the extremal regions (ER's) by using a given classifier.
*/
class CV_EXPORTS_W ERFilter : public Algorithm
{
public:
/** @brief Callback with the classifier is made a class.
By doing it we hide SVM, Boost etc. Developers can provide their own classifiers to the
ERFilter algorithm.
*/
class CV_EXPORTS_W Callback
{
public:
virtual ~Callback() { }
/** @brief The classifier must return probability measure for the region.
@param stat : The region to be classified
*/
virtual double eval(const ERStat& stat) = 0; //const = 0; //TODO why cannot use const = 0 here?
};
/** @brief The key method of ERFilter algorithm.
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 Single channel image CV_8UC1
@param regions Output for the 1st stage and Input/Output for the 2nd. The selected Extremal Regions
are stored here.
Extracts the component tree (if needed) and filter the extremal regions (ER's) by using a given
classifier.
*/
virtual void run( InputArray image, std::vector<ERStat>& regions ) = 0;
//! set/get methods to set the algorithm properties,
virtual void setCallback(const 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() const = 0;
};
/** @brief Create an Extremal Region Filter for the 1st stage classifier of N&M algorithm @cite Neumann12.
@param cb : Callback with the classifier. Default classifier can be implicitly load with function
loadClassifierNM1, e.g. from file in samples/cpp/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 maxArea : 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 minProbabilityDiff : The minimum probability difference between local maxima and local minima ERs
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).
*/
CV_EXPORTS_W Ptr<ERFilter> createERFilterNM1(const Ptr<ERFilter::Callback>& cb,
int thresholdDelta = 1, float minArea = (float)0.00025,
float maxArea = (float)0.13, float minProbability = (float)0.4,
bool nonMaxSuppression = true,
float minProbabilityDiff = (float)0.1);
/** @brief Create an Extremal Region Filter for the 2nd stage classifier of N&M algorithm @cite Neumann12.
@param cb : Callback with the classifier. Default classifier can be implicitly load with function
loadClassifierNM2, e.g. from file in samples/cpp/trained_classifierNM2.xml
@param minProbability : The minimum probability P(er|character) allowed for retreived ER's
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.
*/
CV_EXPORTS_W Ptr<ERFilter> createERFilterNM2(const Ptr<ERFilter::Callback>& cb,
float minProbability = (float)0.3);
/** @brief Reads an Extremal Region Filter for the 1st stage classifier of N&M algorithm
from the provided path e.g. /path/to/cpp/trained_classifierNM1.xml
@overload
*/
CV_EXPORTS_W Ptr<ERFilter> createERFilterNM1(const String& filename,
int thresholdDelta = 1, float minArea = (float)0.00025,
float maxArea = (float)0.13, float minProbability = (float)0.4,
bool nonMaxSuppression = true,
float minProbabilityDiff = (float)0.1);
/** @brief Reads an Extremal Region Filter for the 2nd stage classifier of N&M algorithm
from the provided path e.g. /path/to/cpp/trained_classifierNM2.xml
@overload
*/
CV_EXPORTS_W Ptr<ERFilter> createERFilterNM2(const String& filename,
float minProbability = (float)0.3);
/** @brief Allow to implicitly load the default classifier when creating an ERFilter object.
@param filename The XML or YAML file with the classifier model (e.g. trained_classifierNM1.xml)
returns a pointer to ERFilter::Callback.
*/
CV_EXPORTS_W Ptr<ERFilter::Callback> loadClassifierNM1(const String& filename);
/** @brief Allow to implicitly load the default classifier when creating an ERFilter object.
@param filename The XML or YAML file with the classifier model (e.g. trained_classifierNM2.xml)
returns a pointer to ERFilter::Callback.
*/
CV_EXPORTS_W Ptr<ERFilter::Callback> loadClassifierNM2(const String& filename);
//! computeNMChannels operation modes
enum { ERFILTER_NM_RGBLGrad,
ERFILTER_NM_IHSGrad
};
/** @brief Compute the different channels to be processed independently in the N&M algorithm @cite Neumann12.
@param _src Source image. Must be RGB CV_8UC3.
@param _channels Output vector\<Mat\> where computed channels are stored.
@param _mode Mode of operation. Currently the only available options are:
**ERFILTER_NM_RGBLGrad** (used by default) and **ERFILTER_NM_IHSGrad**.
In N&M algorithm, the combination of intensity (I), hue (H), saturation (S), and gradient magnitude
channels (Grad) are used in order to obtain high localization recall. This implementation also
provides an alternative combination of red (R), green (G), blue (B), lightness (L), and gradient
magnitude (Grad).
*/
CV_EXPORTS_W void computeNMChannels(InputArray _src, CV_OUT OutputArrayOfArrays _channels, int _mode = ERFILTER_NM_RGBLGrad);
//! text::erGrouping operation modes
enum erGrouping_Modes {
/** Exhaustive Search algorithm proposed in @cite Neumann11 for grouping horizontally aligned text.
The algorithm models a verification function for all the possible ER sequences. The
verification fuction for ER pairs consists in a set of threshold-based pairwise rules which
compare measurements of two regions (height ratio, centroid angle, and region distance). The
verification function for ER triplets creates a word text line estimate using Least
Median-Squares fitting for a given triplet and then verifies that the estimate is valid (based
on thresholds created during training). Verification functions for sequences larger than 3 are
approximated by verifying that the text line parameters of all (sub)sequences of length 3 are
consistent.
*/
ERGROUPING_ORIENTATION_HORIZ,
/** Text grouping method proposed in @cite Gomez13 @cite Gomez14 for grouping arbitrary oriented text. Regions
are agglomerated by Single Linkage Clustering in a weighted feature space that combines proximity
(x,y coordinates) and similarity measures (color, size, gradient magnitude, stroke width, etc.).
SLC provides a dendrogram where each node represents a text group hypothesis. Then the algorithm
finds the branches corresponding to text groups by traversing this dendrogram with a stopping rule
that combines the output of a rotation invariant text group classifier and a probabilistic measure
for hierarchical clustering validity assessment.
@note This mode is not supported due NFA code removal ( https://github.com/opencv/opencv_contrib/issues/2235 )
*/
ERGROUPING_ORIENTATION_ANY
};
/** @brief Find groups of Extremal Regions that are organized as text blocks.
@param img Original RGB or Greyscale image from wich the regions were extracted.
@param channels Vector of single channel images CV_8UC1 from wich the regions were extracted.
@param regions Vector of ER's retrieved from the ERFilter algorithm from each channel.
@param groups The output of the algorithm is stored in this parameter as set of lists of indexes to
provided regions.
@param groups_rects The output of the algorithm are stored in this parameter as list of rectangles.
@param method Grouping method (see text::erGrouping_Modes). Can be one of ERGROUPING_ORIENTATION_HORIZ,
ERGROUPING_ORIENTATION_ANY.
@param filename The XML or YAML file with the classifier model (e.g.
samples/trained_classifier_erGrouping.xml). Only to use when grouping method is
ERGROUPING_ORIENTATION_ANY.
@param minProbablity The minimum probability for accepting a group. Only to use when grouping
method is ERGROUPING_ORIENTATION_ANY.
*/
CV_EXPORTS void erGrouping(InputArray img, InputArrayOfArrays channels,
std::vector<std::vector<ERStat> > &regions,
std::vector<std::vector<Vec2i> > &groups,
std::vector<Rect> &groups_rects,
int method = ERGROUPING_ORIENTATION_HORIZ,
const std::string& filename = std::string(),
float minProbablity = 0.5);
CV_EXPORTS_W void erGrouping(InputArray image, InputArray channel,
std::vector<std::vector<Point> > regions,
CV_OUT std::vector<Rect> &groups_rects,
int method = ERGROUPING_ORIENTATION_HORIZ,
const String& filename = String(),
float minProbablity = (float)0.5);
/** @brief Converts MSER contours (vector\<Point\>) to ERStat regions.
@param image Source image CV_8UC1 from which the MSERs where extracted.
@param contours Input vector with all the contours (vector\<Point\>).
@param regions Output where the ERStat regions are stored.
It takes as input the contours provided by the OpenCV MSER feature detector and returns as output
two vectors of ERStats. This is because MSER() output contains both MSER+ and MSER- regions in a
single vector\<Point\>, the function separates them in two different vectors (this is as if the
ERStats where extracted from two different channels).
An example of MSERsToERStats in use can be found in the text detection webcam_demo:
<https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/webcam_demo.cpp>
*/
CV_EXPORTS void MSERsToERStats(InputArray image, std::vector<std::vector<Point> > &contours,
std::vector<std::vector<ERStat> > &regions);
// Utility funtion for scripting
CV_EXPORTS_W void detectRegions(InputArray image, const Ptr<ERFilter>& er_filter1, const Ptr<ERFilter>& er_filter2, CV_OUT std::vector< std::vector<Point> >& regions);
/** @brief Extracts text regions from image.
@param image Source image where text blocks needs to be extracted from. Should be CV_8UC3 (color).
@param er_filter1 Extremal Region Filter for the 1st stage classifier of N&M algorithm @cite Neumann12
@param er_filter2 Extremal Region Filter for the 2nd stage classifier of N&M algorithm @cite Neumann12
@param groups_rects Output list of rectangle blocks with text
@param method Grouping method (see text::erGrouping_Modes). Can be one of ERGROUPING_ORIENTATION_HORIZ, ERGROUPING_ORIENTATION_ANY.
@param filename The XML or YAML file with the classifier model (e.g. samples/trained_classifier_erGrouping.xml). Only to use when grouping method is ERGROUPING_ORIENTATION_ANY.
@param minProbability The minimum probability for accepting a group. Only to use when grouping method is ERGROUPING_ORIENTATION_ANY.
*/
CV_EXPORTS_W void detectRegions(InputArray image, const Ptr<ERFilter>& er_filter1, const Ptr<ERFilter>& er_filter2, CV_OUT std::vector<Rect> &groups_rects,
int method = ERGROUPING_ORIENTATION_HORIZ,
const String& filename = String(),
float minProbability = (float)0.5);
//! @}
}
}
#endif // _OPENCV_TEXT_ERFILTER_HPP_
+590
View File
@@ -0,0 +1,590 @@
/*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_TEXT_OCR_HPP__
#define __OPENCV_TEXT_OCR_HPP__
#include <opencv2/core.hpp>
#include <vector>
#include <string>
namespace cv
{
namespace text
{
//! @addtogroup text_recognize
//! @{
enum
{
OCR_LEVEL_WORD,
OCR_LEVEL_TEXTLINE
};
//! Tesseract.PageSegMode Enumeration
enum page_seg_mode
{
PSM_OSD_ONLY,
PSM_AUTO_OSD,
PSM_AUTO_ONLY,
PSM_AUTO,
PSM_SINGLE_COLUMN,
PSM_SINGLE_BLOCK_VERT_TEXT,
PSM_SINGLE_BLOCK,
PSM_SINGLE_LINE,
PSM_SINGLE_WORD,
PSM_CIRCLE_WORD,
PSM_SINGLE_CHAR
};
//! Tesseract.OcrEngineMode Enumeration
enum ocr_engine_mode
{
OEM_TESSERACT_ONLY,
OEM_CUBE_ONLY,
OEM_TESSERACT_CUBE_COMBINED,
OEM_DEFAULT
};
//base class BaseOCR declares a common API that would be used in a typical text recognition scenario
class CV_EXPORTS_W BaseOCR
{
public:
virtual ~BaseOCR() {};
virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) = 0;
virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) = 0;
};
/** @brief OCRTesseract class provides an interface with the tesseract-ocr API (v3.02.02) in C++.
Notice that it is compiled only when tesseract-ocr is correctly installed.
@note
- (C++) An example of OCRTesseract recognition combined with scene text detection can be found
at the end_to_end_recognition demo:
<https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/end_to_end_recognition.cpp>
- (C++) Another example of OCRTesseract recognition combined with scene text detection can be
found at the webcam_demo:
<https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/webcam_demo.cpp>
*/
class CV_EXPORTS_W OCRTesseract : public BaseOCR
{
public:
/** @brief Recognize text using the tesseract-ocr API.
Takes image on input and returns recognized text in the output_text parameter. Optionally
provides also the Rects for individual text elements found (e.g. words), and the list of those
text elements with their confidence values.
@param image Input image CV_8UC1 or CV_8UC3
@param output_text Output text of the tesseract-ocr.
@param component_rects If provided the method will output a list of Rects for the individual
text elements found (e.g. words or text lines).
@param component_texts If provided the method will output a list of text strings for the
recognition of individual text elements found (e.g. words or text lines).
@param component_confidences If provided the method will output a list of confidence values
for the recognition of individual text elements found (e.g. words or text lines).
@param component_level OCR_LEVEL_WORD (by default), or OCR_LEVEL_TEXTLINE.
*/
virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) CV_OVERRIDE;
virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) CV_OVERRIDE;
// aliases for scripting
CV_WRAP String run(InputArray image, int min_confidence, int component_level=0);
CV_WRAP String run(InputArray image, InputArray mask, int min_confidence, int component_level=0);
CV_WRAP virtual void setWhiteList(const String& char_whitelist) = 0;
/** @brief Creates an instance of the OCRTesseract class. Initializes Tesseract.
@param datapath the name of the parent directory of tessdata ended with "/", or NULL to use the
system's default directory.
@param language an ISO 639-3 code or NULL will default to "eng".
@param char_whitelist specifies the list of characters used for recognition. NULL defaults to ""
(All characters will be used for recognition).
@param oem tesseract-ocr offers different OCR Engine Modes (OEM), by default
tesseract::OEM_DEFAULT is used. See the tesseract-ocr API documentation for other possible
values.
@param psmode tesseract-ocr offers different Page Segmentation Modes (PSM) tesseract::PSM_AUTO
(fully automatic layout analysis) is used. See the tesseract-ocr API documentation for other
possible values.
@note The char_whitelist default is changed after OpenCV 4.7.0/3.19.0 from "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" to "".
*/
CV_WRAP static Ptr<OCRTesseract> create(const char* datapath=NULL, const char* language=NULL,
const char* char_whitelist=NULL, int oem=OEM_DEFAULT, int psmode=PSM_AUTO);
};
/* OCR HMM Decoder */
enum decoder_mode
{
OCR_DECODER_VITERBI = 0 // Other algorithms may be added
};
/* OCR classifier type*/
enum classifier_type
{
OCR_KNN_CLASSIFIER = 0,
OCR_CNN_CLASSIFIER = 1
};
/** @brief OCRHMMDecoder class provides an interface for OCR using Hidden Markov Models.
@note
- (C++) An example on using OCRHMMDecoder recognition combined with scene text detection can
be found at the webcam_demo sample:
<https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/webcam_demo.cpp>
*/
class CV_EXPORTS_W OCRHMMDecoder : public BaseOCR
{
public:
/** @brief Callback with the character classifier is made a class.
This way it hides the feature extractor and the classifier itself, so developers can write
their own OCR code.
The default character classifier and feature extractor can be loaded using the utility function
loadOCRHMMClassifierNM and KNN model provided in
<https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/OCRHMM_knn_model_data.xml.gz>.
*/
class CV_EXPORTS_W ClassifierCallback
{
public:
virtual ~ClassifierCallback() { }
/** @brief The character classifier must return a (ranked list of) class(es) id('s)
@param image Input image CV_8UC1 or CV_8UC3 with a single letter.
@param out_class The classifier returns the character class categorical label, or list of
class labels, to which the input image corresponds.
@param out_confidence The classifier returns the probability of the input image
corresponding to each classes in out_class.
*/
virtual void eval( InputArray image, std::vector<int>& out_class, std::vector<double>& out_confidence);
};
public:
/** @brief Recognize text using HMM.
Takes binary image on input and returns recognized text in the output_text parameter. Optionally
provides also the Rects for individual text elements found (e.g. words), and the list of those
text elements with their confidence values.
@param image Input binary image CV_8UC1 with a single text line (or word).
@param output_text Output text. Most likely character sequence found by the HMM decoder.
@param component_rects If provided the method will output a list of Rects for the individual
text elements found (e.g. words).
@param component_texts If provided the method will output a list of text strings for the
recognition of individual text elements found (e.g. words).
@param component_confidences If provided the method will output a list of confidence values
for the recognition of individual text elements found (e.g. words).
@param component_level Only OCR_LEVEL_WORD is supported.
*/
virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) CV_OVERRIDE;
/** @brief Recognize text using HMM.
Takes an image and a mask (where each connected component corresponds to a segmented character)
on input and returns recognized text in the output_text parameter. Optionally
provides also the Rects for individual text elements found (e.g. words), and the list of those
text elements with their confidence values.
@param image Input image CV_8UC1 or CV_8UC3 with a single text line (or word).
@param mask Input binary image CV_8UC1 same size as input image. Each connected component in mask corresponds to a segmented character in the input image.
@param output_text Output text. Most likely character sequence found by the HMM decoder.
@param component_rects If provided the method will output a list of Rects for the individual
text elements found (e.g. words).
@param component_texts If provided the method will output a list of text strings for the
recognition of individual text elements found (e.g. words).
@param component_confidences If provided the method will output a list of confidence values
for the recognition of individual text elements found (e.g. words).
@param component_level Only OCR_LEVEL_WORD is supported.
*/
virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) CV_OVERRIDE;
// aliases for scripting
CV_WRAP String run(InputArray image, int min_confidence, int component_level=0);
CV_WRAP String run(InputArray image, InputArray mask, int min_confidence, int component_level=0);
/** @brief Creates an instance of the OCRHMMDecoder class. Initializes HMMDecoder.
@param classifier The character classifier with built in feature extractor.
@param vocabulary The language vocabulary (chars when ascii english text). vocabulary.size()
must be equal to the number of classes of the classifier.
@param transition_probabilities_table Table with transition probabilities between character
pairs. cols == rows == vocabulary.size().
@param emission_probabilities_table Table with observation emission probabilities. cols ==
rows == vocabulary.size().
@param mode HMM Decoding algorithm. Only OCR_DECODER_VITERBI is available for the moment
(<http://en.wikipedia.org/wiki/Viterbi_algorithm>).
*/
CV_WRAP static Ptr<OCRHMMDecoder> create(const Ptr<OCRHMMDecoder::ClassifierCallback> classifier,// The character classifier with built in feature extractor
const String& vocabulary, // The language vocabulary (chars when ASCII English text)
// size() must be equal to the number of classes
InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
// cols == rows == vocabulary.size()
InputArray emission_probabilities_table, // Table with observation emission probabilities
// cols == rows == vocabulary.size()
int mode = OCR_DECODER_VITERBI); // HMM Decoding algorithm (only Viterbi for the moment)
/** @brief Creates an instance of the OCRHMMDecoder class. Loads and initializes HMMDecoder from the specified path
@overload
*/
CV_WRAP static Ptr<OCRHMMDecoder> create(const String& filename,
const String& vocabulary, // The language vocabulary (chars when ASCII English text)
// size() must be equal to the number of classes
InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
// cols == rows == vocabulary.size()
InputArray emission_probabilities_table, // Table with observation emission probabilities
// cols == rows == vocabulary.size()
int mode = OCR_DECODER_VITERBI, // HMM Decoding algorithm (only Viterbi for the moment)
int classifier = OCR_KNN_CLASSIFIER); // The character classifier type
protected:
Ptr<OCRHMMDecoder::ClassifierCallback> classifier;
std::string vocabulary;
Mat transition_p;
Mat emission_p;
decoder_mode mode;
};
/** @brief Allow to implicitly load the default character classifier when creating an OCRHMMDecoder object.
@param filename The XML or YAML file with the classifier model (e.g. OCRHMM_knn_model_data.xml)
The KNN default classifier is based in the scene text recognition method proposed by Lukás Neumann &
Jiri Matas in [Neumann11b]. Basically, the region (contour) in the input image is normalized to a
fixed size, while retaining the centroid and aspect ratio, in order to extract a feature vector
based on gradient orientations along the chain-code of its perimeter. Then, the region is classified
using a KNN model trained with synthetic data of rendered characters with different standard font
types.
@deprecated loadOCRHMMClassifier instead
*/
CV_EXPORTS_W Ptr<OCRHMMDecoder::ClassifierCallback> loadOCRHMMClassifierNM(const String& filename);
/** @brief Allow to implicitly load the default character classifier when creating an OCRHMMDecoder object.
@param filename The XML or YAML file with the classifier model (e.g. OCRBeamSearch_CNN_model_data.xml.gz)
The CNN default classifier is based in the scene text recognition method proposed by Adam Coates &
Andrew NG in [Coates11a]. The character classifier consists in a Single Layer Convolutional Neural Network and
a linear classifier. It is applied to the input image in a sliding window fashion, providing a set of recognitions
at each window location.
@deprecated use loadOCRHMMClassifier instead
*/
CV_EXPORTS_W Ptr<OCRHMMDecoder::ClassifierCallback> loadOCRHMMClassifierCNN(const String& filename);
/** @brief Allow to implicitly load the default character classifier when creating an OCRHMMDecoder object.
@param filename The XML or YAML file with the classifier model (e.g. OCRBeamSearch_CNN_model_data.xml.gz)
@param classifier Can be one of classifier_type enum values.
*/
CV_EXPORTS_W Ptr<OCRHMMDecoder::ClassifierCallback> loadOCRHMMClassifier(const String& filename, int classifier);
/** @brief Utility function to create a tailored language model transitions table from a given list of words (lexicon).
*
* @param vocabulary The language vocabulary (chars when ASCII English text).
*
* @param lexicon The list of words that are expected to be found in a particular image.
*
* @param transition_probabilities_table Output table with transition probabilities between character pairs. cols == rows == vocabulary.size().
*
* The function calculate frequency statistics of character pairs from the given lexicon and fills the output transition_probabilities_table with them. The transition_probabilities_table can be used as input in the OCRHMMDecoder::create() and OCRBeamSearchDecoder::create() methods.
* @note
* - (C++) An alternative would be to load the default generic language transition table provided in the text module samples folder (created from ispell 42869 english words list) :
* <https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/OCRHMM_transitions_table.xml>
**/
CV_EXPORTS void createOCRHMMTransitionsTable(std::string& vocabulary, std::vector<std::string>& lexicon, OutputArray transition_probabilities_table);
CV_EXPORTS_W Mat createOCRHMMTransitionsTable(const String& vocabulary, std::vector<cv::String>& lexicon);
/* OCR BeamSearch Decoder */
/** @brief OCRBeamSearchDecoder class provides an interface for OCR using Beam Search algorithm.
@note
- (C++) An example on using OCRBeamSearchDecoder recognition combined with scene text detection can
be found at the demo sample:
<https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/word_recognition.cpp>
*/
class CV_EXPORTS_W OCRBeamSearchDecoder : public BaseOCR
{
public:
/** @brief Callback with the character classifier is made a class.
This way it hides the feature extractor and the classifier itself, so developers can write
their own OCR code.
The default character classifier and feature extractor can be loaded using the utility function
loadOCRBeamSearchClassifierCNN with all its parameters provided in
<https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/OCRBeamSearch_CNN_model_data.xml.gz>.
*/
class CV_EXPORTS_W ClassifierCallback
{
public:
virtual ~ClassifierCallback() { }
/** @brief The character classifier must return a (ranked list of) class(es) id('s)
@param image Input image CV_8UC1 or CV_8UC3 with a single letter.
@param recognition_probabilities For each of the N characters found the classifier returns a list with
class probabilities for each class.
@param oversegmentation The classifier returns a list of N+1 character locations' x-coordinates,
including 0 as start-sequence location.
*/
virtual void eval( InputArray image, std::vector< std::vector<double> >& recognition_probabilities, std::vector<int>& oversegmentation );
int getWindowSize() {return 0;}
int getStepSize() {return 0;}
};
public:
/** @brief Recognize text using Beam Search.
Takes image on input and returns recognized text in the output_text parameter. Optionally
provides also the Rects for individual text elements found (e.g. words), and the list of those
text elements with their confidence values.
@param image Input binary image CV_8UC1 with a single text line (or word).
@param output_text Output text. Most likely character sequence found by the HMM decoder.
@param component_rects If provided the method will output a list of Rects for the individual
text elements found (e.g. words).
@param component_texts If provided the method will output a list of text strings for the
recognition of individual text elements found (e.g. words).
@param component_confidences If provided the method will output a list of confidence values
for the recognition of individual text elements found (e.g. words).
@param component_level Only OCR_LEVEL_WORD is supported.
*/
virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) CV_OVERRIDE;
virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) CV_OVERRIDE;
// aliases for scripting
CV_WRAP String run(InputArray image, int min_confidence, int component_level=0);
CV_WRAP String run(InputArray image, InputArray mask, int min_confidence, int component_level=0);
/** @brief Creates an instance of the OCRBeamSearchDecoder class. Initializes HMMDecoder.
@param classifier The character classifier with built in feature extractor.
@param vocabulary The language vocabulary (chars when ASCII English text). vocabulary.size()
must be equal to the number of classes of the classifier.
@param transition_probabilities_table Table with transition probabilities between character
pairs. cols == rows == vocabulary.size().
@param emission_probabilities_table Table with observation emission probabilities. cols ==
rows == vocabulary.size().
@param mode HMM Decoding algorithm. Only OCR_DECODER_VITERBI is available for the moment
(<http://en.wikipedia.org/wiki/Viterbi_algorithm>).
@param beam_size Size of the beam in Beam Search algorithm.
*/
static CV_WRAP
Ptr<OCRBeamSearchDecoder> create(const Ptr<OCRBeamSearchDecoder::ClassifierCallback> classifier,// The character classifier with built in feature extractor
const std::string& vocabulary, // The language vocabulary (chars when ASCII English text)
// size() must be equal to the number of classes
InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
// cols == rows == vocabulary.size()
InputArray emission_probabilities_table, // Table with observation emission probabilities
// cols == rows == vocabulary.size()
text::decoder_mode mode = OCR_DECODER_VITERBI, // HMM Decoding algorithm (only Viterbi for the moment)
int beam_size = 500 // Size of the beam in Beam Search algorithm
);
/** @brief Creates an instance of the OCRBeamSearchDecoder class. Initializes HMMDecoder from the specified path.
@overload
*/
static //CV_WRAP FIXIT bug in handling of Java overloads
Ptr<OCRBeamSearchDecoder> create(const String& filename, // The character classifier file
const String& vocabulary, // The language vocabulary (chars when ASCII English text)
// size() must be equal to the number of classes
InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
// cols == rows == vocabulary.size()
InputArray emission_probabilities_table, // Table with observation emission probabilities
// cols == rows == vocabulary.size()
text::decoder_mode mode = OCR_DECODER_VITERBI, // HMM Decoding algorithm (only Viterbi for the moment)
int beam_size = 500 // Size of the beam in Beam Search algorithm
);
protected:
Ptr<OCRBeamSearchDecoder::ClassifierCallback> classifier;
std::string vocabulary;
Mat transition_p;
Mat emission_p;
decoder_mode mode;
int beam_size;
};
/** @brief Allow to implicitly load the default character classifier when creating an OCRBeamSearchDecoder object.
@param filename The XML or YAML file with the classifier model (e.g. OCRBeamSearch_CNN_model_data.xml.gz)
The CNN default classifier is based in the scene text recognition method proposed by Adam Coates &
Andrew NG in [Coates11a]. The character classifier consists in a Single Layer Convolutional Neural Network and
a linear classifier. It is applied to the input image in a sliding window fashion, providing a set of recognitions
at each window location.
*/
CV_EXPORTS_W Ptr<OCRBeamSearchDecoder::ClassifierCallback> loadOCRBeamSearchClassifierCNN(const String& filename);
/** @brief OCRHolisticWordRecognizer class provides the functionallity of segmented wordspotting.
* Given a predefined vocabulary , a DictNet is employed to select the most probable
* word given an input image.
*
* DictNet is described in detail in:
* Max Jaderberg et al.: Reading Text in the Wild with Convolutional Neural Networks, IJCV 2015
* http://arxiv.org/abs/1412.1842
*/
class CV_EXPORTS OCRHolisticWordRecognizer : public BaseOCR
{
public:
virtual void run(Mat& image,
std::string& output_text,
std::vector<Rect>* component_rects = NULL,
std::vector<std::string>* component_texts = NULL,
std::vector<float>* component_confidences = NULL,
int component_level = OCR_LEVEL_WORD) CV_OVERRIDE = 0;
/** @brief Recognize text using a segmentation based word-spotting/classifier cnn.
Takes image on input and returns recognized text in the output_text parameter. Optionally
provides also the Rects for individual text elements found (e.g. words), and the list of those
text elements with their confidence values.
@param image Input image CV_8UC1 or CV_8UC3
@param mask is totally ignored and is only available for compatibillity reasons
@param output_text Output text of the the word spoting, always one that exists in the dictionary.
@param component_rects Not applicable for word spotting can be be NULL if not, a single elemnt will
be put in the vector.
@param component_texts Not applicable for word spotting can be be NULL if not, a single elemnt will
be put in the vector.
@param component_confidences Not applicable for word spotting can be be NULL if not, a single elemnt will
be put in the vector.
@param component_level must be OCR_LEVEL_WORD.
*/
virtual void run(Mat& image,
Mat& mask,
std::string& output_text,
std::vector<Rect>* component_rects = NULL,
std::vector<std::string>* component_texts = NULL,
std::vector<float>* component_confidences = NULL,
int component_level = OCR_LEVEL_WORD) CV_OVERRIDE = 0;
/** @brief Creates an instance of the OCRHolisticWordRecognizer class.
*/
static Ptr<OCRHolisticWordRecognizer> create(const std::string &archFilename,
const std::string &weightsFilename,
const std::string &wordsFilename);
};
//! @}
}} // cv::text::
#endif // _OPENCV_TEXT_OCR_HPP_
@@ -0,0 +1,24 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_TEXT_SWTTEXTDETECTOR_HPP__
#define __OPENCV_TEXT_SWTTEXTDETECTOR_HPP__
#include <opencv2/core.hpp>
#include <vector>
namespace cv {
namespace text {
/** @brief Applies the Stroke Width Transform operator followed by filtering of connected components of similar Stroke Widths to return letter candidates. It also chain them by proximity and size, saving the result in chainBBs.
@param input the input image with 3 channels.
@param result a vector of resulting bounding boxes where probability of finding text is high
@param dark_on_light a boolean value signifying whether the text is darker or lighter than the background, it is observed to reverse the gradient obtained from Scharr operator, and significantly affect the result.
@param draw an optional Mat of type CV_8UC3 which visualises the detected letters using bounding boxes.
@param chainBBs an optional parameter which chains the letter candidates according to heuristics in the paper and returns all possible regions where text is likely to occur.
*/
CV_EXPORTS_W void detectTextSWT (InputArray input, CV_OUT std::vector<cv::Rect>& result, bool dark_on_light, OutputArray& draw=noArray(), OutputArray & chainBBs =noArray());
}
}
#endif
@@ -0,0 +1,73 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_TEXT_TEXTDETECTOR_HPP__
#define __OPENCV_TEXT_TEXTDETECTOR_HPP__
#include "ocr.hpp"
namespace cv
{
namespace text
{
//! @addtogroup text_detect
//! @{
/** @brief An abstract class providing interface for text detection algorithms
*/
class CV_EXPORTS_W TextDetector
{
public:
/**
@brief Method that provides a quick and simple interface to detect text inside an image
@param inputImage an image to process
@param Bbox a vector of Rect that will store the detected word bounding box
@param confidence a vector of float that will be updated with the confidence the classifier has for the selected bounding box
*/
CV_WRAP virtual void detect(InputArray inputImage, CV_OUT std::vector<Rect>& Bbox, CV_OUT std::vector<float>& confidence) = 0;
virtual ~TextDetector() {}
};
/** @brief TextDetectorCNN class provides the functionallity of text bounding box detection.
This class is representing to find bounding boxes of text words given an input image.
This class uses OpenCV dnn module to load pre-trained model described in @cite LiaoSBWL17.
The original repository with the modified SSD Caffe version: https://github.com/MhLiao/TextBoxes.
Model can be downloaded from [DropBox](https://www.dropbox.com/s/g8pjzv2de9gty8g/TextBoxes_icdar13.caffemodel?dl=0).
Modified .prototxt file with the model description can be found in `opencv_contrib/modules/text/samples/textbox.prototxt`.
*/
class CV_EXPORTS_W TextDetectorCNN : public TextDetector
{
public:
/**
@overload
@param inputImage an image expected to be a CV_U8C3 of any size
@param Bbox a vector of Rect that will store the detected word bounding box
@param confidence a vector of float that will be updated with the confidence the classifier has for the selected bounding box
*/
CV_WRAP virtual void detect(InputArray inputImage, CV_OUT std::vector<Rect>& Bbox, CV_OUT std::vector<float>& confidence) CV_OVERRIDE = 0;
/** @brief Creates an instance of the TextDetectorCNN class using the provided parameters.
@param modelArchFilename the relative or absolute path to the prototxt file describing the classifiers architecture.
@param modelWeightsFilename the relative or absolute path to the file containing the pretrained weights of the model in caffe-binary form.
@param detectionSizes a list of sizes for multiscale detection. The values`[(300,300),(700,500),(700,300),(700,700),(1600,1600)]` are
recommended in @cite LiaoSBWL17 to achieve the best quality.
*/
static Ptr<TextDetectorCNN> create(const String& modelArchFilename, const String& modelWeightsFilename,
std::vector<Size> detectionSizes);
/**
@overload
*/
CV_WRAP static Ptr<TextDetectorCNN> create(const String& modelArchFilename, const String& modelWeightsFilename);
};
//! @}
}//namespace text
}//namespace cv
#endif // _OPENCV_TEXT_OCR_HPP_
+28
View File
@@ -0,0 +1,28 @@
{
"AdditionalImports" : {
"*" : [ "\"text.hpp\"" ]
},
"func_arg_fix" : {
"Text" : {
"detectRegions" : { "method" : {"ctype" : "erGrouping_Modes"} },
"erGrouping" : { "method" : {"ctype" : "erGrouping_Modes"} },
"loadOCRHMMClassifier" : { "classifier" : {"ctype" : "classifier_type"} },
"(ERFilter*)createERFilterNM1:(NSString*)filename thresholdDelta:(int)thresholdDelta minArea:(float)minArea maxArea:(float)maxArea minProbability:(float)minProbability nonMaxSuppression:(BOOL)nonMaxSuppression minProbabilityDiff:(float)minProbabilityDiff" : { "createERFilterNM1" : {"name" : "createERFilterNM1FromFile"} },
"(ERFilter*)createERFilterNM2:(NSString*)filename minProbability:(float)minProbability" : { "createERFilterNM2" : {"name" : "createERFilterNM2FromFile"} }
},
"OCRBeamSearchDecoder" : {
"(OCRBeamSearchDecoder*)create:(NSString*)filename vocabulary:(NSString*)vocabulary transition_probabilities_table:(Mat*)transition_probabilities_table emission_probabilities_table:(Mat*)emission_probabilities_table mode:(decoder_mode)mode beam_size:(int)beam_size" : { "create" : {"name" : "createFromFile"} },
"create" : { "mode" : {"ctype" : "decoder_mode",
"defval" : ""
} }
},
"OCRHMMDecoder" : {
"(OCRHMMDecoder*)create:(NSString*)filename vocabulary:(NSString*)vocabulary transition_probabilities_table:(Mat*)transition_probabilities_table emission_probabilities_table:(Mat*)emission_probabilities_table mode:(decoder_mode)mode classifier:(int)classifier" : { "create" : {"name" : "createFromFile"} },
"create" : { "mode" : {"ctype" : "decoder_mode"} }
},
"OCRTesseract" : {
"create" : { "oem" : {"ctype" : "ocr_engine_mode"},
"psmode" : {"ctype" : "page_seg_mode"} }
}
}
}
Binary file not shown.
@@ -0,0 +1,71 @@
<?xml version="1.0"?>
<opencv_storage>
<transition_probabilities type_id="opencv-matrix">
<rows>62</rows>
<cols>62</cols>
<dt>d</dt>
<data>
8.50520944078e-05 0.0544758664682 0.0617478205401 0.0385285987667 0.00146714862853 0.00848394641718 0.0317882202849 0.000893046991282 0.0399106953009 0.000786731873272 0.013140548586 0.118371252392 0.0354667233681 0.116691473528 0.000233893259622 0.034509887306 0.000403997448437 0.113565809058 0.0640442270891 0.196980650649 0.0180948330853 0.0160748458431 0.00967467573889 0.00372102913034 0.0153519030406 0.00550712311291 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.100866163733 0.0314799292167 0.00242153301667 0.00502933780386 0.126292260408 0.00204898947564 0.00130390239359 0.00130390239359 0.112973828816 0.00614696842693 0.0 0.289000651951 0.00484306603334 0.00186271770513 0.0848467914688 0.00186271770513 0.0 0.0757194747136 0.053366862252 0.0113625780013 0.0739498928937 0.0027940765577 0.00111763062308 0.0 0.00940672441092 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.123990600299 0.0 0.0241828669088 0.00046998504593 0.136594744713 0.0 0.0 0.107583849605 0.0869472334971 0.0 0.0763939329203 0.0394787438581 0.000299081392865 0.00128177739799 0.152574236274 0.000341807306131 0.00290536210211 0.0535355693228 0.0138431958983 0.115274513993 0.053407391583 0.0 0.0 0.0 0.0107242042299 0.000170903653066 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0556214577567 0.00263608804534 0.00263608804534 0.0282061420851 0.318834849084 0.00355871886121 0.0217477263741 0.00329511005668 0.226110452089 0.00724924212469 0.000527217609068 0.0645841571108 0.00948991696323 0.0267562936602 0.0452089099776 0.00237247924081 0.000131804402267 0.0322261763543 0.067615658363 0.00105443521814 0.0506787926717 0.011862396204 0.00546988269408 0.0 0.0119942006063 0.000131804402267 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0467747490338 0.00432414505657 0.0376672576757 0.147403599628 0.0215824585124 0.0132020357922 0.0106891845351 0.00250009566693 0.00761508731201 0.000982180440578 0.00145413727566 0.0484584869319 0.0227814839854 0.112134392898 0.00517876959578 0.0174496473079 0.00329094225544 0.22472798704 0.197022845262 0.0348482722553 0.0022194726839 0.0134061252344 0.00769162085284 0.0118626988278 0.00371187672998 0.00102044721099 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0729166666667 0.0 0.000508130081301 0.0 0.146214430894 0.111788617886 0.000762195121951 0.0 0.21468495935 0.0 0.0 0.0782520325203 0.0 0.00279471544715 0.111280487805 0.0 0.0 0.0581808943089 0.0162601626016 0.0429369918699 0.114456300813 0.0 0.000762195121951 0.0 0.0282012195122 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0787207872079 0.0 0.0 0.000351432085749 0.212440695836 0.000527148128624 0.0419961342471 0.0789843612722 0.107977508347 0.0 0.0 0.125812686698 0.00773150588649 0.0492883500264 0.0347917764892 0.000615006150062 0.0 0.110964681075 0.081356527851 0.00404146898612 0.0533298190125 0.0 0.000527148128624 0.0 0.0103672465296 0.000175716042875 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.154653160894 0.00144704711947 0.000542642669802 0.00271321334901 0.298996111061 0.00614995025776 0.0 0.00271321334901 0.184136745953 0.0 0.0 0.016460160984 0.0103102107262 0.0121190196256 0.133309215881 0.00090440444967 0.000361761779868 0.0371710228814 0.0124807814054 0.0618612643574 0.0365379397667 0.0 0.00614995025776 0.0 0.0205299810075 0.000452202224835 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0319377484564 0.0157151315233 0.0745496066988 0.0267783134568 0.0643998985029 0.0226507654572 0.0288928359976 0.000439820688489 0.000473653049142 0.000405988327836 0.00314640954073 0.0478558741436 0.0279116975387 0.299247229975 0.0865431785503 0.0175928275395 0.00182694747526 0.0263554089487 0.0856297048127 0.0803856889114 0.00159012095069 0.0491584200288 6.76647213059e-05 0.00296033155713 0.0 0.00348473314726 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.125310173697 0.0 0.0 0.0 0.263027295285 0.0 0.0 0.0 0.014888337469 0.0 0.00248138957816 0.0 0.0 0.0 0.233250620347 0.0 0.0 0.00124069478908 0.0 0.0 0.359801488834 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0458695168114 0.00668002672011 0.000890670229348 0.00267201068804 0.422845691383 0.00400801603206 0.000890670229348 0.0060120240481 0.197951458473 0.000890670229348 0.00222667557337 0.0721442885772 0.00668002672011 0.0334001336005 0.0106880427522 0.00534402137609 0.0 0.00489868626141 0.135381874861 0.00668002672011 0.00935203740815 0.0 0.00801603206413 0.0 0.0164773992429 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.10850279394 0.00172821015035 0.00357163431073 0.0179157785587 0.21712080189 0.00385966933579 0.0033988132957 0.000460856040094 0.153148222824 0.0 0.00558787948615 0.111066305663 0.00483898842099 0.0107149029322 0.0684659254565 0.00397488334581 0.0 0.00138256812028 0.0326055648367 0.0247710121551 0.0354859150873 0.00639437755631 0.000806498070165 0.0 0.18414079152 5.76070050118e-05 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.142847308275 0.0638854467851 0.000619578686493 0.000275368305108 0.226215062646 0.00385515627151 0.0 0.000481894533939 0.159920143192 0.0 0.0 0.00530083987333 0.0549359768691 0.00901831199229 0.0927991188214 0.153242461793 0.0 0.000963789067878 0.037518931571 0.000413052457662 0.0410298774611 0.00068842076277 0.000275368305108 0.0 0.00571389233099 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0555165707641 0.00324799005554 0.0619123042685 0.0696914409447 0.155843374702 0.016380295527 0.238707219761 0.00453114662069 0.0559175571907 0.00272670770094 0.0130721575075 0.0076989393909 0.00364897648215 0.0156384706378 0.0215730697516 0.00425045612206 0.00272670770094 0.00411011087275 0.0990837460152 0.134691340698 0.00978406880927 0.011588507729 0.00312769412755 0.000280690498627 0.0034885819115 0.000761874210558 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0216214746118 0.0155565829911 0.0320378579782 0.0258913758873 0.00579292338655 0.00976365960456 0.0219206396693 0.00244771410699 0.0185754303897 0.000815904702331 0.0101444151323 0.0608664907939 0.0677744839403 0.23911447143 0.0419103048764 0.0337512578531 0.000435149174576 0.141885827735 0.0423726508744 0.0431341619299 0.0949985041747 0.0226005602546 0.0346215562022 0.00329081563273 0.00709837091028 0.00157741575784 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0966494041305 0.00112310476072 0.000623947089287 0.000686341798216 0.202595619891 0.00124789417857 0.000748736507144 0.0483558994197 0.0851687776877 0.0 0.000873525925002 0.101516191427 0.00174705185 0.00118549946964 0.104760716291 0.0722530729394 0.0 0.145878829475 0.0374992200661 0.0552817121108 0.0366880888501 0.0 0.000998315342859 0.0 0.00411805078929 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.000904977375566 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.999095022624 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.143023601179 0.00941305490187 0.0207224291165 0.0281934702643 0.211610957527 0.00680847174941 0.0125659713496 0.0013479860175 0.139322351436 9.13888825424e-05 0.0106924992575 0.0147136100893 0.0268683314675 0.021065137426 0.0920742991615 0.0123831935845 0.00036555553017 0.029153053531 0.110146450684 0.050538052046 0.0292215951929 0.00959583266696 0.00228472206356 0.0 0.0177065959926 9.13888825424e-05 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0315670455934 0.00171128223934 0.0422320009779 0.001466813348 0.125473658477 0.00409485392984 0.002933626696 0.0628896222956 0.108330277472 0.000733406674001 0.00928981787068 0.0301002322454 0.0154626573768 0.0203825938149 0.0375565334311 0.0439738418286 0.00443099865542 0.00161960640508 0.200647842562 0.199425498105 0.0422931182007 0.0 0.00589781200342 6.11172228334e-05 0.00742574257426 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0828099447776 0.000805820870761 0.011542198943 0.000237006138459 0.250088877302 0.00355509207688 0.000805820870761 0.0456473822672 0.276775768492 0.0 4.74012276918e-05 0.028914748892 0.00312848102766 0.006920579243 0.055056525964 0.00109022823691 0.0 0.0702723200531 0.0615741947716 0.0335126679781 0.0341762851658 0.000189604910767 0.0037683976015 0.0 0.0284170360012 0.000663617187685 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0407358156028 0.0375886524823 0.0419769503546 0.0322695035461 0.043085106383 0.0132092198582 0.0246010638298 8.86524822695e-05 0.0436170212766 0.0 0.000886524822695 0.105230496454 0.0573581560284 0.148670212766 0.00833333333333 0.0339095744681 0.000177304964539 0.131826241135 0.144060283688 0.0859485815603 0.000487588652482 0.000531914893617 0.0 0.00168439716312 0.000975177304965 0.00274822695035 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.131552670994 0.0 0.000124812780829 0.0 0.623689465801 0.0 0.0 0.0 0.18072890664 0.0 0.0 0.0 0.000124812780829 0.0 0.0567898152771 0.0 0.0 0.0 0.0 0.0 0.00399400898652 0.0 0.0 0.0 0.00299550673989 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.196585140748 0.00646054453161 0.00230733733272 0.0133825565298 0.193124134749 0.00599907706507 0.000922934933087 0.0539916935856 0.174665436087 0.0 0.0073834794647 0.0535302261191 0.00323027226581 0.0779880018459 0.095062298108 0.000922934933087 0.0 0.03737886479 0.0673742501154 0.00553760959852 0.00138440239963 0.0 0.0 0.0 0.00276880479926 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.092132505176 0.0 0.123188405797 0.0 0.15734989648 0.0 0.0 0.0310559006211 0.166149068323 0.0 0.0 0.00621118012422 0.0 0.00103519668737 0.0207039337474 0.230848861284 0.00310559006211 0.0 0.0 0.142857142857 0.0175983436853 0.00207039337474 0.00103519668737 0.000517598343685 0.00414078674948 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0538403614458 0.0143072289157 0.042921686747 0.0143072289157 0.140436746988 0.00753012048193 0.00978915662651 0.00451807228916 0.181852409639 0.0 0.0 0.0504518072289 0.0730421686747 0.0557228915663 0.0368975903614 0.0798192771084 0.0 0.0286144578313 0.157379518072 0.0316265060241 0.00263554216867 0.0 0.0135542168675 0.000753012048193 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.104328523862 0.0 0.0 0.0 0.369589345172 0.0 0.0 0.0 0.19089900111 0.0 0.00221975582686 0.0732519422863 0.00443951165372 0.0 0.0532741398446 0.0 0.0 0.0 0.0 0.0 0.00887902330744 0.00887902330744 0.00443951165372 0.0 0.0332963374029 0.146503884573 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0094510076442 0.0116747741487 0.012022237665 0.000799166087561 0.00371785962474 0.00357887421821 6.94927032662e-05 0.00267546907575 3.47463516331e-05 6.94927032662e-05 0.0102501737318 0.00681028492008 0.0127171646977 3.47463516331e-05 0.0097637248089 0.00045170257123 0.0105281445448 0.0102849200834 0.00535093815149 0.00615010423905 0.00277970813065 0.00156358582349 0.000868658790827 0.000138985406532 0.000173731758165 6.94927032662e-05 0.0492355802641 0.0562890896456 0.0374913134121 0.00159833217512 0.00879082696317 0.0277623349548 0.000764419735928 0.0339471855455 0.000660180681028 0.0107713690063 0.101841556637 0.032383599722 0.10170257123 0.000208478109798 0.0330785267547 0.000555941626129 0.0980542043085 0.0574704656011 0.16362056984 0.0178596247394 0.0145239749826 0.00868658790827 0.00347463516331 0.0126129256428 0.00458651841557 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0593910159783 0.0 0.0 0.0 0.0530599939705 0.0 0.0 0.0 0.0276354135263 0.0 0.0 0.0354738217265 0.0 0.0 0.0400964727163 0.0 0.0 0.0477338960908 0.000100492412823 0.0 0.0410009044317 0.0 0.0 0.0 0.00251231032057 0.0 0.0841121495327 0.0169832177671 0.0013064013667 0.00271329514622 0.0946638528791 0.00110541654105 0.00070344688976 0.00070344688976 0.0747663551402 0.00331624962315 0.0 0.173650889358 0.00261280273339 0.00100492412823 0.065822530399 0.00100492412823 0.0 0.0647171138579 0.0288413224802 0.00613003718219 0.0603959401065 0.00150738619234 0.000602954476937 0.0 0.00633102200784 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0294084251164 0.0 0.0 5.67730214602e-05 0.00573407516748 0.0 0.0 0.0255478596571 0.00482570682412 0.0 0.0 0.0181673668673 5.67730214602e-05 0.0 0.106335869195 0.00011354604292 0.0 0.0204950607471 0.0 0.0 0.0112410582491 0.0 0.0 0.0 0.00164641762235 0.00011354604292 0.0970818666969 0.0 0.0160667650732 0.000340638128761 0.0936187123879 0.0 0.0 0.0842511638469 0.0601794027478 0.0 0.0507550811854 0.0353128193482 0.000227092085841 0.000851595321903 0.154536164415 0.000283865107301 0.00193028272965 0.0458158283184 0.00919722947655 0.0765868059498 0.0411036675372 0.0 0.0 0.0 0.00794822300443 0.000170319064381 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0157669237361 0.0 0.0 0.0 0.0894601542416 0.0 0.0 0.0 0.0814910025707 0.0 0.0 0.0 0.0 0.0 0.0176520994002 0.0 0.0 0.0177377892031 0.0 0.0 0.0083119108826 0.0 0.0012853470437 0.0 0.00154241645244 0.0 0.0440445586975 0.00171379605827 0.00171379605827 0.0183376178235 0.252013710368 0.00231362467866 0.0141388174807 0.00214224507284 0.187746358183 0.00471293916024 0.000342759211654 0.0419880034276 0.00616966580977 0.0173950299914 0.0382176520994 0.00154241645244 8.56898029135e-05 0.0298200514139 0.0439588688946 0.000685518423308 0.0371036846615 0.00771208226221 0.00419880034276 0.0 0.00856898029135 8.56898029135e-05 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0019322058157 0.000119271963932 0.000596359819661 0.00100188449703 9.54175711457e-05 0.000548651034088 0.000190835142291 0.0 0.000310107106224 0.000262398320651 9.54175711457e-05 0.00388826602419 0.00329190620453 0.00970873786408 0.0 0.000763340569166 0.000811049354739 0.00114501085375 0.00166980749505 0.000429379070156 0.000190835142291 0.00236158488586 7.15631783593e-05 0.0133107511748 0.000500942248515 0.0 0.0447031320818 0.00410295555927 0.035519190859 0.138331623769 0.0202285250829 0.012618973784 0.0100904081487 0.00233773049307 0.00727558979986 0.0010495932826 0.0014074091744 0.0472555521099 0.0229479258605 0.109706352425 0.00484244173565 0.0166980749505 0.00348274134682 0.210705851483 0.185062379237 0.0327997900813 0.00217074974357 0.0137162758522 0.00722788101429 0.0177476682331 0.00372128527468 0.000954175711457 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0470732080086 0.0 0.0 0.0 0.0255234601865 0.0 0.0 0.0 0.0354577410974 0.0 0.0 0.0409598043711 0.0 0.0 0.0602170258291 0.0 0.0 0.038820113098 0.0 0.0 0.0175760354577 0.0 0.0 0.0 0.0 0.0 0.0674002751032 0.0 0.000305670181874 0.0 0.100718324927 0.0672474400122 0.000458505272811 0.0 0.14687452239 0.0 0.0 0.0675531101941 0.0 0.00168118600031 0.0970502827449 0.0 0.0 0.0544092923735 0.00978144581996 0.0258291303683 0.0776402261959 0.0 0.000458505272811 0.0 0.016964695094 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0279106858054 0.0 0.0 0.0 0.0154173312068 0.0 0.0 0.00146198830409 0.0104997341839 0.0 0.0 0.0204678362573 0.0 0.000930356193514 0.0151515151515 0.000132908027645 0.0 0.0530303030303 0.0 0.0 0.0152844231792 0.0 0.0 0.0 0.00212652844232 0.0 0.0734981392876 0.0 0.0 0.00026581605529 0.168394471026 0.000398724082935 0.0317650186071 0.0604731525784 0.0869218500797 0.0 0.0 0.105396065922 0.00584795321637 0.0377458798511 0.0338915470494 0.000531632110579 0.0 0.110446570973 0.0615364167996 0.00305688463583 0.0479797979798 0.0 0.000398724082935 0.0 0.00890483785221 0.000132908027645 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0484241850399 0.0 0.0 0.0 0.0346273501961 0.0 0.0 0.0 0.0175842012715 0.0 0.0 0.0 0.0 0.0 0.0416610307047 0.0 0.0 0.000135263086704 0.0 0.0 0.020559989179 0.0 0.0 0.0 0.00500473420803 0.000135263086704 0.139862031652 0.00108210469363 0.000405789260111 0.00202894630055 0.240903557419 0.00459894494792 0.0 0.00202894630055 0.1464899229 0.0 0.0 0.01230894089 0.00770999594211 0.00906262680914 0.120519410253 0.000676315433518 0.000270526173407 0.0278641958609 0.00933315298255 0.0462599756526 0.0376031381036 0.0 0.00459894494792 0.0 0.0178547274449 0.000405789260111 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 3.0466441215e-05 0.000396063735795 0.00143192273711 3.0466441215e-05 3.0466441215e-05 0.00036559729458 0.0 6.093288243e-05 0.0 0.0 0.00137098985468 0.010480455778 0.0476495140603 0.00012186576486 0.0 0.0 0.00225451664991 0.00109679188374 0.00079212747159 0.0 0.00018279864729 0.0 3.0466441215e-05 0.0 0.0 0.028760320507 0.014166895165 0.0673308350852 0.0248301495902 0.0580081040734 0.0204125156141 0.0262011394449 0.000396063735795 0.000456996618225 0.00036559729458 0.002833379033 0.043780276026 0.0303750418914 0.293300429577 0.0779940895104 0.0158425494318 0.00164518782561 0.0248606160314 0.077658958657 0.0727843280626 0.00143192273711 0.044359138409 6.093288243e-05 0.00268104682692 0.0 0.00313804344515 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0665859564165 0.0 0.0 0.0 0.0556900726392 0.0 0.0 0.0 0.00968523002421 0.0 0.0 0.0 0.0 0.0 0.089588377724 0.0 0.0 0.00121065375303 0.0 0.0 0.118644067797 0.0 0.0 0.0 0.0 0.0 0.0944309927361 0.0 0.0 0.0 0.15617433414 0.0 0.0 0.0 0.0121065375303 0.0 0.00121065375303 0.0 0.0 0.0 0.158595641646 0.0 0.0 0.00121065375303 0.0 0.0 0.234866828087 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.017523364486 0.0 0.0 0.000389408099688 0.0323208722741 0.0 0.0 0.00545171339564 0.0 0.0272585669782 0.0 0.0 0.0 0.0 0.0 0.0 0.000778816199377 0.0 0.0 0.0 0.0 0.0 0.0401090342679 0.00584112149533 0.000778816199377 0.00233644859813 0.378504672897 0.0035046728972 0.000778816199377 0.00545171339564 0.189252336449 0.000778816199377 0.00194704049844 0.0658099688474 0.00584112149533 0.0428348909657 0.00934579439252 0.00467289719626 0.0 0.00428348909657 0.118380062305 0.00584112149533 0.00856697819315 0.0 0.00700934579439 0.0 0.0144080996885 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0148755154236 0.0 0.0 0.0 0.0127355289942 0.0 0.0 0.0 0.0153974633332 0.0 0.0 0.0 0.0 0.0 0.0140403987682 0.0 0.0 0.0 0.0 0.0 0.00480192076831 0.0 0.0 0.0 0.000782921864398 0.0 0.105746646485 0.0015658437288 0.00323607703951 0.0162325799885 0.203089931625 0.00349705099431 0.00307949266663 0.000417558327679 0.146458583433 0.0 0.00506289472311 0.100631556971 0.00438436244063 0.00970823111853 0.0690537084399 0.00360144057623 0.0 0.00125267498304 0.0295422516833 0.0224437601127 0.0345529516154 0.00579362179654 0.000730727073438 0.0 0.167232110235 5.21947909599e-05 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0529742527375 0.0 0.00029594554602 0.0 0.0317648219394 0.0 0.0 9.86485153398e-05 0.0415310249581 0.0 0.0 9.86485153398e-05 0.0 0.00029594554602 0.0378810298905 0.0 0.0 0.00019729703068 9.86485153398e-05 0.0 0.0217026733748 0.0 0.0 0.0 0.00207161882214 0.0 0.128834961034 0.0457729111177 0.000591891092039 0.00019729703068 0.177961921673 0.00276215842952 0.0 0.000394594061359 0.135345763046 0.0 0.0 0.00384729209825 0.0393607576206 0.00660945052777 0.0854296142843 0.109795797573 0.0 0.000789188122719 0.0269310446878 0.00029594554602 0.0402485942587 0.000493242576699 0.00019729703068 0.0 0.00512972279767 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.00405734379226 0.0 0.0 0.0 0.00676223965377 3.86413694501e-05 0.0 0.0 0.00274353723096 0.0 0.0 0.0 0.0 0.0 0.00811468758453 0.0 0.0 3.86413694501e-05 0.0 0.0 0.00239576490591 0.0 0.0 0.0 7.72827389003e-05 0.0 0.0555276478998 0.00312995092546 0.059662274431 0.0671587001043 0.153560802195 0.0158043201051 0.230032072337 0.00436647474787 0.0552571583137 0.00262761312261 0.0125970864407 0.00741914293443 0.00351636461996 0.0150701340856 0.0248464005564 0.00409598516171 0.00262761312261 0.00398006105336 0.0954828239113 0.129796359983 0.0106263765988 0.0111673557711 0.00301402681711 0.000270489586151 0.00340044051161 0.000734186019553 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.000760726239984 0.00699868140785 0.00213003347195 0.00101430165331 0.000152145247997 0.00238360888528 0.0 0.000101430165331 0.000659296074653 0.0 0.000253575413328 0.000405720661325 0.000811441322649 0.00121716198397 0.000253575413328 0.0047165026879 0.0 0.00583223450654 0.000710011157318 0.000304290495994 0.00542651384522 0.00968658078913 0.000760726239984 0.000355005578659 0.000152145247997 0.0 0.0205396084796 0.0180038543463 0.030936200426 0.0246475301755 0.00547722892788 0.0102951617811 0.0204381783142 0.00233289380262 0.0176488487676 0.000760726239984 0.0095851506238 0.0569530378335 0.0635967136626 0.22355208439 0.0392027589005 0.0338269601379 0.000405720661325 0.135206410386 0.0398620549751 0.0403692058018 0.0912871487981 0.0259154072421 0.0326605132366 0.0032457652906 0.00669439091186 0.0014707373973 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0404912836767 0.0 0.000158478605388 7.92393026941e-05 0.0355784469097 0.0 0.0 0.00625990491284 0.01735340729 0.0 0.0 0.0184627575277 0.0 7.92393026941e-05 0.0282884310618 0.0 0.0 0.0790808240887 0.00118858954041 0.0 0.0160063391442 0.0 0.0 0.0 0.000316957210777 0.0 0.081616481775 0.000713153724247 0.000475435816165 0.000475435816165 0.146434231379 0.000792393026941 0.000475435816165 0.0338351822504 0.0627575277338 0.0 0.000554675118859 0.0736925515055 0.00110935023772 0.000792393026941 0.0806656101426 0.0458795562599 0.0 0.132171156894 0.0244057052298 0.0351030110935 0.0312995245642 0.0 0.000633914421553 0.0 0.00277337559429 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.00115473441109 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.240184757506 0.0 0.0 0.0 0.0 0.0 0.00115473441109 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.757505773672 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0120088165995 0.0 3.80025841757e-05 0.0 0.0800334422741 0.0 0.0 0.000570038762636 0.00646043930987 0.0 0.0 0.0 0.0 0.0 0.00805654784525 0.0 0.0 0.0 3.80025841757e-05 0.0 0.00497833852702 0.0 0.0 0.0 3.80025841757e-05 0.0 0.12495249677 0.0078285323402 0.0172531732158 0.0234475944364 0.216006688455 0.00566238504218 0.0104507106483 0.0014060956145 0.119100098807 7.60051683514e-05 0.00889260469712 0.0122368321046 0.0223455194953 0.017519191305 0.0806034810367 0.0102987003116 0.000304020673406 0.0242456487041 0.0916242304477 0.0420308580984 0.0267918218439 0.0079805426769 0.00190012920879 0.0 0.0147450026602 7.60051683514e-05 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0124152054269 0.0 0.0137377874483 0.0 0.0185161482998 0.0 0.0 0.0170655744699 0.0123298775545 0.0 0.00435172148982 0.00908741840522 0.00443704936217 0.00482102478775 0.0107086479799 0.0176202056402 0.00285848372371 4.26639361748e-05 0.0 0.0348990997909 0.0287554929818 0.0 0.00627159861769 0.0 0.00337045095781 0.0 0.0282435257477 0.00119459021289 0.0363496736209 0.00102393446819 0.0968471351167 0.00285848372371 0.00204786893639 0.0524339775588 0.081786765647 0.000511967234097 0.00866077904347 0.0255556977687 0.0130125005333 0.0166389351082 0.0315713127693 0.0395068048978 0.00452237723452 0.00115192627672 0.140065702462 0.156661973634 0.0439011903238 0.0 0.00725286914971 4.26639361748e-05 0.00686889372413 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.010395010395 0.0 4.158004158e-05 0.0 0.0128898128898 0.0 0.0 0.0122245322245 0.00756756756757 0.0 0.0 0.0 0.0 0.0 0.00935550935551 0.0 0.0 0.0216632016632 0.0 8.31600831601e-05 0.00390852390852 0.0 0.00253638253638 0.0 0.00120582120582 0.0 0.0778378378378 0.000706860706861 0.0101455301455 0.0002079002079 0.225821205821 0.0031185031185 0.000706860706861 0.0461538461538 0.24656964657 0.0 4.158004158e-05 0.0253638253638 0.00274428274428 0.00607068607069 0.052972972973 0.000956340956341 0.0 0.072474012474 0.0540124740125 0.0294386694387 0.0319334719335 0.00016632016632 0.0045738045738 0.0 0.0255301455301 0.000582120582121 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
7.35672772751e-05 0.0002942691091 7.35672772751e-05 0.0 0.0 0.0 0.000367836386375 0.0 0.0 0.0 0.0 0.000588538218201 0.00044140366365 0.101081438976 0.0 0.00537041124108 0.0 0.0014713455455 0.0023541528728 0.00125064371368 7.35672772751e-05 0.0 0.0 0.0 0.0 0.0 0.0338409475465 0.0313396601192 0.0348708894284 0.0267784889281 0.0357536967557 0.010961524314 0.020598837637 7.35672772751e-05 0.0361951004193 0.0 0.000735672772751 0.0876186272346 0.0478187302288 0.173913043478 0.00691532406386 0.0308246891783 0.00014713455455 0.110130214081 0.120723902008 0.071948797175 0.00044140366365 0.00044140366365 0.0 0.00139777826823 0.000809240050026 0.00228058559553 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0331849453662 0.0 0.000202347227843 0.0 0.0333872925941 0.0 0.0 0.0 0.0400647511129 0.0 0.0 0.0 0.000202347227843 0.0 0.0176042088223 0.0 0.0 0.0 0.0 0.0 0.00161877782274 0.0 0.0 0.0 0.0 0.0 0.123229461756 0.0 0.000202347227843 0.0 0.522258195063 0.0 0.0 0.0 0.166531768515 0.0 0.0 0.0 0.000202347227843 0.0 0.0548360987454 0.0 0.0 0.0 0.0 0.0 0.00404694455686 0.0 0.0 0.0 0.00242816673412 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0719386405713 0.0 0.0 0.0 0.0409944459138 0.0 0.0 0.0444326897646 0.0573922242793 0.0 0.0 0.0 0.0 0.0 0.0454906109495 0.0 0.0 0.024332187252 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.148637926474 0.00370272414705 0.00132240148109 0.00766992859032 0.131182226924 0.00343824385083 0.000528960592436 0.0531605395398 0.128801904258 0.0 0.00423168473949 0.0306797143613 0.00185136207353 0.0446971700608 0.0772282464956 0.000528960592436 0.0 0.0335889976197 0.0386141232478 0.00317376355462 0.000793440888654 0.0 0.0 0.0 0.00158688177731 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.00405268490375 0.0 0.0 0.0 0.00506585612969 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.00405268490375 0.0 0.00101317122594 0.0 0.0 0.0901722391084 0.0 0.120567375887 0.0 0.156028368794 0.0 0.0 0.0303951367781 0.165146909828 0.0 0.0 0.00607902735562 0.0 0.00101317122594 0.0202634245187 0.225937183384 0.00303951367781 0.0 0.0 0.139817629179 0.0172239108409 0.00405268490375 0.00101317122594 0.00101317122594 0.00405268490375 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0142083897158 0.0 0.0 0.0 0.0290933694181 0.0 0.0 0.0 0.00338294993234 0.0 0.0 0.0 0.0 0.0 0.0175913396482 0.0 0.0 0.0 0.0 0.0 0.00338294993234 0.0 0.0 0.0 0.0 0.0 0.0554803788904 0.0128552097429 0.0385656292287 0.0128552097429 0.140730717185 0.00676589986468 0.00879566982409 0.00405953991881 0.165087956698 0.0 0.0 0.0453315290934 0.0656292286874 0.0500676589986 0.041948579161 0.0717185385656 0.0 0.0257104194858 0.141407307172 0.0284167794317 0.00405953991881 0.0 0.0121786197564 0.000676589986468 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0078125 0.0 0.0 0.0 0.029296875 0.0 0.0 0.0 0.00390625 0.0 0.0 0.0 0.0 0.0 0.03515625 0.0 0.0 0.0 0.0 0.0 0.00390625 0.0 0.0 0.0 0.0 0.0 0.095703125 0.0 0.0 0.0 0.33984375 0.0 0.0 0.0 0.169921875 0.0 0.001953125 0.064453125 0.00390625 0.0 0.064453125 0.0 0.0 0.0 0.0 0.0 0.009765625 0.0078125 0.00390625 0.0 0.029296875 0.12890625 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
</data></transition_probabilities>
</opencv_storage>
@@ -0,0 +1,55 @@
/*
* cropped_word_recognition.cpp
*
* A demo program of text recognition in a given cropped word.
* Shows the use of the OCRBeamSearchDecoder class API using the provided default classifier.
*
* Created on: Jul 9, 2015
* Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
*/
#include "opencv2/text.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::text;
int main(int argc, char* argv[])
{
cout << endl << argv[0] << endl << endl;
cout << "A demo program of Scene Text Character Recognition: " << endl;
cout << "Shows the use of the OCRBeamSearchDecoder::ClassifierCallback class using the Single Layer CNN character classifier described in:" << endl;
cout << "Coates, Adam, et al. \"Text detection and character recognition in scene images with unsupervised feature learning.\" ICDAR 2011." << endl << endl;
Mat image;
if(argc>1)
image = imread(argv[1]);
else
{
cout << " Usage: " << argv[0] << " <input_image>" << endl;
cout << " the input image must contain a single character (e.g. scenetext_char01.jpg)." << endl << endl;
return(0);
}
string vocabulary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // must have the same order as the classifier output classes
Ptr<OCRHMMDecoder::ClassifierCallback> ocr = loadOCRHMMClassifierCNN("OCRBeamSearch_CNN_model_data.xml.gz");
double t_r = (double)getTickCount();
vector<int> out_classes;
vector<double> out_confidences;
ocr->eval(image, out_classes, out_confidences);
cout << "OCR output = \"" << vocabulary[out_classes[0]] << "\" with confidence "
<< out_confidences[0] << ". Evaluated in "
<< ((double)getTickCount() - t_r)*1000/getTickFrequency() << " ms." << endl << endl;
return 0;
}
@@ -0,0 +1,83 @@
/*
* cropped_word_recognition.cpp
*
* A demo program of text recognition in a given cropped word.
* Shows the use of the OCRBeamSearchDecoder class API using the provided default classifier.
*
* Created on: Jul 9, 2015
* Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
*/
#include "opencv2/text.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::text;
int main(int argc, char* argv[])
{
cout << endl << argv[0] << endl << endl;
cout << "A demo program of Scene Text cropped word Recognition: " << endl;
cout << "Shows the use of the OCRBeamSearchDecoder class using the Single Layer CNN character classifier described in:" << endl;
cout << "Coates, Adam, et al. \"Text detection and character recognition in scene images with unsupervised feature learning.\" ICDAR 2011." << endl << endl;
Mat image;
if(argc>1)
image = imread(argv[1]);
else
{
cout << " Usage: " << argv[0] << " <input_image>" << endl << endl;
return(0);
}
string vocabulary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // must have the same order as the classifier output classes
vector<string> lexicon; // a list of words expected to be found on the input image
lexicon.push_back(string("abb"));
lexicon.push_back(string("riser"));
lexicon.push_back(string("CHINA"));
lexicon.push_back(string("HERE"));
lexicon.push_back(string("President"));
lexicon.push_back(string("smash"));
lexicon.push_back(string("KUALA"));
lexicon.push_back(string("Produkt"));
lexicon.push_back(string("NINTENDO"));
// Create tailored language model a small given lexicon
Mat transition_p;
createOCRHMMTransitionsTable(vocabulary,lexicon,transition_p);
// An alternative would be to load the default generic language model
// (created from ispell 42869 English words list)
/*Mat transition_p;
string filename = "OCRHMM_transitions_table.xml";
FileStorage fs(filename, FileStorage::READ);
fs["transition_probabilities"] >> transition_p;
fs.release();*/
Mat emission_p = Mat::eye(62,62,CV_64FC1);
// Notice we set here a beam size of 50. This is much faster than using the default value (500).
// 50 works well with our tiny lexicon example, but may not with larger dictionaries.
Ptr<OCRBeamSearchDecoder> ocr = OCRBeamSearchDecoder::create(
loadOCRBeamSearchClassifierCNN("OCRBeamSearch_CNN_model_data.xml.gz"),
vocabulary, transition_p, emission_p, OCR_DECODER_VITERBI, 50);
double t_r = (double)getTickCount();
string output;
vector<Rect> boxes;
vector<string> words;
vector<float> confidences;
ocr->run(image, output, &boxes, &words, &confidences, OCR_LEVEL_WORD);
cout << "OCR output = \"" << output << "\". Decoded in "
<< ((double)getTickCount() - t_r)*1000/getTickFrequency() << " ms." << endl << endl;
return 0;
}
+37
View File
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
#!/usr/bin/python
import sys
import os
import cv2 as cv
import numpy as np
def main():
print('\nDeeptextdetection.py')
print(' A demo script of text box alogorithm of the paper:')
print(' * Minghui Liao et al.: TextBoxes: A Fast Text Detector with a Single Deep Neural Network https://arxiv.org/abs/1611.06779\n')
if (len(sys.argv) < 2):
print(' (ERROR) You must call this script with an argument (path_to_image_to_be_processed)\n')
quit()
if not os.path.isfile('TextBoxes_icdar13.caffemodel') or not os.path.isfile('textbox.prototxt'):
print " Model files not found in current directory. Aborting"
print " See the documentation of text::TextDetectorCNN class to get download links."
quit()
img = cv.imread(str(sys.argv[1]))
textSpotter = cv.text.TextDetectorCNN_create("textbox.prototxt", "TextBoxes_icdar13.caffemodel")
rects, outProbs = textSpotter.detect(img);
vis = img.copy()
thres = 0.6
for r in range(np.shape(rects)[0]):
if outProbs[r] > thres:
rect = rects[r]
cv.rectangle(vis, (rect[0],rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (255, 0, 0), 2)
cv.imshow("Text detection result", vis)
cv.waitKey()
if __name__ == "__main__":
main()
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/python
import sys
import os
import cv2 as cv
import numpy as np
print('\ndetect_er_chars.py')
print(' A simple demo script using the Extremal Region Filter algorithm described in:')
print(' Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012\n')
if (len(sys.argv) < 2):
print(' (ERROR) You must call this script with an argument (path_to_image_to_be_processed)\n')
quit()
pathname = os.path.dirname(sys.argv[0])
img = cv.imread(str(sys.argv[1]))
gray = cv.imread(str(sys.argv[1]),0)
erc1 = cv.text.loadClassifierNM1(pathname+'/trained_classifierNM1.xml')
er1 = cv.text.createERFilterNM1(erc1)
erc2 = cv.text.loadClassifierNM2(pathname+'/trained_classifierNM2.xml')
er2 = cv.text.createERFilterNM2(erc2)
regions = cv.text.detectRegions(gray,er1,er2)
#Visualization
rects = [cv.boundingRect(p.reshape(-1, 1, 2)) for p in regions]
for rect in rects:
cv.rectangle(img, rect[0:2], (rect[0]+rect[2],rect[1]+rect[3]), (0, 0, 0), 2)
for rect in rects:
cv.rectangle(img, rect[0:2], (rect[0]+rect[2],rect[1]+rect[3]), (255, 255, 255), 1)
cv.imshow("Text detection result", img)
cv.waitKey(0)
+52
View File
@@ -0,0 +1,52 @@
#include "opencv2/text.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <sstream>
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::text;
inline void printHelp()
{
cout << " Demo of wordspotting CNN for text recognition." << endl;
cout << " Max Jaderberg et al.: Reading Text in the Wild with Convolutional Neural Networks, IJCV 2015"<<std::endl<<std::endl;
cout << " Usage: program <input_image>" << endl;
cout << " Caffe Model files (dictnet_vgg.caffemodel, dictnet_vgg_deploy.prototxt, dictnet_vgg_labels.txt)"<<endl;
cout << " must be in the current directory." << endl << endl;
cout << " Obtaining Caffe Model files in linux shell:"<<endl;
cout << " wget http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg.caffemodel"<<endl;
cout << " wget http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg_deploy.prototxt"<<endl;
cout << " wget http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg_labels.txt"<<endl<<endl;
}
int main(int argc, const char * argv[])
{
if (argc != 2)
{
printHelp();
exit(1);
}
Mat image = imread(argv[1], IMREAD_GRAYSCALE);
cout << "Read image (" << argv[1] << "): " << image.size << ", channels: " << image.channels() << ", depth: " << image.depth() << endl;
if (image.empty())
{
printHelp();
exit(1);
}
Ptr<OCRHolisticWordRecognizer> wordSpotter = OCRHolisticWordRecognizer::create("dictnet_vgg_deploy.prototxt", "dictnet_vgg.caffemodel", "dictnet_vgg_labels.txt");
std::string word;
vector<float> confs;
wordSpotter->run(image, word, 0, 0, &confs);
cout << "Detected word: '" << word << "', confidence: " << confs[0] << endl;
}
@@ -0,0 +1,343 @@
/*
* textdetection.cpp
*
* A demo program of End-to-end Scene Text Detection and Recognition:
* Shows the use of the Tesseract OCR API with the Extremal Region Filter algorithm described in:
* Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012
*
* Created on: Jul 31, 2014
* Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
*/
#include "opencv2/text.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::text;
//Calculate edit distance between two words
size_t edit_distance(const string& A, const string& B);
size_t min(size_t x, size_t y, size_t z);
bool isRepetitive(const string& s);
bool sort_by_length(const string &a, const string &b);
//Draw ER's in an image via floodFill
void er_draw(vector<Mat> &channels, vector<vector<ERStat> > &regions, vector<Vec2i> group, Mat& segmentation);
//Perform text detection and recognition and evaluate results using edit distance
int main(int argc, char* argv[])
{
cout << endl << argv[0] << endl << endl;
cout << "A demo program of End-to-end Scene Text Detection and Recognition: " << endl;
cout << "Shows the use of the Tesseract OCR API with the Extremal Region Filter algorithm described in:" << endl;
cout << "Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012" << endl << endl;
Mat image;
if(argc>1)
image = imread(argv[1]);
else
{
cout << " Usage: " << argv[0] << " <input_image> [<gt_word1> ... <gt_wordN>]" << endl;
return(0);
}
cout << "IMG_W=" << image.cols << endl;
cout << "IMG_H=" << image.rows << endl;
/*Text Detection*/
// Extract channels to be processed individually
vector<Mat> channels;
Mat grey;
cvtColor(image,grey,COLOR_RGB2GRAY);
// Notice here we are only using grey channel, see textdetection.cpp for example with more channels
channels.push_back(grey);
channels.push_back(255-grey);
double t_d = (double)getTickCount();
// Create ERFilter objects with the 1st and 2nd stage default classifiers
Ptr<ERFilter> er_filter1 = createERFilterNM1(loadClassifierNM1("trained_classifierNM1.xml"),8,0.00015f,0.13f,0.2f,true,0.1f);
Ptr<ERFilter> er_filter2 = createERFilterNM2(loadClassifierNM2("trained_classifierNM2.xml"),0.5);
vector<vector<ERStat> > regions(channels.size());
// Apply the default cascade classifier to each independent channel (could be done in parallel)
for (int c=0; c<(int)channels.size(); c++)
{
er_filter1->run(channels[c], regions[c]);
er_filter2->run(channels[c], regions[c]);
}
cout << "TIME_REGION_DETECTION = " << ((double)getTickCount() - t_d)*1000/getTickFrequency() << endl;
Mat out_img_decomposition= Mat::zeros(image.rows+2, image.cols+2, CV_8UC1);
vector<Vec2i> tmp_group;
for (int i=0; i<(int)regions.size(); i++)
{
for (int j=0; j<(int)regions[i].size();j++)
{
tmp_group.push_back(Vec2i(i,j));
}
Mat tmp= Mat::zeros(image.rows+2, image.cols+2, CV_8UC1);
er_draw(channels, regions, tmp_group, tmp);
if (i > 0)
tmp = tmp / 2;
out_img_decomposition = out_img_decomposition | tmp;
tmp_group.clear();
}
double t_g = (double)getTickCount();
// Detect character groups
vector< vector<Vec2i> > nm_region_groups;
vector<Rect> nm_boxes;
erGrouping(image, channels, regions, nm_region_groups, nm_boxes,ERGROUPING_ORIENTATION_HORIZ);
cout << "TIME_GROUPING = " << ((double)getTickCount() - t_g)*1000/getTickFrequency() << endl;
/*Text Recognition (OCR)*/
double t_r = (double)getTickCount();
Ptr<OCRTesseract> ocr = OCRTesseract::create();
cout << "TIME_OCR_INITIALIZATION = " << ((double)getTickCount() - t_r)*1000/getTickFrequency() << endl;
string output;
Mat out_img;
Mat out_img_detection;
Mat out_img_segmentation = Mat::zeros(image.rows+2, image.cols+2, CV_8UC1);
image.copyTo(out_img);
image.copyTo(out_img_detection);
float scale_img = 600.f/image.rows;
float scale_font = (float)(2-scale_img)/1.4f;
vector<string> words_detection;
t_r = (double)getTickCount();
for (int i=0; i<(int)nm_boxes.size(); i++)
{
rectangle(out_img_detection, nm_boxes[i].tl(), nm_boxes[i].br(), Scalar(0,255,255), 3);
Mat group_img = Mat::zeros(image.rows+2, image.cols+2, CV_8UC1);
er_draw(channels, regions, nm_region_groups[i], group_img);
Mat group_segmentation;
group_img.copyTo(group_segmentation);
//image(nm_boxes[i]).copyTo(group_img);
group_img(nm_boxes[i]).copyTo(group_img);
copyMakeBorder(group_img,group_img,15,15,15,15,BORDER_CONSTANT,Scalar(0));
vector<Rect> boxes;
vector<string> words;
vector<float> confidences;
ocr->run(group_img, output, &boxes, &words, &confidences, OCR_LEVEL_WORD);
output.erase(remove(output.begin(), output.end(), '\n'), output.end());
//cout << "OCR output = \"" << output << "\" length = " << output.size() << endl;
if (output.size() < 3)
continue;
for (int j=0; j<(int)boxes.size(); j++)
{
boxes[j].x += nm_boxes[i].x-15;
boxes[j].y += nm_boxes[i].y-15;
//cout << " word = " << words[j] << "\t confidence = " << confidences[j] << endl;
if ((words[j].size() < 2) || (confidences[j] < 51) ||
((words[j].size()==2) && (words[j][0] == words[j][1])) ||
((words[j].size()< 4) && (confidences[j] < 60)) ||
isRepetitive(words[j]))
continue;
words_detection.push_back(words[j]);
rectangle(out_img, boxes[j].tl(), boxes[j].br(), Scalar(255,0,255),3);
Size word_size = getTextSize(words[j], FONT_HERSHEY_SIMPLEX, (double)scale_font, (int)(3*scale_font), NULL);
rectangle(out_img, boxes[j].tl()-Point(3,word_size.height+3), boxes[j].tl()+Point(word_size.width,0), Scalar(255,0,255),-1);
putText(out_img, words[j], boxes[j].tl()-Point(1,1), FONT_HERSHEY_SIMPLEX, scale_font, Scalar(255,255,255),(int)(3*scale_font));
out_img_segmentation = out_img_segmentation | group_segmentation;
}
}
cout << "TIME_OCR = " << ((double)getTickCount() - t_r)*1000/getTickFrequency() << endl;
/* Recognition evaluation with (approximate) Hungarian matching and edit distances */
if(argc>2)
{
int num_gt_characters = 0;
vector<string> words_gt;
for (int i=2; i<argc; i++)
{
string s = string(argv[i]);
if (s.size() > 0)
{
words_gt.push_back(string(argv[i]));
//cout << " GT word " << words_gt[words_gt.size()-1] << endl;
num_gt_characters += (int)(words_gt[words_gt.size()-1].size());
}
}
if (words_detection.empty())
{
//cout << endl << "number of characters in gt = " << num_gt_characters << endl;
cout << "TOTAL_EDIT_DISTANCE = " << num_gt_characters << endl;
cout << "EDIT_DISTANCE_RATIO = 1" << endl;
}
else
{
sort(words_gt.begin(),words_gt.end(),sort_by_length);
int max_dist=0;
vector< vector<int> > assignment_mat;
for (int i=0; i<(int)words_gt.size(); i++)
{
vector<int> assignment_row(words_detection.size(),0);
assignment_mat.push_back(assignment_row);
for (int j=0; j<(int)words_detection.size(); j++)
{
assignment_mat[i][j] = (int)(edit_distance(words_gt[i],words_detection[j]));
max_dist = max(max_dist,assignment_mat[i][j]);
}
}
vector<int> words_detection_matched;
int total_edit_distance = 0;
int tp=0, fp=0, fn=0;
for (int search_dist=0; search_dist<=max_dist; search_dist++)
{
for (int i=0; i<(int)assignment_mat.size(); i++)
{
int min_dist_idx = (int)distance(assignment_mat[i].begin(),
min_element(assignment_mat[i].begin(),assignment_mat[i].end()));
if (assignment_mat[i][min_dist_idx] == search_dist)
{
//cout << " GT word \"" << words_gt[i] << "\" best match \"" << words_detection[min_dist_idx] << "\" with dist " << assignment_mat[i][min_dist_idx] << endl;
if(search_dist == 0)
tp++;
else { fp++; fn++; }
total_edit_distance += assignment_mat[i][min_dist_idx];
words_detection_matched.push_back(min_dist_idx);
words_gt.erase(words_gt.begin()+i);
assignment_mat.erase(assignment_mat.begin()+i);
for (int j=0; j<(int)assignment_mat.size(); j++)
{
assignment_mat[j][min_dist_idx]=INT_MAX;
}
i--;
}
}
}
for (int j=0; j<(int)words_gt.size(); j++)
{
//cout << " GT word \"" << words_gt[j] << "\" no match found" << endl;
fn++;
total_edit_distance += (int)words_gt[j].size();
}
for (int j=0; j<(int)words_detection.size(); j++)
{
if (find(words_detection_matched.begin(),words_detection_matched.end(),j) == words_detection_matched.end())
{
//cout << " Detection word \"" << words_detection[j] << "\" no match found" << endl;
fp++;
total_edit_distance += (int)words_detection[j].size();
}
}
//cout << endl << "number of characters in gt = " << num_gt_characters << endl;
cout << "TOTAL_EDIT_DISTANCE = " << total_edit_distance << endl;
cout << "EDIT_DISTANCE_RATIO = " << (float)total_edit_distance / num_gt_characters << endl;
cout << "TP = " << tp << endl;
cout << "FP = " << fp << endl;
cout << "FN = " << fn << endl;
}
}
//resize(out_img_detection,out_img_detection,Size(image.cols*scale_img,image.rows*scale_img),0,0,INTER_LINEAR_EXACT);
//imshow("detection", out_img_detection);
//imwrite("detection.jpg", out_img_detection);
//resize(out_img,out_img,Size(image.cols*scale_img,image.rows*scale_img),0,0,INTER_LINEAR_EXACT);
namedWindow("recognition",WINDOW_NORMAL);
imshow("recognition", out_img);
waitKey(0);
//imwrite("recognition.jpg", out_img);
//imwrite("segmentation.jpg", out_img_segmentation);
//imwrite("decomposition.jpg", out_img_decomposition);
return 0;
}
size_t min(size_t x, size_t y, size_t z)
{
return x < y ? min(x,z) : min(y,z);
}
size_t edit_distance(const string& A, const string& B)
{
size_t NA = A.size();
size_t NB = B.size();
vector< vector<size_t> > M(NA + 1, vector<size_t>(NB + 1));
for (size_t a = 0; a <= NA; ++a)
M[a][0] = a;
for (size_t b = 0; b <= NB; ++b)
M[0][b] = b;
for (size_t a = 1; a <= NA; ++a)
for (size_t b = 1; b <= NB; ++b)
{
size_t x = M[a-1][b] + 1;
size_t y = M[a][b-1] + 1;
size_t z = M[a-1][b-1] + (A[a-1] == B[b-1] ? 0 : 1);
M[a][b] = min(x,y,z);
}
return M[A.size()][B.size()];
}
bool isRepetitive(const string& s)
{
int count = 0;
for (int i=0; i<(int)s.size(); i++)
{
if ((s[i] == 'i') ||
(s[i] == 'l') ||
(s[i] == 'I'))
count++;
}
if (count > ((int)s.size()+1)/2)
{
return true;
}
return false;
}
void er_draw(vector<Mat> &channels, vector<vector<ERStat> > &regions, vector<Vec2i> group, Mat& segmentation)
{
for (int r=0; r<(int)group.size(); r++)
{
ERStat er = regions[group[r][0]][group[r][1]];
if (er.parent != NULL) // deprecate the root region
{
int newMaskVal = 255;
int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
floodFill(channels[group[r][0]],segmentation,Point(er.pixel%channels[group[r][0]].cols,er.pixel/channels[group[r][0]].cols),
Scalar(255),0,Scalar(er.level),Scalar(0),flags);
}
}
}
bool sort_by_length(const string &a, const string &b){return (a.size()>b.size());}
Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 538 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 523 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 541 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 528 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 682 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

@@ -0,0 +1,116 @@
/*
* segmented_word_recognition.cpp
*
* A demo program on segmented word recognition.
* Shows the use of the OCRHMMDecoder API with the two provided default character classifiers.
*
* Created on: Jul 31, 2015
* Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
*/
#include "opencv2/text.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
using namespace text;
int main(int argc, char* argv[]) {
const String keys =
"{help h usage ? | | print this message.}"
"{@image | | source image for recognition.}"
"{@mask | | binary segmentation mask where each contour is a character.}"
"{lexicon lex l | | (optional) lexicon provided as a list of comma separated words.}"
;
CommandLineParser parser(argc, argv, keys);
parser.about("\nSegmented word recognition.\nA demo program on segmented word recognition. Shows the use of the OCRHMMDecoder API with the two provided default character classifiers.\n");
String filename1 = parser.get<String>(0);
String filename2 = parser.get<String>(1);
parser.printMessage();
cout << endl << endl;
if ((parser.has("help")) || (filename1.size()==0))
{
return 0;
}
if (!parser.check())
{
parser.printErrors();
return 0;
}
Mat image = imread(filename1);
Mat mask;
if (filename2.size() > 0)
mask = imread(filename2);
else
image.copyTo(mask);
// be sure the mask is a binary image
cvtColor(mask, mask, COLOR_BGR2GRAY);
threshold(mask, mask, 128., 255, THRESH_BINARY);
// character recognition vocabulary
string voc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// Emission probabilities for the HMM language model (identity matrix by default)
Mat emissionProbabilities = Mat::eye((int)voc.size(), (int)voc.size(), CV_64FC1);
// Bigram transition probabilities for the HMM language model
Mat transitionProbabilities;
string lex = parser.get<string>("lex");
if (lex.size()>0)
{
// Build tailored language model for the provided lexicon
vector<string> lexicon;
size_t pos = 0;
string delimiter = ",";
std::string token;
while ((pos = lex.find(delimiter)) != std::string::npos) {
token = lex.substr(0, pos);
lexicon.push_back(token);
lex.erase(0, pos + delimiter.length());
}
lexicon.push_back(lex);
createOCRHMMTransitionsTable(voc,lexicon,transitionProbabilities);
} else {
// Or load the generic language model (from Aspell English dictionary)
FileStorage fs("./OCRHMM_transitions_table.xml", FileStorage::READ);
fs["transition_probabilities"] >> transitionProbabilities;
fs.release();
}
Ptr<OCRTesseract> ocrTes = OCRTesseract::create();
Ptr<OCRHMMDecoder> ocrNM = OCRHMMDecoder::create(
loadOCRHMMClassifierNM("./OCRHMM_knn_model_data.xml.gz"),
voc, transitionProbabilities, emissionProbabilities);
Ptr<OCRHMMDecoder> ocrCNN = OCRHMMDecoder::create(
loadOCRHMMClassifierCNN("OCRBeamSearch_CNN_model_data.xml.gz"),
voc, transitionProbabilities, emissionProbabilities);
std::string output;
double t_r = (double)getTickCount();
ocrTes->run(mask, output);
output.erase(remove(output.begin(), output.end(), '\n'), output.end());
cout << " OCR_Tesseract output \"" << output << "\". Done in "
<< ((double)getTickCount() - t_r)*1000/getTickFrequency() << " ms." << endl;
t_r = (double)getTickCount();
ocrNM->run(mask, output);
cout << " OCR_NM output \"" << output << "\". Done in "
<< ((double)getTickCount() - t_r)*1000/getTickFrequency() << " ms." << endl;
t_r = (double)getTickCount();
ocrCNN->run(image, mask, output);
cout << " OCR_CNN output \"" << output << "\". Done in "
<< ((double)getTickCount() - t_r)*1000/getTickFrequency() << " ms." << endl;
}
@@ -0,0 +1,122 @@
#include <opencv2/text.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;
namespace
{
void printHelpStr(const string& progFname)
{
cout << " Demo of text recognition CNN for text detection." << endl
<< " Max Jaderberg et al.: Reading Text in the Wild with Convolutional Neural Networks, IJCV 2015"<<endl<<endl
<< " Usage: " << progFname << " <output_file> <input_image>" << endl
<< " Caffe Model files (textbox.prototxt, TextBoxes_icdar13.caffemodel)"<<endl
<< " must be in the current directory. See the documentation of text::TextDetectorCNN class to get download links." << endl
<< " Obtaining text recognition Caffe Model files in linux shell:" << endl
<< " wget http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg.caffemodel" << endl
<< " wget http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg_deploy.prototxt" << endl
<< " wget http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg_labels.txt" <<endl << endl;
}
bool fileExists (const string& filename)
{
ifstream f(filename.c_str());
return f.good();
}
void textbox_draw(Mat src, std::vector<Rect>& groups, std::vector<float>& probs, std::vector<int>& indexes)
{
for (size_t i = 0; i < indexes.size(); i++)
{
if (src.type() == CV_8UC3)
{
Rect currrentBox = groups[indexes[i]];
rectangle(src, currrentBox, Scalar( 0, 255, 255 ), 2, LINE_AA);
String label = format("%.2f", probs[indexes[i]]);
std::cout << "text box: " << currrentBox << " confidence: " << probs[indexes[i]] << "\n";
int baseLine = 0;
Size labelSize = getTextSize(label, FONT_HERSHEY_PLAIN, 1, 1, &baseLine);
int yLeftBottom = std::max(currrentBox.y, labelSize.height);
rectangle(src, Point(currrentBox.x, yLeftBottom - labelSize.height),
Point(currrentBox.x + labelSize.width, yLeftBottom + baseLine), Scalar( 255, 255, 255 ), FILLED);
putText(src, label, Point(currrentBox.x, yLeftBottom), FONT_HERSHEY_PLAIN, 1, Scalar( 0,0,0 ), 1, LINE_AA);
}
else
rectangle(src, groups[i], Scalar( 255 ), 3, 8 );
}
}
}
int main(int argc, const char * argv[])
{
if (argc < 2)
{
printHelpStr(argv[0]);
cout << "Insufiecient parameters. Aborting!" << endl;
exit(1);
}
const string modelArch = "textbox.prototxt";
const string moddelWeights = "TextBoxes_icdar13.caffemodel";
if (!fileExists(modelArch) || !fileExists(moddelWeights))
{
printHelpStr(argv[0]);
cout << "Model files not found in the current directory. Aborting!" << endl;
exit(1);
}
Mat image = imread(String(argv[1]), IMREAD_COLOR);
cout << "Starting Text Box Demo" << endl;
Ptr<text::TextDetectorCNN> textSpotter =
text::TextDetectorCNN::create(modelArch, moddelWeights);
vector<Rect> bbox;
vector<float> outProbabillities;
textSpotter->detect(image, bbox, outProbabillities);
std::vector<int> indexes;
cv::dnn::NMSBoxes(bbox, outProbabillities, 0.4f, 0.5f, indexes);
Mat image_copy = image.clone();
textbox_draw(image_copy, bbox, outProbabillities, indexes);
imshow("Text detection", image_copy);
image_copy = image.clone();
Ptr<text::OCRHolisticWordRecognizer> wordSpotter =
text::OCRHolisticWordRecognizer::create("dictnet_vgg_deploy.prototxt", "dictnet_vgg.caffemodel", "dictnet_vgg_labels.txt");
for(size_t i = 0; i < indexes.size(); i++)
{
Mat wordImg;
cvtColor(image(bbox[indexes[i]]), wordImg, COLOR_BGR2GRAY);
string word;
vector<float> confs;
wordSpotter->run(wordImg, word, NULL, NULL, &confs);
Rect currrentBox = bbox[indexes[i]];
rectangle(image_copy, currrentBox, Scalar( 0, 255, 255 ), 2, LINE_AA);
int baseLine = 0;
Size labelSize = getTextSize(word, FONT_HERSHEY_PLAIN, 1, 1, &baseLine);
int yLeftBottom = std::max(currrentBox.y, labelSize.height);
rectangle(image_copy, Point(currrentBox.x, yLeftBottom - labelSize.height),
Point(currrentBox.x + labelSize.width, yLeftBottom + baseLine), Scalar( 255, 255, 255 ), FILLED);
putText(image_copy, word, Point(currrentBox.x, yLeftBottom), FONT_HERSHEY_PLAIN, 1, Scalar( 0,0,0 ), 1, LINE_AA);
}
imshow("Text recognition", image_copy);
cout << "Recognition finished. Press any key to exit.\n";
waitKey();
return 0;
}
File diff suppressed because it is too large Load Diff
+96
View File
@@ -0,0 +1,96 @@
#include <opencv2/text.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/dnn.hpp>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace cv;
namespace
{
std::string getHelpStr(const std::string& progFname)
{
std::stringstream out;
out << " Demo of text detection CNN for text detection." << std::endl
<< " Minghui Liao, Baoguang Shi, Xiang Bai, Xinggang Wang, Wenyu Liu: TextBoxes: A Fast Text Detector with a Single Deep Neural Network, AAAI2017\n\n"
<< " Usage: " << progFname << " <output_file> <input_image>" << std::endl
<< " Caffe Model files (textbox.prototxt, TextBoxes_icdar13.caffemodel)"<<std::endl
<< " must be in the current directory. See the documentation of text::TextDetectorCNN class to get download links." << std::endl;
return out.str();
}
bool fileExists (const std::string& filename)
{
std::ifstream f(filename.c_str());
return f.good();
}
void textbox_draw(Mat src, std::vector<Rect>& groups, std::vector<float>& probs, std::vector<int>& indexes)
{
for (size_t i = 0; i < indexes.size(); i++)
{
if (src.type() == CV_8UC3)
{
Rect currrentBox = groups[indexes[i]];
rectangle(src, currrentBox, Scalar( 0, 255, 255 ), 2, LINE_AA);
String label = format("%.2f", probs[indexes[i]]);
std::cout << "text box: " << currrentBox << " confidence: " << probs[indexes[i]] << "\n";
int baseLine = 0;
Size labelSize = getTextSize(label, FONT_HERSHEY_PLAIN, 1, 1, &baseLine);
int yLeftBottom = std::max(currrentBox.y, labelSize.height);
rectangle(src, Point(currrentBox.x, yLeftBottom - labelSize.height),
Point(currrentBox.x + labelSize.width, yLeftBottom + baseLine), Scalar( 255, 255, 255 ), FILLED);
putText(src, label, Point(currrentBox.x, yLeftBottom), FONT_HERSHEY_PLAIN, 1, Scalar( 0,0,0 ), 1, LINE_AA);
}
else
rectangle(src, groups[i], Scalar( 255 ), 3, 8 );
}
}
}
int main(int argc, const char * argv[])
{
if (argc < 2)
{
std::cout << getHelpStr(argv[0]);
std::cout << "Insufiecient parameters. Aborting!" << std::endl;
exit(1);
}
const std::string modelArch = "textbox.prototxt";
const std::string moddelWeights = "TextBoxes_icdar13.caffemodel";
if (!fileExists(modelArch) || !fileExists(moddelWeights))
{
std::cout << getHelpStr(argv[0]);
std::cout << "Model files not found in the current directory. Aborting!" << std::endl;
exit(1);
}
Mat image = imread(String(argv[1]), IMREAD_COLOR);
std::cout << "Starting Text Box Demo" << std::endl;
Ptr<text::TextDetectorCNN> textSpotter =
text::TextDetectorCNN::create(modelArch, moddelWeights);
std::vector<Rect> bbox;
std::vector<float> outProbabillities;
textSpotter->detect(image, bbox, outProbabillities);
std::vector<int> indexes;
cv::dnn::NMSBoxes(bbox, outProbabillities, 0.3f, 0.4f, indexes);
textbox_draw(image, bbox, outProbabillities, indexes);
imshow("TextBox Demo",image);
std::cout << "Done!" << std::endl << std::endl;
std::cout << "Press any key to exit." << std::endl << std::endl;
waitKey();
return 0;
}
+127
View File
@@ -0,0 +1,127 @@
/*
* textdetection.cpp
*
* A demo program of the Extremal Region Filter algorithm described in
* Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012
*
* Created on: Sep 23, 2013
* Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
*/
#include "opencv2/text.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <vector>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace cv;
using namespace cv::text;
void show_help_and_exit(const char *cmd);
void groups_draw(Mat &src, vector<Rect> &groups);
void er_show(vector<Mat> &channels, vector<vector<ERStat> > &regions);
int main(int argc, const char * argv[])
{
cout << endl << argv[0] << endl << endl;
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;
if (argc < 2) show_help_and_exit(argv[0]);
Mat src = imread(argv[1]);
// Extract channels to be processed individually
vector<Mat> channels;
computeNMChannels(src, channels);
int cn = (int)channels.size();
// Append negative channels to detect ER- (bright regions over dark background)
for (int c = 0; c < cn-1; c++)
channels.push_back(255-channels[c]);
// Create ERFilter objects with the 1st and 2nd stage default classifiers
Ptr<ERFilter> er_filter1 = createERFilterNM1(loadClassifierNM1("trained_classifierNM1.xml"),16,0.00015f,0.13f,0.2f,true,0.1f);
Ptr<ERFilter> er_filter2 = createERFilterNM2(loadClassifierNM2("trained_classifierNM2.xml"),0.5);
vector<vector<ERStat> > regions(channels.size());
// Apply the default cascade classifier to each independent channel (could be done in parallel)
cout << "Extracting Class Specific Extremal Regions from " << (int)channels.size() << " channels ..." << endl;
cout << " (...) this may take a while (...)" << endl << endl;
for (int c=0; c<(int)channels.size(); c++)
{
er_filter1->run(channels[c], regions[c]);
er_filter2->run(channels[c], regions[c]);
}
// Detect character groups
cout << "Grouping extracted ERs ... ";
vector< vector<Vec2i> > region_groups;
vector<Rect> groups_boxes;
erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_HORIZ);
//erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_ANY, "./trained_classifier_erGrouping.xml", 0.5);
// draw groups
groups_draw(src, groups_boxes);
imshow("grouping",src);
cout << "Done!" << endl << endl;
cout << "Press 'space' to show the extracted Extremal Regions, any other key to exit." << endl << endl;
if ((waitKey()&0xff) == ' ')
er_show(channels,regions);
// memory clean-up
er_filter1.release();
er_filter2.release();
regions.clear();
if (!groups_boxes.empty())
{
groups_boxes.clear();
}
}
// helper functions
void show_help_and_exit(const char *cmd)
{
cout << " Usage: " << cmd << " <input_image> " << endl;
cout << " Default classifier files (trained_classifierNM*.xml) must be in current directory" << endl << endl;
exit(-1);
}
void groups_draw(Mat &src, vector<Rect> &groups)
{
for (int i=(int)groups.size()-1; i>=0; i--)
{
if (src.type() == CV_8UC3)
rectangle(src,groups.at(i).tl(),groups.at(i).br(),Scalar( 0, 255, 255 ), 3, 8 );
else
rectangle(src,groups.at(i).tl(),groups.at(i).br(),Scalar( 255 ), 3, 8 );
}
}
void er_show(vector<Mat> &channels, vector<vector<ERStat> > &regions)
{
for (int c=0; c<(int)channels.size(); c++)
{
Mat dst = Mat::zeros(channels[0].rows+2,channels[0].cols+2,CV_8UC1);
for (int r=0; r<(int)regions[c].size(); r++)
{
ERStat er = regions[c][r];
if (er.parent != NULL) // deprecate the root region
{
int newMaskVal = 255;
int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
floodFill(channels[c],dst,Point(er.pixel%channels[c].cols,er.pixel/channels[c].cols),
Scalar(255),0,Scalar(er.level),Scalar(0),flags);
}
}
char buff[20]; char *buff_ptr = buff;
sprintf(buff, "channel %d", c);
imshow(buff_ptr, dst);
}
waitKey(-1);
}
+56
View File
@@ -0,0 +1,56 @@
#!/usr/bin/python
import sys
import os
import cv2 as cv
import numpy as np
print('\ntextdetection.py')
print(' A demo script of the Extremal Region Filter algorithm described in:')
print(' Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012\n')
if (len(sys.argv) < 2):
print(' (ERROR) You must call this script with an argument (path_to_image_to_be_processed)\n')
quit()
pathname = os.path.dirname(sys.argv[0])
img = cv.imread(str(sys.argv[1]))
# for visualization
vis = img.copy()
# Extract channels to be processed individually
channels = list(cv.text.computeNMChannels(img))
# Append negative channels to detect ER- (bright regions over dark background)
cn = len(channels)-1
for c in range(0,cn):
channels.append(255-channels[c])
# Apply the default cascade classifier to each independent channel (could be done in parallel)
erc1 = cv.text.loadClassifierNM1('trained_classifierNM1.xml')
er1 = cv.text.createERFilterNM1(erc1,16,0.00015,0.13,0.2,True,0.1)
erc2 = cv.text.loadClassifierNM2('trained_classifierNM2.xml')
er2 = cv.text.createERFilterNM2(erc2,0.5)
print("Extracting Class Specific Extremal Regions from "+str(len(channels))+" channels ...")
print(" (...) this may take a while (...)")
for channel in channels:
regions = cv.text.detectRegions(channel,er1,er2)
rects = cv.text.erGrouping(img,channel,[r.tolist() for r in regions])
#rects = cv.text.erGrouping(img,channel,[x.tolist() for x in regions], cv.text.ERGROUPING_ORIENTATION_ANY,'../../GSoC2014/opencv_contrib/modules/text/samples/trained_classifier_erGrouping.xml',0.5)
#Visualization
for rect in rects:
cv.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (0, 0, 0), 2)
cv.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (255, 255, 255), 1)
#Visualization
cv.imshow("Text detection result", vis)
cv.waitKey(0)
@@ -0,0 +1,89 @@
// Sample code which demonstrates the working of
// stroke width transform in the text module of OpenCV
#include <opencv2/text.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
using namespace cv;
static void help(const CommandLineParser& cmd, const string& errorMessage)
{
cout << errorMessage << endl;
cout << "Avaible options:" << endl;
cmd.printMessage();
}
static bool fileExists (const string& filename)
{
ifstream f(filename.c_str());
return f.good();
}
int main(int argc, const char * argv[])
{
const char* keys =
"{help h usage ? |false | print this message }"
"{@image | | path to image }"
"{@darkOnLight |false | indicates whether text to be extracted is dark on a light brackground. Defaults to false. }"
;
CommandLineParser cmd(argc, argv, keys);
if(cmd.get<bool>("help"))
{
help(cmd, "Usage: ./textdetection_swt [options] \nExample: ./textdetection_swt scenetext_segmented_word03.jpg true");
return EXIT_FAILURE;
}
string filepath = cmd.get<string>("@image");
if (!fileExists(filepath)) {
help(cmd, "ERROR: Could not find the image file. Please check the path.");
return EXIT_FAILURE;
}
bool dark_on_light = cmd.get<bool>("@darkOnLight");
Mat image = imread(filepath, IMREAD_COLOR);
if (image.empty())
{
help(cmd, "ERROR: Could not load the image file");
return EXIT_FAILURE;
}
cout << "Starting SWT Text Detection Demo with dark_on_light variable set to " << dark_on_light << endl;
imshow("Input Image", image);
waitKey(1);
vector<cv::Rect> components;
Mat out;
vector<cv::Rect> regions;
cv::text::detectTextSWT(image, components, dark_on_light, out, regions);
imshow ("Letter Candidates", out);
waitKey(1);
cout << components.size() << " letter candidates found." << endl;
Mat image_copy = image.clone();
for (unsigned int i = 0; i < regions.size(); i++) {
rectangle(image_copy, regions[i], cv::Scalar(0, 0, 0), 3);
}
cout << regions.size() << " chains were obtained after merging suitable pairs" << endl;
cout << "Recognition finished. Press any key to exit..." << endl;
imshow ("Chains After Merging", image_copy);
waitKey();
return 0;
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+408
View File
@@ -0,0 +1,408 @@
/*
* webcam-demo.cpp
*
* A demo program of End-to-end Scene Text Detection and Recognition using webcam or video.
*
* Created on: Jul 31, 2014
* Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
*/
#include "opencv2/text.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/features.hpp"
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::text;
//ERStat extraction is done in parallel for different channels
class Parallel_extractCSER: public cv::ParallelLoopBody
{
private:
vector<Mat> &channels;
vector< vector<ERStat> > &regions;
vector< Ptr<ERFilter> > er_filter1;
vector< Ptr<ERFilter> > er_filter2;
public:
Parallel_extractCSER(vector<Mat> &_channels, vector< vector<ERStat> > &_regions,
vector<Ptr<ERFilter> >_er_filter1, vector<Ptr<ERFilter> >_er_filter2)
: channels(_channels),regions(_regions),er_filter1(_er_filter1),er_filter2(_er_filter2) {}
virtual void operator()( const cv::Range &r ) const CV_OVERRIDE
{
for (int c=r.start; c < r.end; c++)
{
er_filter1[c]->run(channels[c], regions[c]);
er_filter2[c]->run(channels[c], regions[c]);
}
}
Parallel_extractCSER & operator=(const Parallel_extractCSER &a);
};
//OCR recognition is done in parallel for different detections
template <class T>
class Parallel_OCR: public cv::ParallelLoopBody
{
private:
vector<Mat> &detections;
vector<string> &outputs;
vector< vector<Rect> > &boxes;
vector< vector<string> > &words;
vector< vector<float> > &confidences;
vector< Ptr<T> > &ocrs;
public:
Parallel_OCR(vector<Mat> &_detections, vector<string> &_outputs, vector< vector<Rect> > &_boxes,
vector< vector<string> > &_words, vector< vector<float> > &_confidences,
vector< Ptr<T> > &_ocrs)
: detections(_detections), outputs(_outputs), boxes(_boxes), words(_words),
confidences(_confidences), ocrs(_ocrs)
{}
virtual void operator()( const cv::Range &r ) const CV_OVERRIDE
{
for (int c=r.start; c < r.end; c++)
{
ocrs[c%ocrs.size()]->run(detections[c], outputs[c], &boxes[c], &words[c], &confidences[c], OCR_LEVEL_WORD);
}
}
Parallel_OCR & operator=(const Parallel_OCR &a);
};
//Discard wrongly recognised strings
bool isRepetitive(const string& s);
//Draw ER's in an image via floodFill
void er_draw(vector<Mat> &channels, vector<vector<ERStat> > &regions, vector<Vec2i> group, Mat& segmentation);
const char* keys =
{
"{@input | 0 | camera index or video file name}"
"{ image i | | specify input image}"
};
//Perform text detection and recognition from webcam or video
int main(int argc, char* argv[])
{
CommandLineParser parser(argc, argv, keys);
cout << "A demo program of End-to-end Scene Text Detection and Recognition using webcam or video." << endl << endl;
cout << " Keys: " << endl;
cout << " Press 'r' to switch between MSER/CSER regions." << endl;
cout << " Press 'g' to switch between Horizontal and Arbitrary oriented grouping." << endl;
cout << " Press 'o' to switch between OCRTesseract/OCRHMMDecoder recognition." << endl;
cout << " Press 's' to scale down frame size to 320x240." << endl;
cout << " Press 'ESC' to exit." << endl << endl;
parser.printMessage();
VideoCapture cap;
Mat frame, image, gray, out_img;
String input = parser.get<String>("@input");
String image_file_name = parser.get<String>("image");
if (image_file_name != "")
{
image = imread(image_file_name);
if (image.empty())
{
cout << "\nunable to open " << image_file_name << "\nprogram terminated!\n";
return 1;
}
else
{
cout << "\nimage " << image_file_name << " loaded!\n";
frame = image.clone();
}
}
else
{
cout << "\nInitializing capturing... ";
if (input.size() == 1 && isdigit(input[0]))
cap.open(input[0] - '0');
else
cap.open(input);
if (!cap.isOpened())
{
cout << "\nCould not initialize capturing!\n";
return 1;
}
cout << " Done!" << endl;
cap.read(frame);
}
namedWindow("recognition",WINDOW_NORMAL);
imshow("recognition", frame);
waitKey(1);
bool downsize = false;
int REGION_TYPE = 1;
int GROUPING_ALGORITHM = 0;
int RECOGNITION = 0;
String region_types_str[2] = {"ERStats", "MSER"};
String grouping_algorithms_str[2] = {"exhaustive_search", "multioriented"};
String recognitions_str[2] = {"Tesseract", "NM_chain_features + KNN"};
vector<Mat> channels;
vector<vector<ERStat> > regions(2); //two channels
// Create ERFilter objects with the 1st and 2nd stage default classifiers
// since er algorithm is not reentrant we need one filter for channel
vector< Ptr<ERFilter> > er_filters1;
vector< Ptr<ERFilter> > er_filters2;
for (int i=0; i<2; i++)
{
Ptr<ERFilter> er_filter1 = createERFilterNM1(loadClassifierNM1("trained_classifierNM1.xml"),8,0.00015f,0.13f,0.2f,true,0.1f);
Ptr<ERFilter> er_filter2 = createERFilterNM2(loadClassifierNM2("trained_classifierNM2.xml"),0.5);
er_filters1.push_back(er_filter1);
er_filters2.push_back(er_filter2);
}
//Initialize OCR engine (we initialize 10 instances in order to work several recognitions in parallel)
cout << "Initializing OCR engines ... ";
int num_ocrs = 10;
vector< Ptr<OCRTesseract> > ocrs;
for (int o=0; o<num_ocrs; o++)
{
ocrs.push_back(OCRTesseract::create());
}
Mat transition_p;
string filename = "OCRHMM_transitions_table.xml";
FileStorage fs(filename, FileStorage::READ);
fs["transition_probabilities"] >> transition_p;
fs.release();
Mat emission_p = Mat::eye(62,62,CV_64FC1);
string voc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
vector< Ptr<OCRHMMDecoder> > decoders;
for (int o=0; o<num_ocrs; o++)
{
decoders.push_back(OCRHMMDecoder::create(loadOCRHMMClassifierNM("OCRHMM_knn_model_data.xml.gz"),
voc, transition_p, emission_p));
}
cout << " Done!" << endl;
while ( true )
{
double t_all = (double)getTickCount();
if (downsize)
resize(frame,frame,Size(320,240),0,0,INTER_LINEAR_EXACT);
/*Text Detection*/
cvtColor(frame,gray,COLOR_BGR2GRAY);
// Extract channels to be processed individually
channels.clear();
channels.push_back(gray);
channels.push_back(255-gray);
regions[0].clear();
regions[1].clear();
switch (REGION_TYPE)
{
case 0: // ERStats
parallel_for_(cv::Range(0, (int)channels.size()), Parallel_extractCSER(channels, regions, er_filters1, er_filters2));
break;
case 1: // MSER
vector<vector<Point> > contours;
vector<Rect> bboxes;
Ptr<MSER> mser = MSER::create(21, (int)(0.00002*gray.cols*gray.rows), (int)(0.05*gray.cols*gray.rows), 1, 0.7);
mser->detectRegions(gray, contours, bboxes);
//Convert the output of MSER to suitable input for the grouping/recognition algorithms
if (contours.size() > 0)
MSERsToERStats(gray, contours, regions);
break;
}
// Detect character groups
vector< vector<Vec2i> > nm_region_groups;
vector<Rect> nm_boxes;
switch (GROUPING_ALGORITHM)
{
case 0: // exhaustive_search
erGrouping(frame, channels, regions, nm_region_groups, nm_boxes, ERGROUPING_ORIENTATION_HORIZ);
break;
case 1: //multioriented
erGrouping(frame, channels, regions, nm_region_groups, nm_boxes, ERGROUPING_ORIENTATION_ANY, "./trained_classifier_erGrouping.xml", 0.5);
break;
}
/*Text Recognition (OCR)*/
int bottom_bar_height= out_img.rows/7 ;
copyMakeBorder(frame, out_img, 0, bottom_bar_height, 0, 0, BORDER_CONSTANT, Scalar(150, 150, 150));
float scale_font = (float)(bottom_bar_height /85.0);
vector<string> words_detection;
float min_confidence1 = 0.f, min_confidence2 = 0.f;
if (RECOGNITION == 0)
{
min_confidence1 = 51.f;
min_confidence2 = 60.f;
}
vector<Mat> detections;
for (int i=0; i<(int)nm_boxes.size(); i++)
{
rectangle(out_img, nm_boxes[i].tl(), nm_boxes[i].br(), Scalar(255,255,0),3);
Mat group_img = Mat::zeros(frame.rows+2, frame.cols+2, CV_8UC1);
er_draw(channels, regions, nm_region_groups[i], group_img);
group_img(nm_boxes[i]).copyTo(group_img);
copyMakeBorder(group_img,group_img,15,15,15,15,BORDER_CONSTANT,Scalar(0));
detections.push_back(group_img);
}
vector<string> outputs((int)detections.size());
vector< vector<Rect> > boxes((int)detections.size());
vector< vector<string> > words((int)detections.size());
vector< vector<float> > confidences((int)detections.size());
// parallel process detections in batches of ocrs.size() (== num_ocrs)
for (int i=0; i<(int)detections.size(); i=i+(int)num_ocrs)
{
Range r;
if (i+(int)num_ocrs <= (int)detections.size())
r = Range(i,i+(int)num_ocrs);
else
r = Range(i,(int)detections.size());
switch(RECOGNITION)
{
case 0: // Tesseract
parallel_for_(r, Parallel_OCR<OCRTesseract>(detections, outputs, boxes, words, confidences, ocrs));
break;
case 1: // NM_chain_features + KNN
parallel_for_(r, Parallel_OCR<OCRHMMDecoder>(detections, outputs, boxes, words, confidences, decoders));
break;
}
}
for (int i=0; i<(int)detections.size(); i++)
{
outputs[i].erase(remove(outputs[i].begin(), outputs[i].end(), '\n'), outputs[i].end());
//cout << "OCR output = \"" << outputs[i] << "\" length = " << outputs[i].size() << endl;
if (outputs[i].size() < 3)
continue;
for (int j=0; j<(int)boxes[i].size(); j++)
{
boxes[i][j].x += nm_boxes[i].x-15;
boxes[i][j].y += nm_boxes[i].y-15;
//cout << " word = " << words[j] << "\t confidence = " << confidences[j] << endl;
if ((words[i][j].size() < 2) || (confidences[i][j] < min_confidence1) ||
((words[i][j].size()==2) && (words[i][j][0] == words[i][j][1])) ||
((words[i][j].size()< 4) && (confidences[i][j] < min_confidence2)) ||
isRepetitive(words[i][j]))
continue;
words_detection.push_back(words[i][j]);
rectangle(out_img, boxes[i][j].tl(), boxes[i][j].br(), Scalar(255,0,255),3);
Size word_size = getTextSize(words[i][j], FONT_HERSHEY_SIMPLEX, (double)scale_font, (int)(3*scale_font), NULL);
rectangle(out_img, boxes[i][j].tl()-Point(3,word_size.height+3), boxes[i][j].tl()+Point(word_size.width,0), Scalar(255,0,255),-1);
putText(out_img, words[i][j], boxes[i][j].tl()-Point(1,1), FONT_HERSHEY_SIMPLEX, scale_font, Scalar(255,255,255),(int)(3*scale_font));
}
}
t_all = ((double)getTickCount() - t_all)*1000/getTickFrequency();
int text_thickness = 1+(out_img.rows/500);
string fps_info = format("%2.1f Fps. %dx%d", (float)(1000 / t_all), frame.cols, frame.rows);
putText(out_img, fps_info, Point( 10,out_img.rows-5 ), FONT_HERSHEY_DUPLEX, scale_font, Scalar(255,0,0), text_thickness);
putText(out_img, region_types_str[REGION_TYPE], Point((int)(out_img.cols*0.5), out_img.rows - (int)(bottom_bar_height / 1.5)), FONT_HERSHEY_DUPLEX, scale_font, Scalar(255,0,0), text_thickness);
putText(out_img, grouping_algorithms_str[GROUPING_ALGORITHM], Point((int)(out_img.cols*0.5),out_img.rows-((int)(bottom_bar_height /3)+4) ), FONT_HERSHEY_DUPLEX, scale_font, Scalar(255,0,0), text_thickness);
putText(out_img, recognitions_str[RECOGNITION], Point((int)(out_img.cols*0.5),out_img.rows-5 ), FONT_HERSHEY_DUPLEX, scale_font, Scalar(255,0,0), text_thickness);
imshow("recognition", out_img);
if ((image_file_name == "") && !cap.read(frame))
{
cout << "Capturing ended! press any key to exit." << endl;
waitKey();
return 0;
}
int key = waitKey(30); //wait for a key press
switch (key)
{
case 27: //ESC
cout << "ESC key pressed and exited." << endl;
return 0;
case 32: //SPACE
imwrite("recognition_alt.jpg", out_img);
break;
case 103: //'g'
GROUPING_ALGORITHM = (GROUPING_ALGORITHM+1)%2;
cout << "Grouping switched to " << grouping_algorithms_str[GROUPING_ALGORITHM] << endl;
break;
case 111: //'o'
RECOGNITION = (RECOGNITION+1)%2;
cout << "OCR switched to " << recognitions_str[RECOGNITION] << endl;
break;
case 114: //'r'
REGION_TYPE = (REGION_TYPE+1)%2;
cout << "Regions switched to " << region_types_str[REGION_TYPE] << endl;
break;
case 115: //'s'
downsize = !downsize;
if (!image.empty())
{
frame = image.clone();
}
break;
default:
break;
}
}
return 0;
}
bool isRepetitive(const string& s)
{
int count = 0;
int count2 = 0;
int count3 = 0;
int first=(int)s[0];
int last=(int)s[(int)s.size()-1];
for (int i=0; i<(int)s.size(); i++)
{
if ((s[i] == 'i') ||
(s[i] == 'l') ||
(s[i] == 'I'))
count++;
if((int)s[i]==first)
count2++;
if((int)s[i]==last)
count3++;
}
if ((count > ((int)s.size()+1)/2) || (count2 == (int)s.size()) || (count3 > ((int)s.size()*2)/3))
{
return true;
}
return false;
}
void er_draw(vector<Mat> &channels, vector<vector<ERStat> > &regions, vector<Vec2i> group, Mat& segmentation)
{
for (int r=0; r<(int)group.size(); r++)
{
ERStat er = regions[group[r][0]][group[r][1]];
if (er.parent != NULL) // deprecate the root region
{
int newMaskVal = 255;
int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
floodFill(channels[group[r][0]],segmentation,Point(er.pixel%channels[group[r][0]].cols,er.pixel/channels[group[r][0]].cols),
Scalar(255),0,Scalar(er.level),Scalar(0),flags);
}
}
}
File diff suppressed because it is too large Load Diff
+791
View File
@@ -0,0 +1,791 @@
/*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 "opencv2/imgproc.hpp"
#include "opencv2/ml.hpp"
#include <iostream>
#include <fstream>
#include <set>
namespace cv
{
namespace text
{
using namespace std;
using namespace cv::ml;
/* OCR BeamSearch Decoder */
void OCRBeamSearchDecoder::run(Mat& image, string& output_text, vector<Rect>* component_rects,
vector<string>* component_texts, vector<float>* component_confidences,
int component_level)
{
CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC3) );
CV_Assert( (component_level == OCR_LEVEL_TEXTLINE) || (component_level == OCR_LEVEL_WORD) );
output_text.clear();
if (component_rects != NULL)
component_rects->clear();
if (component_texts != NULL)
component_texts->clear();
if (component_confidences != NULL)
component_confidences->clear();
}
void OCRBeamSearchDecoder::run(Mat& image, Mat& mask, string& output_text, vector<Rect>* component_rects,
vector<string>* component_texts, vector<float>* component_confidences,
int component_level)
{
CV_Assert(mask.type() == CV_8UC1);
CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC3) );
CV_Assert( (component_level == OCR_LEVEL_TEXTLINE) || (component_level == OCR_LEVEL_WORD) );
output_text.clear();
if (component_rects != NULL)
component_rects->clear();
if (component_texts != NULL)
component_texts->clear();
if (component_confidences != NULL)
component_confidences->clear();
}
CV_WRAP String OCRBeamSearchDecoder::run(InputArray image, int min_confidence, int component_level)
{
std::string output1;
std::string output2;
vector<string> component_texts;
vector<float> component_confidences;
Mat image_m = image.getMat();
run(image_m, output1, NULL, &component_texts, &component_confidences, component_level);
for(unsigned int i = 0; i < component_texts.size(); i++)
{
//cout << "confidence: " << component_confidences[i] << " text:" << component_texts[i] << endl;
if(component_confidences[i] > min_confidence)
{
output2 += component_texts[i];
}
}
return String(output2);
}
CV_WRAP String OCRBeamSearchDecoder::run(InputArray image, InputArray mask, int min_confidence, int component_level)
{
std::string output1;
std::string output2;
vector<string> component_texts;
vector<float> component_confidences;
Mat image_m = image.getMat();
Mat mask_m = mask.getMat();
run(image_m, mask_m, output1, NULL, &component_texts, &component_confidences, component_level);
for(unsigned int i = 0; i < component_texts.size(); i++)
{
//cout << "confidence: " << component_confidences[i] << " text:" << component_texts[i] << endl;
if(component_confidences[i] > min_confidence)
{
output2 += component_texts[i];
}
}
return String(output2);
}
void OCRBeamSearchDecoder::ClassifierCallback::eval( InputArray image, vector< vector<double> >& recognition_probabilities, vector<int>& oversegmentation)
{
CV_Assert(( image.getMat().type() == CV_8UC3 ) || ( image.getMat().type() == CV_8UC1 ));
if (!recognition_probabilities.empty())
{
for (size_t i=0; i<recognition_probabilities.size(); i++)
recognition_probabilities[i].clear();
}
recognition_probabilities.clear();
oversegmentation.clear();
}
struct beamSearch_node {
double score;
vector<int> segmentation;
bool expanded;
// TODO calculating score of its child would be much faster if we store the last column
// of their "root" path.
};
bool beam_sort_function ( beamSearch_node a, beamSearch_node b );
bool beam_sort_function ( beamSearch_node a, beamSearch_node b )
{
return (a.score > b.score);
}
class OCRBeamSearchDecoderImpl CV_FINAL : public OCRBeamSearchDecoder
{
public:
//Default constructor
OCRBeamSearchDecoderImpl( Ptr<OCRBeamSearchDecoder::ClassifierCallback> _classifier,
const string& _vocabulary,
InputArray transition_probabilities_table,
InputArray emission_probabilities_table,
decoder_mode _mode,
int _beam_size)
{
classifier = _classifier;
step_size = classifier->getStepSize();
win_size = classifier->getWindowSize();
emission_p = emission_probabilities_table.getMat();
vocabulary = _vocabulary;
mode = _mode;
beam_size = _beam_size;
transition_probabilities_table.getMat().copyTo(transition_p);
for (int i=0; i<transition_p.rows; i++)
{
for (int j=0; j<transition_p.cols; j++)
{
if (transition_p.at<double>(i,j) == 0)
transition_p.at<double>(i,j) = -DBL_MAX;
else
transition_p.at<double>(i,j) = log(transition_p.at<double>(i,j));
}
}
}
~OCRBeamSearchDecoderImpl() CV_OVERRIDE
{
}
void run( Mat& src,
Mat& mask,
string& out_sequence,
vector<Rect>* component_rects,
vector<string>* component_texts,
vector<float>* component_confidences,
int component_level) CV_OVERRIDE
{
CV_Assert(mask.type() == CV_8UC1);
//nothing to do with a mask here
run( src, out_sequence, component_rects, component_texts, component_confidences,
component_level);
}
void run( Mat& src,
string& out_sequence,
vector<Rect>* component_rects,
vector<string>* component_texts,
vector<float>* component_confidences,
int component_level) CV_OVERRIDE
{
CV_Assert( (src.type() == CV_8UC1) || (src.type() == CV_8UC3) );
CV_Assert( (src.cols > 0) && (src.rows > 0) );
CV_Assert( component_level == OCR_LEVEL_WORD );
out_sequence.clear();
if (component_rects != NULL)
component_rects->clear();
if (component_texts != NULL)
component_texts->clear();
if (component_confidences != NULL)
component_confidences->clear();
if(src.type() == CV_8UC3)
{
cvtColor(src,src,COLOR_RGB2GRAY);
}
// TODO if input is a text line (not a word) we may need to split into words here!
// do sliding window classification along a cropped word image
classifier->eval(src, recognition_probabilities, oversegmentation);
// if the number of oversegmentation points found is less than 2 we can not do nothing!!
if (oversegmentation.size() < 2) return;
//NMS of recognitions
double last_best_p = 0;
int last_best_idx = -1;
for (size_t i=0; i<recognition_probabilities.size(); )
{
double best_p = 0;
int best_idx = -1;
for (size_t j=0; j<recognition_probabilities[i].size(); j++)
{
if (recognition_probabilities[i][j] > best_p)
{
best_p = recognition_probabilities[i][j];
best_idx = (int)j;
}
}
if ((i>0) && (best_idx == last_best_idx)
&& (oversegmentation[i]*step_size < oversegmentation[i-1]*step_size + win_size) )
{
if (last_best_p > best_p)
{
//remove i'th elements and do not increment i
recognition_probabilities.erase (recognition_probabilities.begin()+i);
oversegmentation.erase (oversegmentation.begin()+i);
continue;
} else {
//remove (i-1)'th elements and do not increment i
recognition_probabilities.erase (recognition_probabilities.begin()+i-1);
oversegmentation.erase (oversegmentation.begin()+i-1);
last_best_idx = best_idx;
last_best_p = best_p;
continue;
}
}
last_best_idx = best_idx;
last_best_p = best_p;
i++;
}
/*Now we go with the beam search algorithm to optimize the recognition score*/
//convert probabilities to log probabilities
for (size_t i=0; i<recognition_probabilities.size(); i++)
{
for (size_t j=0; j<recognition_probabilities[i].size(); j++)
{
if (recognition_probabilities[i][j] == 0)
recognition_probabilities[i][j] = -DBL_MAX;
else
recognition_probabilities[i][j] = log(recognition_probabilities[i][j]);
}
}
// initialize the beam with all possible character's pairs
int generated_chids = 0;
for (size_t i=0; i<recognition_probabilities.size()-1; i++)
{
for (size_t j=i+1; j<recognition_probabilities.size(); j++)
{
beamSearch_node node;
node.segmentation.push_back((int)i);
node.segmentation.push_back((int)j);
node.score = score_segmentation(node.segmentation, out_sequence);
vector< vector<int> > childs = generate_childs( node.segmentation );
node.expanded = true;
beam.push_back( node );
if (!childs.empty())
update_beam( childs );
generated_chids += (int)childs.size();
}
}
while (generated_chids != 0)
{
generated_chids = 0;
for (size_t i=0; i<beam.size(); i++)
{
vector< vector<int> > childs;
if (!beam[i].expanded)
{
childs = generate_childs( beam[i].segmentation );
beam[i].expanded = true;
}
if (!childs.empty())
update_beam( childs );
generated_chids += (int)childs.size();
}
}
// Done! Get the best prediction found into out_sequence
double lp = score_segmentation( beam[0].segmentation, out_sequence );
// fill other (dummy) output parameters
if (component_rects != NULL)
component_rects->push_back(Rect(0,0,src.cols,src.rows));
if (component_texts != NULL)
component_texts->push_back(out_sequence);
if (component_confidences != NULL)
component_confidences->push_back((float)exp(lp));
return;
}
private:
int win_size;
int step_size;
vector< beamSearch_node > beam;
vector< vector<double> > recognition_probabilities;
vector<int> oversegmentation;
vector< vector<int> > generate_childs( vector<int> &segmentation )
{
vector< vector<int> > childs;
for (size_t i=segmentation[segmentation.size()-1]+1; i<oversegmentation.size(); i++)
{
int seg_point = (int)i;
if (find(segmentation.begin(), segmentation.end(), seg_point) == segmentation.end())
{
vector<int> child = segmentation;
child.push_back(seg_point);
childs.push_back(child);
}
}
return childs;
}
void update_beam ( vector< vector<int> > &childs )
{
string out_sequence;
double min_score = -DBL_MAX; //min score value to be part of the beam
if ((int)beam.size() >= beam_size)
min_score = beam[beam_size-1].score; //last element has the lowest score
for (size_t i=0; i<childs.size(); i++)
{
double score = score_segmentation(childs[i], out_sequence);
if (score > min_score)
{
beamSearch_node node;
node.score = score;
node.segmentation = childs[i];
node.expanded = false;
beam.push_back(node);
sort(beam.begin(),beam.end(),beam_sort_function);
if ((int)beam.size() > beam_size)
{
beam.erase(beam.begin()+beam_size,beam.end());
min_score = beam[beam.size()-1].score;
}
}
}
}
double score_segmentation( vector<int> &segmentation, string& outstring )
{
// Score Heuristics:
// No need to use Viterbi to know a given segmentation is bad
// e.g.: in some cases we discard a segmentation because it includes a very large character
// in other cases we do it because the overlapping between two chars is too large
// TODO Add more heuristics (e.g. penalize large inter-character variance)
Mat interdist ((int)segmentation.size()-1, 1, CV_32F, 1);
for (size_t i=0; i<segmentation.size()-1; i++)
{
interdist.at<float>((int)i,0) = (float)oversegmentation[segmentation[(int)i+1]]*step_size
- (float)oversegmentation[segmentation[(int)i]]*step_size;
if ((float)interdist.at<float>((int)i,0)/win_size > 2.25) // TODO explain how did you set this thrs
{
return -DBL_MAX;
}
if ((float)interdist.at<float>((int)i,0)/win_size < 0.15) // TODO explain how did you set this thrs
{
return -DBL_MAX;
}
}
Scalar m, std;
meanStdDev(interdist, m, std);
//double interdist_std = std[0];
//TODO Extracting start probs from lexicon (if we have it) may boost accuracy!
vector<double> start_p(vocabulary.size());
for (int i=0; i<(int)vocabulary.size(); i++)
start_p[i] = log(1.0/vocabulary.size());
Mat V = Mat::ones((int)segmentation.size(),(int)vocabulary.size(),CV_64FC1);
V = V * -DBL_MAX;
vector<string> path(vocabulary.size());
// Initialize base cases (t == 0)
for (int i=0; i<(int)vocabulary.size(); i++)
{
V.at<double>(0,i) = start_p[i] + recognition_probabilities[segmentation[0]][i];
path[i] = vocabulary.at(i);
}
// Run Viterbi for t > 0
for (int t=1; t<(int)segmentation.size(); t++)
{
vector<string> newpath(vocabulary.size());
for (int i=0; i<(int)vocabulary.size(); i++)
{
double max_prob = -DBL_MAX;
int best_idx = 0;
for (int j=0; j<(int)vocabulary.size(); j++)
{
double prob = V.at<double>(t-1,j) + transition_p.at<double>(j,i) + recognition_probabilities[segmentation[t]][i];
if ( prob > max_prob)
{
max_prob = prob;
best_idx = j;
}
}
V.at<double>(t,i) = max_prob;
newpath[i] = path[best_idx] + vocabulary.at(i);
}
// Don't need to remember the old paths
path.swap(newpath);
}
double max_prob = -DBL_MAX;
int best_idx = 0;
for (int i=0; i<(int)vocabulary.size(); i++)
{
double prob = V.at<double>((int)segmentation.size()-1,i);
if ( prob > max_prob)
{
max_prob = prob;
best_idx = i;
}
}
outstring = path[best_idx];
return (max_prob / (segmentation.size()-1));
}
};
Ptr<OCRBeamSearchDecoder> OCRBeamSearchDecoder::create( Ptr<OCRBeamSearchDecoder::ClassifierCallback> _classifier,
const string& _vocabulary,
InputArray transition_p,
InputArray emission_p,
decoder_mode _mode,
int _beam_size)
{
return makePtr<OCRBeamSearchDecoderImpl>(_classifier, _vocabulary, transition_p, emission_p, _mode, _beam_size);
}
Ptr<OCRBeamSearchDecoder> OCRBeamSearchDecoder::create(const String& _filename,
const String& _vocabulary,
InputArray transition_p,
InputArray emission_p,
decoder_mode _mode,
int _beam_size)
{
return makePtr<OCRBeamSearchDecoderImpl>(loadOCRBeamSearchClassifierCNN(_filename), _vocabulary, transition_p, emission_p, (decoder_mode)_mode, _beam_size);
}
class OCRBeamSearchClassifierCNN CV_FINAL : public OCRBeamSearchDecoder::ClassifierCallback
{
public:
//constructor
OCRBeamSearchClassifierCNN(const std::string& filename);
// Destructor
~OCRBeamSearchClassifierCNN() CV_OVERRIDE {}
void eval( InputArray src, vector< vector<double> >& recognition_probabilities, vector<int>& oversegmentation ) CV_OVERRIDE;
int getWindowSize() {return window_size;}
int getStepSize() {return step_size;}
void setStepSize(int _step_size) {step_size = _step_size;}
protected:
void normalizeAndZCA(Mat& patches);
double eval_feature(Mat& feature, double* prob_estimates);
private:
int window_size; // window size
int step_size; // sliding window step
int nr_class; // number of classes
int nr_feature; // number of features
Mat feature_min; // scale range
Mat feature_max;
Mat weights; // Logistic Regression weights
Mat kernels; // CNN kernels
Mat M, P; // ZCA Whitening parameters
int quad_size;
int patch_size;
int num_quads; // extract 25 quads (12x12) from each image
int num_tiles; // extract 25 patches (8x8) from each quad
double alpha; // used in non-linear activation function z = max(0, |D*a| - alpha)
};
OCRBeamSearchClassifierCNN::OCRBeamSearchClassifierCNN (const string& filename)
{
if (ifstream(filename.c_str()))
{
FileStorage fs(filename, FileStorage::READ);
// Load kernels bank and withenning params
fs["kernels"] >> kernels;
fs["M"] >> M;
fs["P"] >> P;
// Load Logistic Regression weights
fs["weights"] >> weights;
// Load feature scaling ranges
fs["feature_min"] >> feature_min;
fs["feature_max"] >> feature_max;
fs.release();
}
else
CV_Error(Error::StsBadArg, "Default classifier data file not found!");
nr_feature = weights.rows;
nr_class = weights.cols;
patch_size = cvRound(sqrt((float)kernels.cols));
window_size = 4*patch_size;
step_size = 4;
quad_size = 12;
num_quads = 25;
num_tiles = 25;
alpha = 0.5; // used in non-linear activation function z = max(0, |D*a| - alpha)
}
void OCRBeamSearchClassifierCNN::eval( InputArray _src, vector< vector<double> >& recognition_probabilities, vector<int>& oversegmentation)
{
CV_Assert(( _src.getMat().type() == CV_8UC3 ) || ( _src.getMat().type() == CV_8UC1 ));
if (!recognition_probabilities.empty())
{
for (size_t i=0; i<recognition_probabilities.size(); i++)
recognition_probabilities[i].clear();
}
recognition_probabilities.clear();
oversegmentation.clear();
Mat src = _src.getMat();
if(src.type() == CV_8UC3)
{
cvtColor(src,src,COLOR_RGB2GRAY);
}
resize(src,src,Size(window_size*src.cols/src.rows,window_size),0,0,INTER_LINEAR_EXACT);
int seg_points = 0;
Mat quad;
Mat tmp;
Mat img;
int sz = src.cols - window_size;
int sz_window_quad = window_size - quad_size;
int sz_half_quad = (int)(quad_size/2-1);
int sz_quad_patch = quad_size - patch_size;
// begin sliding window loop foreach detection window
for (int x_c = 0; x_c <= sz; x_c += step_size)
{
img = src(Rect(Point(x_c,0),Size(window_size,window_size)));
vector< vector<double> > data_pool(9);
int quad_id = 1;
for (int q_x = 0; q_x <= sz_window_quad; q_x += sz_half_quad)
{
for (int q_y = 0; q_y <= sz_window_quad; q_y += sz_half_quad)
{
Rect quad_rect = Rect(q_x,q_y,quad_size,quad_size);
quad = img(quad_rect);
//start sliding window (8x8) in each tile and store the patch as row in data_pool
for (int w_x = 0; w_x <= sz_quad_patch; w_x++)
{
for (int w_y = 0; w_y <= sz_quad_patch; w_y++)
{
quad(Rect(w_x,w_y,patch_size,patch_size)).convertTo(tmp, CV_64F);
tmp = tmp.reshape(0,1);
normalizeAndZCA(tmp);
vector<double> patch;
tmp.copyTo(patch);
if ((quad_id == 1)||(quad_id == 2)||(quad_id == 6)||(quad_id == 7))
data_pool[0].insert(data_pool[0].end(),patch.begin(),patch.end());
if ((quad_id == 2)||(quad_id == 7)||(quad_id == 3)||(quad_id == 8)||(quad_id == 4)||(quad_id == 9))
data_pool[1].insert(data_pool[1].end(),patch.begin(),patch.end());
if ((quad_id == 4)||(quad_id == 9)||(quad_id == 5)||(quad_id == 10))
data_pool[2].insert(data_pool[2].end(),patch.begin(),patch.end());
if ((quad_id == 6)||(quad_id == 11)||(quad_id == 16)||(quad_id == 7)||(quad_id == 12)||(quad_id == 17))
data_pool[3].insert(data_pool[3].end(),patch.begin(),patch.end());
if ((quad_id == 7)||(quad_id == 12)||(quad_id == 17)||(quad_id == 8)||(quad_id == 13)||(quad_id == 18)||(quad_id == 9)||(quad_id == 14)||(quad_id == 19))
data_pool[4].insert(data_pool[4].end(),patch.begin(),patch.end());
if ((quad_id == 9)||(quad_id == 14)||(quad_id == 19)||(quad_id == 10)||(quad_id == 15)||(quad_id == 20))
data_pool[5].insert(data_pool[5].end(),patch.begin(),patch.end());
if ((quad_id == 16)||(quad_id == 21)||(quad_id == 17)||(quad_id == 22))
data_pool[6].insert(data_pool[6].end(),patch.begin(),patch.end());
if ((quad_id == 17)||(quad_id == 22)||(quad_id == 18)||(quad_id == 23)||(quad_id == 19)||(quad_id == 24))
data_pool[7].insert(data_pool[7].end(),patch.begin(),patch.end());
if ((quad_id == 19)||(quad_id == 24)||(quad_id == 20)||(quad_id == 25))
data_pool[8].insert(data_pool[8].end(),patch.begin(),patch.end());
}
}
quad_id++;
}
}
//do dot product of each normalized and whitened patch
//each pool is averaged and this yields a representation of 9xD
Mat feature = Mat::zeros(9,kernels.rows,CV_64FC1);
for (int i=0; i<9; i++)
{
Mat pool = Mat(data_pool[i]);
pool = pool.reshape(0,(int)data_pool[i].size()/kernels.cols);
for (int p=0; p<pool.rows; p++)
{
for (int f=0; f<kernels.rows; f++)
{
feature.row(i).at<double>(0,f) = feature.row(i).at<double>(0,f) + max(0.0,std::abs(pool.row(p).dot(kernels.row(f)))-alpha);
}
}
}
feature = feature.reshape(0,1);
// data must be normalized within the range obtained during training
double lower = -1.0;
double upper = 1.0;
for (int k=0; k<feature.cols; k++)
{
feature.at<double>(0,k) = lower + (upper-lower) *
(feature.at<double>(0,k)-feature_min.at<double>(0,k))/
(feature_max.at<double>(0,k)-feature_min.at<double>(0,k));
}
double *p = new double[nr_class];
double predict_label = eval_feature(feature,p);
if ( (predict_label < 0) || (predict_label > nr_class) )
CV_Error(Error::StsOutOfRange, "OCRBeamSearchClassifierCNN::eval Error: unexpected prediction in eval_feature()");
vector<double> recognition_p(p, p+nr_class);
recognition_probabilities.push_back(recognition_p);
oversegmentation.push_back(seg_points);
seg_points++;
}
}
// normalize for contrast and apply ZCA whitening to a set of image patches
void OCRBeamSearchClassifierCNN::normalizeAndZCA(Mat& patches)
{
//Normalize for contrast
for (int i=0; i<patches.rows; i++)
{
Scalar row_mean, row_std;
meanStdDev(patches.row(i),row_mean,row_std);
row_std[0] = sqrt(pow(row_std[0],2)*patches.cols/(patches.cols-1)+10);
patches.row(i) = (patches.row(i) - row_mean[0]) / row_std[0];
}
//ZCA whitening
if ((M.dims == 0) || (P.dims == 0))
{
Mat CC;
calcCovarMatrix(patches,CC,M,COVAR_NORMAL|COVAR_ROWS|COVAR_SCALE);
CC = CC * patches.rows / (patches.rows-1);
Mat e_val,e_vec;
eigen(CC.t(),e_val,e_vec);
e_vec = e_vec.t();
sqrt(1./(e_val + 0.1), e_val);
Mat V = Mat::zeros(e_vec.rows, e_vec.cols, CV_64FC1);
Mat D = Mat::eye(e_vec.rows, e_vec.cols, CV_64FC1);
for (int i=0; i<e_vec.cols; i++)
{
e_vec.col(e_vec.cols-i-1).copyTo(V.col(i));
D.col(i) = D.col(i) * e_val.at<double>(0,e_val.rows-i-1);
}
P = V * D * V.t();
}
for (int i=0; i<patches.rows; i++)
patches.row(i) = patches.row(i) - M;
patches = patches * P;
}
double OCRBeamSearchClassifierCNN::eval_feature(Mat& feature, double* prob_estimates)
{
for(int i=0;i<nr_class;i++)
prob_estimates[i] = 0;
for(int idx=0; idx<nr_feature; idx++)
for(int i=0;i<nr_class;i++)
prob_estimates[i] += weights.at<float>(idx,i)*feature.at<double>(0,idx); //TODO use vectorized dot product
int dec_max_idx = 0;
for(int i=1;i<nr_class;i++)
{
if(prob_estimates[i] > prob_estimates[dec_max_idx])
dec_max_idx = i;
}
for(int i=0;i<nr_class;i++)
prob_estimates[i]=1/(1+exp(-prob_estimates[i]));
double sum=0;
for(int i=0; i<nr_class; i++)
sum+=prob_estimates[i];
for(int i=0; i<nr_class; i++)
prob_estimates[i]=prob_estimates[i]/sum;
return dec_max_idx;
}
Ptr<OCRBeamSearchDecoder::ClassifierCallback> loadOCRBeamSearchClassifierCNN(const String& filename)
{
return makePtr<OCRBeamSearchClassifierCNN>(std::string(filename));
}
}
}
File diff suppressed because it is too large Load Diff
+106
View File
@@ -0,0 +1,106 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/core.hpp"
#include "opencv2/dnn.hpp"
#include <fstream>
using namespace std;
namespace cv { namespace text {
class OCRHolisticWordRecognizerImpl CV_FINAL : public OCRHolisticWordRecognizer
{
private:
dnn::Net net;
vector<string> words;
public:
OCRHolisticWordRecognizerImpl(const string &archFilename, const string &weightsFilename, const string &wordsFilename)
{
net = dnn::readNet(weightsFilename, archFilename);
std::ifstream in(wordsFilename.c_str());
if (!in)
{
CV_Error(Error::StsError, "Could not read Labels from file");
}
std::string line;
while (std::getline(in, line))
words.push_back(line);
CV_Assert(getClassCount() == words.size());
}
void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL, std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL, int component_level=0) CV_OVERRIDE
{
CV_Assert(component_level==OCR_LEVEL_WORD); //Componnents not applicable for word spotting
double confidence;
output_text = classify(image, confidence);
if(component_rects!=NULL){
component_rects->resize(1);
(*component_rects)[0]=Rect(0,0,image.size().width,image.size().height);
}
if(component_texts!=NULL){
component_texts->resize(1);
(*component_texts)[0] = output_text;
}
if(component_confidences!=NULL){
component_confidences->resize(1);
(*component_confidences)[0] = float(confidence);
}
}
void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL, std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL, int component_level=0) CV_OVERRIDE
{
//Mask is ignored because the CNN operates on a full image
CV_Assert(mask.cols == image.cols && mask.rows == image.rows);
this->run(image, output_text, component_rects, component_texts, component_confidences, component_level);
}
protected:
Size getPerceptiveField() const
{
return Size(100, 32);
}
size_t getClassCount()
{
int id = net.getLayerId("prob");
MatShape inputShape;
inputShape.push_back(1);
inputShape.push_back(1);
inputShape.push_back(getPerceptiveField().height);
inputShape.push_back(getPerceptiveField().width);
vector<MatShape> inShapes, outShapes;
net.getLayerShapes(inputShape, CV_32F, id, inShapes, outShapes);
CV_Assert(outShapes.size() == 1 && outShapes[0].size() == 4);
CV_Assert(outShapes[0][0] == 1 && outShapes[0][2] == 1 && outShapes[0][3] == 1);
return outShapes[0][1];
}
string classify(InputArray image, double & conf)
{
CV_Assert(image.channels() == 1 && image.depth() == CV_8U);
Mat resized;
resize(image, resized, getPerceptiveField(), 0, 0, INTER_LINEAR_EXACT);
Mat blob = dnn::blobFromImage(resized);
net.setInput(blob, "data");
Mat prob = net.forward("prob");
CV_Assert(prob.dims == 4 && !prob.empty() && prob.size[1] == (int)getClassCount());
int idx[4] = {0};
minMaxIdx(prob, 0, &conf, 0, idx);
CV_Assert(0 <= idx[1] && idx[1] < (int)words.size());
return words[idx[1]];
}
};
Ptr<OCRHolisticWordRecognizer> OCRHolisticWordRecognizer::create(const string &archFilename, const string &weightsFilename, const string &wordsFilename)
{
return makePtr<OCRHolisticWordRecognizerImpl>(archFilename, weightsFilename, wordsFilename);
}
}} // cv::text::
+288
View File
@@ -0,0 +1,288 @@
/*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 "opencv2/imgproc.hpp"
#include "opencv2/ml.hpp"
#include <iostream>
#include <fstream>
#include <queue>
#ifdef HAVE_TESSERACT
#if !defined(USE_STD_NAMESPACE)
#define USE_STD_NAMESPACE
#endif
#include <tesseract/baseapi.h>
#include <tesseract/resultiterator.h>
#endif
namespace cv
{
namespace text
{
using namespace std;
void OCRTesseract::run(Mat& image, string& output_text, vector<Rect>* component_rects,
vector<string>* component_texts, vector<float>* component_confidences,
int component_level)
{
CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC3) );
CV_Assert( (component_level == OCR_LEVEL_TEXTLINE) || (component_level == OCR_LEVEL_WORD) );
output_text.clear();
if (component_rects != NULL)
component_rects->clear();
if (component_texts != NULL)
component_texts->clear();
if (component_confidences != NULL)
component_confidences->clear();
}
void OCRTesseract::run(Mat& image, Mat& mask, string& output_text, vector<Rect>* component_rects,
vector<string>* component_texts, vector<float>* component_confidences,
int component_level)
{
CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC3) );
CV_Assert( mask.type() == CV_8UC1 );
CV_Assert( (component_level == OCR_LEVEL_TEXTLINE) || (component_level == OCR_LEVEL_WORD) );
output_text.clear();
if (component_rects != NULL)
component_rects->clear();
if (component_texts != NULL)
component_texts->clear();
if (component_confidences != NULL)
component_confidences->clear();
}
CV_WRAP String OCRTesseract::run(InputArray image, int min_confidence, int component_level)
{
std::string output1;
std::string output2;
vector<string> component_texts;
vector<float> component_confidences;
Mat image_m = image.getMat();
run(image_m, output1, NULL, &component_texts, &component_confidences, component_level);
for(unsigned int i = 0; i < component_texts.size(); i++)
{
// cout << "confidence: " << component_confidences[i] << " text:" << component_texts[i] << endl;
if(component_confidences[i] > min_confidence)
{
output2 += component_texts[i];
}
}
return String(output2);
}
CV_WRAP String OCRTesseract::run(InputArray image, InputArray mask, int min_confidence, int component_level)
{
std::string output1;
std::string output2;
vector<string> component_texts;
vector<float> component_confidences;
Mat image_m = image.getMat();
Mat mask_m = mask.getMat();
run(image_m, mask_m, output1, NULL, &component_texts, &component_confidences, component_level);
for(unsigned int i = 0; i < component_texts.size(); i++)
{
// cout << "confidence: " << component_confidences[i] << " text:" << component_texts[i] << endl;
if(component_confidences[i] > min_confidence)
{
output2 += component_texts[i];
}
}
return String(output2);
}
class OCRTesseractImpl CV_FINAL : public OCRTesseract
{
private:
#ifdef HAVE_TESSERACT
tesseract::TessBaseAPI tess;
#endif
public:
//Default constructor
OCRTesseractImpl(const char* datapath, const char* language, const char* char_whitelist, int oemode, int psmode)
{
#ifdef HAVE_TESSERACT
const char *lang = "eng";
if (language != NULL)
lang = language;
if (tess.Init(datapath, lang, (tesseract::OcrEngineMode)oemode))
{
cout << "OCRTesseract: Could not initialize tesseract." << endl;
throw 1;
}
//cout << "OCRTesseract: tesseract version " << tess.Version() << endl;
tesseract::PageSegMode pagesegmode = (tesseract::PageSegMode)psmode;
tess.SetPageSegMode(pagesegmode);
// tessedit_whitelist default changes from [0-9a-zA-Z] to "".
// See https://github.com/opencv/opencv_contrib/issues/3457
if(char_whitelist != NULL)
tess.SetVariable("tessedit_char_whitelist", char_whitelist);
else
tess.SetVariable("tessedit_char_whitelist", "");
tess.SetVariable("save_best_choices", "T");
#else
cout << "OCRTesseract("<<oemode<<psmode<<"): Tesseract not found." << endl;
if (datapath != NULL)
cout << " " << datapath << endl;
if (language != NULL)
cout << " " << language << endl;
if (char_whitelist != NULL)
cout << " " << char_whitelist << endl;
#endif
}
~OCRTesseractImpl() CV_OVERRIDE
{
#ifdef HAVE_TESSERACT
tess.End();
#endif
}
void run(Mat& image, string& output, vector<Rect>* component_rects=NULL,
vector<string>* component_texts=NULL, vector<float>* component_confidences=NULL,
int component_level=0) CV_OVERRIDE
{
CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC3) );
#ifdef HAVE_TESSERACT
if (component_texts != 0)
component_texts->clear();
if (component_rects != 0)
component_rects->clear();
if (component_confidences != 0)
component_confidences->clear();
tess.SetImage((uchar*)image.data, image.size().width, image.size().height, image.channels(), image.step1());
tess.Recognize(0);
char *outText;
outText = tess.GetUTF8Text();
output = string(outText);
if (outText != NULL)
delete [] outText;
if ( (component_rects != NULL) || (component_texts != NULL) || (component_confidences != NULL) )
{
tesseract::ResultIterator* ri = tess.GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
if (component_level == OCR_LEVEL_TEXTLINE)
level = tesseract::RIL_TEXTLINE;
if (ri != 0) {
do {
const char* word = ri->GetUTF8Text(level);
if (word == NULL)
continue;
float conf = ri->Confidence(level);
int x1, y1, x2, y2;
ri->BoundingBox(level, &x1, &y1, &x2, &y2);
if (component_texts != 0)
component_texts->push_back(string(word));
if (component_rects != 0)
component_rects->push_back(Rect(x1,y1,x2-x1,y2-y1));
if (component_confidences != 0)
component_confidences->push_back(conf);
delete[] word;
} while (ri->Next(level));
delete ri;
}
}
tess.Clear();
#else
cout << "OCRTesseract(" << component_level << image.type() <<"): Tesseract not found." << endl;
output.clear();
if(component_rects)
component_rects->clear();
if(component_texts)
component_texts->clear();
if(component_confidences)
component_confidences->clear();
#endif
}
void run(Mat& image, Mat& mask, string& output, vector<Rect>* component_rects=NULL,
vector<string>* component_texts=NULL, vector<float>* component_confidences=NULL,
int component_level=0) CV_OVERRIDE
{
CV_Assert( mask.type() == CV_8UC1 );
CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC3) );
run( mask, output, component_rects, component_texts, component_confidences, component_level);
}
void setWhiteList(const String& char_whitelist) CV_OVERRIDE
{
#ifdef HAVE_TESSERACT
tess.SetVariable("tessedit_char_whitelist", char_whitelist.c_str());
#else
CV_UNUSED(char_whitelist);
#endif
}
};
Ptr<OCRTesseract> OCRTesseract::create(const char* datapath, const char* language,
const char* char_whitelist, int oem, int psmode)
{
return makePtr<OCRTesseractImpl>(datapath, language, char_whitelist, oem, psmode);
}
}
}
+50
View File
@@ -0,0 +1,50 @@
/*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*/
#ifndef __OPENCV_PRECOMP_H__
#define __OPENCV_PRECOMP_H__
#include "opencv2/text.hpp"
#include "text_config.hpp"
#endif
+98
View File
@@ -0,0 +1,98 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/core.hpp"
#include "opencv2/dnn.hpp"
#include <fstream>
#include <algorithm>
using namespace cv::dnn;
namespace cv
{
namespace text
{
class TextDetectorCNNImpl : public TextDetectorCNN
{
protected:
Net net_;
std::vector<Size> sizes_;
int inputChannelCount_;
void getOutputs(const float* buffer,int nbrTextBoxes,int nCol,
std::vector<Rect>& Bbox, std::vector<float>& confidence, Size inputShape)
{
for(int k = 0; k < nbrTextBoxes; k++)
{
float confidence_ = buffer[k*nCol + 2];
if (confidence_ <= FLT_EPSILON) continue;
float x_min_f = buffer[k*nCol + 3]*inputShape.width;
float y_min_f = buffer[k*nCol + 4]*inputShape.height;
float x_max_f = buffer[k*nCol + 5]*inputShape.width;
float y_max_f = buffer[k*nCol + 6]*inputShape.height;
int x_min = cvRound(std::max(0.f, x_min_f));
int y_min = cvRound(std::max(0.f, y_min_f));
int x_max = std::min(inputShape.width - 1, cvRound(x_max_f));
int y_max = std::min(inputShape.height - 1, cvRound(y_max_f));
if (x_min >= x_max) continue;
if (y_min >= y_max) continue;
int wd = x_max - x_min;
int ht = y_max - y_min;
Bbox.push_back(Rect(x_min, y_min, wd, ht));
confidence.push_back(confidence_);
}
}
public:
TextDetectorCNNImpl(const String& modelArchFilename, const String& modelWeightsFilename, std::vector<Size> detectionSizes) :
sizes_(detectionSizes)
{
net_ = readNet(modelWeightsFilename, modelArchFilename);
CV_Assert(!net_.empty());
inputChannelCount_ = 3;
}
void detect(InputArray inputImage_, std::vector<Rect>& Bbox, std::vector<float>& confidence) CV_OVERRIDE
{
CV_CheckEQ(inputImage_.channels(), inputChannelCount_, "");
Mat inputImage = inputImage_.getMat();
Bbox.resize(0);
confidence.resize(0);
for(size_t i = 0; i < sizes_.size(); i++)
{
Size inputGeometry = sizes_[i];
net_.setInput(blobFromImage(inputImage, 1, inputGeometry, Scalar(123, 117, 104), false, false), "data");
Mat outputNet = net_.forward();
int nbrTextBoxes = outputNet.size[2];
int nCol = outputNet.size[3];
int outputChannelCount = outputNet.size[1];
CV_CheckEQ(outputChannelCount, 1, "");
getOutputs((float*)(outputNet.data), nbrTextBoxes, nCol, Bbox, confidence, inputImage.size());
}
}
};
Ptr<TextDetectorCNN> TextDetectorCNN::create(const String &modelArchFilename, const String &modelWeightsFilename, std::vector<Size> detectionSizes)
{
return makePtr<TextDetectorCNNImpl>(modelArchFilename, modelWeightsFilename, detectionSizes);
}
Ptr<TextDetectorCNN> TextDetectorCNN::create(const String &modelArchFilename, const String &modelWeightsFilename)
{
return create(modelArchFilename, modelWeightsFilename, std::vector<Size>(1, Size(300, 300)));
}
} //namespace text
} //namespace cv
+863
View File
@@ -0,0 +1,863 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include <unordered_map>
#include <limits>
#include <stack>
using namespace std;
namespace cv {
namespace text {
namespace {
struct SWTPoint {
int x;
int y;
float SWT;
};
struct Ray {
SWTPoint p;
SWTPoint q;
std::vector<SWTPoint> points;
};
struct Component {
SWTPoint BB_pointP;
SWTPoint BB_pointQ;
float cx;
float cy;
float median;
float mean;
int length, width;
std::vector<SWTPoint> points;
};
struct ComponentAttr {
float mean, variance, median;
int xmin, ymin;
int xmax, ymax;
float length, width;
};
struct ChannelAverage {
float Red, Green, Blue;
};
struct Direction {
float x, y;
};
struct ChainedComponent {
int chainIndexA;
int chainIndexB;
std::vector<int> componentIndices;
float chainDist;
Direction dir;
bool merged;
};
const Scalar BLUE (255, 0, 0);
const Scalar GREEN(0, 255, 0);
const Scalar RED (0, 0, 255);
void SWTFirstPass (const Mat& edgeImage, const Mat& gradientX, const Mat& gradientY, bool dark_on_light, Mat & SWTImage, std::vector<Ray> & rays);
void SWTSecondPass (Mat & SWTImage, std::vector<Ray> & rays);
void normalizeAndScale (const Mat& SWTImage, Mat& output);
std::vector<std::vector<SWTPoint>> getComponents (const Mat& SWTImage);
ComponentAttr getAttributes(const vector<SWTPoint>& component, const Mat& SWTImage);
void renderComponents (const Mat& SWTImage, const std::vector<Component>& components, Mat& output);
std::vector<Component> filterComponents(const Mat& SWTImage, const std::vector<std::vector<SWTPoint>>& components, bool skipChecks);
void renderComponentBBs (const std::vector<Component>& components, Mat& output);
vector<cv::Rect> findValidChains(const Mat& input_image, const Mat& SWTImage, const std::vector<Component>& components, OutputArray output, std::vector<cv::Rect> & chainedTextRegions);
vector<cv::Rect> getComponentBBs (const std::vector<Component>& components);
bool chainSortDist (const ChainedComponent& Chainl, const ChainedComponent& Chainr);
bool chainSortLength (const ChainedComponent& Chainl, const ChainedComponent& Chainr);
// A utility function to add an edge in an
// undirected graph.
static inline
void addEdge(std::vector< std::vector<int> >& adj, int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
static
void DFSUtil(int v, std::vector<bool> & visited, std::vector< std::vector<int> >& adj, int label, std::vector<int> &component_id)
{
stack<int> s;
s.push(v);
while(!s.empty()){
v = s.top();
s.pop();
if(!visited[v])
{
// Mark the current node as visited and label it as belonging to the current component
visited[v] = true;
component_id[v] = label;
// Recur for all the vertices
// adjacent to this vertex
for (size_t i = 0; i < adj[v].size(); i++) {
int neighbour = adj[v][i];
if(!visited[neighbour])
{
s.push(neighbour);
}
}
}
}
}
static
int connected_components(std::vector< std::vector<int> >& adj, std::vector<int> &component_id, int num_vertices)
{
std::vector<bool> visited(num_vertices, false);
int label = 0;
for (int v=0; v<num_vertices; v++)
{
if (visited[v] == false)
{
DFSUtil(v, visited, adj, label, component_id);
label++;
}
}
return label;
}
void SWTFirstPass(const Mat& edgeImage, const Mat& gradientX, const Mat& gradientY, bool dark_on_light, Mat & SWTImage, std::vector<Ray> & rays)
{
SWTImage.setTo(Scalar::all(-1));
for(int row = 0; row < edgeImage.rows; row++ ){
for ( int col = 0; col < edgeImage.cols; col++ ){
uchar canny = edgeImage.at<uchar>(row, col);
if (canny <= 0) continue;
float dx = gradientX.at<float>(row, col);
float dy = gradientY.at<float>(row, col);
float mag = sqrt(dx * dx + dy * dy);
dx = dx / mag;
dy = dy / mag;
if (dark_on_light){
dx = -dx;
dy = -dy;
}
Ray ray;
SWTPoint p;
p.x = col;
p.y = row;
ray.p = p;
std::vector<SWTPoint> points;
points.push_back(p);
float curPosX = (float) col + (float) 0.5;
float curPosY = (float) row + (float) 0.5;
int curPixX = col;
int curPixY = row;
float inc = (float) 0.05;
while (true) {
curPosX += inc * dx;
curPosY += inc * dy;
if ((int)(floor(curPosX)) != curPixX || (int)(floor(curPosY)) != curPixY) {
curPixX = (int)(floor(curPosX));
curPixY = (int)(floor(curPosY));
if (curPixX < 0 || (curPixX >= SWTImage.cols) || curPixY < 0 || (curPixY >= SWTImage.rows)) {
break;
}
SWTPoint pt;
pt.x = curPixX;
pt.y = curPixY;
points.push_back(pt);
if (edgeImage.at<uchar>(curPixY, curPixX) > 0) {
ray.q = pt;
float G_xt = gradientX.at<float>(curPixY,curPixX);
float G_yt = gradientY.at<float>(curPixY,curPixX);
mag = sqrt( (G_xt * G_xt) + (G_yt * G_yt) );
G_xt = G_xt / mag;
G_yt = G_yt / mag;
if (dark_on_light){
G_xt = -G_xt;
G_yt = -G_yt;
}
if (acos(dx * -G_xt + dy * -G_yt) < CV_PI/2.0 ) {
float length = sqrt( ((float)ray.q.x - (float)ray.p.x)*((float)ray.q.x - (float)ray.p.x) + ((float)ray.q.y - (float)ray.p.y)*((float)ray.q.y - (float)ray.p.y));
for (std::vector<SWTPoint>::iterator pit = points.begin(); pit != points.end(); pit++) {
if (SWTImage.at<float>(pit->y, pit->x) < 0) {
SWTImage.at<float>(pit->y, pit->x) = length;
} else {
SWTImage.at<float>(pit->y, pit->x) = std::min(length, SWTImage.at<float>(pit->y, pit->x));
}
}
ray.points = points;
rays.push_back(ray);
}
break;
}
}
}
}
}
}
static inline
bool sortBySWT(const SWTPoint &lhs, const SWTPoint &rhs)
{
return lhs.SWT < rhs.SWT;
}
void SWTSecondPass (Mat & SWTImage, std::vector<Ray> & rays) {
for (std::vector<Ray>::iterator rit = rays.begin(); rit != rays.end(); rit++) {
for (std::vector<SWTPoint>::iterator pit = rit->points.begin(); pit != rit->points.end(); pit++) {
pit->SWT = SWTImage.at<float>(pit->y, pit->x);
}
std::sort(rit->points.begin(), rit->points.end(), sortBySWT);
float median = (rit -> points[rit -> points.size()/2]).SWT;
for (std::vector<SWTPoint>::iterator pit = rit->points.begin(); pit != rit->points.end(); pit++) {
SWTImage.at<float>(pit->y, pit->x) = std::min(pit->SWT, median);
}
}
}
void normalizeAndScale (const Mat& SWTImage, Mat& output) {
CV_CheckTypeEQ(SWTImage.type(), CV_32FC1, "");
CV_CheckTypeEQ(output.type(), CV_8UC1, "");
Mat outputTemp(output.size(), CV_32FC1);
float maxSWT = 0;
float minSWT = (float) FLT_MAX;
for(int row = 0; row < SWTImage.rows; row++){
for (int col = 0; col < SWTImage.cols; col++){
float val = SWTImage.at<float>(row, col);
if (val < 0)
continue;
maxSWT = std::max(val, maxSWT);
minSWT = std::min(val, minSWT);
}
}
float amplitude = maxSWT - minSWT;
for(int row = 0; row < SWTImage.rows; row++){
for (int col = 0; col < SWTImage.cols; col++){
float val = SWTImage.at<float>(row, col);
if (val < 0) {
outputTemp.at<float>(row, col) = 1;
}
else {
outputTemp.at<float>(row, col) = (val - minSWT) / amplitude;
}
}
}
outputTemp.convertTo(output, CV_8UC1, 255);
}
std::vector<std::vector<SWTPoint>> getComponents (const Mat& SWTImage) {
std::unordered_map<int, int> Pix2Node;
std::unordered_map<int, SWTPoint> Node2Pix;
int num_vertices = 0;
for(int row = 0; row < SWTImage.rows; row++){
for (int col = 0; col < SWTImage.cols; col++){
float val = SWTImage.at<float>(row, col);
if (val < 0) {
continue;
}
else {
Pix2Node[row * SWTImage.cols + col] = num_vertices;
SWTPoint p;
p.x = col;
p.y = row;
Node2Pix[num_vertices] = p;
num_vertices++;
}
}
}
std::vector< vector<int> > graph(num_vertices);
for(int row = 0; row < SWTImage.rows; row++){
for (int col = 0; col < SWTImage.cols; col++){
float val = SWTImage.at<float>(row, col);
if (val < 0) {
continue;
}
else {
int currentNode = Pix2Node[row * SWTImage.cols + col];
if (col+1 < SWTImage.cols) {
float right = SWTImage.at<float>(row, col+1);
if (right > 0 && (val/right <= 3.0 || right/val <= 3.0))
addEdge(graph, currentNode, Pix2Node.at(row * SWTImage.cols + col + 1));
}
if (row+1 < SWTImage.rows) {
if (col+1 < SWTImage.cols) {
float right_down = SWTImage.at<float>(row+1, col+1);
if (right_down > 0 && (val/right_down <= 3.0 || right_down/val <= 3.0))
addEdge(graph, currentNode, Pix2Node.at((row+1) * SWTImage.cols + col + 1));
}
float down = SWTImage.at<float>(row+1, col);
if (down > 0 && (val/down <= 3.0 || down/val <= 3.0))
addEdge(graph, currentNode, Pix2Node.at((row+1) * SWTImage.cols + col));
if (col-1 >= 0) {
float left_down = SWTImage.at<float>(row+1, col-1);
if (left_down > 0 && (val/left_down <= 3.0 || left_down/val <= 3.0))
addEdge(graph, currentNode, Pix2Node.at((row+1) * SWTImage.cols + col - 1));
}
}
}
}
}
std::vector<int> component_id(num_vertices);
int num_comp = connected_components(graph, component_id, num_vertices);
std::vector<std::vector<SWTPoint> > components;
components.reserve(num_comp);
for (int j = 0; j < num_comp; j++) {
std::vector<SWTPoint> tmp;
components.push_back(tmp);
}
for (int j = 0; j < num_vertices; j++) {
SWTPoint p = Node2Pix[j];
components[component_id[j]].push_back(p);
}
return components;
}
ComponentAttr getAttributes(const vector<SWTPoint>& component, const Mat& SWTImage)
{
CV_Assert(!component.empty());
std::vector<float> temp;
temp.reserve(component.size());
ComponentAttr attributes;
attributes.mean = 0;
attributes.variance = 0;
attributes.xmin = 100000;
attributes.ymin = 100000;
attributes.xmax = 0;
attributes.ymax = 0;
float sum = 0;
for (size_t i = 0; i < component.size(); i++) {
const SWTPoint& component_i = component[i];
float val = SWTImage.at<float>(component_i.y, component_i.x);
sum += val;
temp.push_back(val);
attributes.xmin = std::min(attributes.xmin, component_i.x);
attributes.ymin = std::min(attributes.ymin, component_i.y);
attributes.xmax = std::max(attributes.xmax, component_i.x);
attributes.ymax = std::max(attributes.ymax, component_i.y);
}
attributes.mean = sum / ((float)component.size());
for (size_t i = 0; i < component.size(); i++) {
attributes.variance += (temp[i] - attributes.mean) * (temp[i] - attributes.mean);
}
attributes.variance = attributes.variance / ((float)component.size());
std::sort(temp.begin(),temp.end());
attributes.median = temp[temp.size()/2];
attributes.length = (float) (attributes.xmax - attributes.xmin + 1);
attributes.width = (float) (attributes.ymax - attributes.ymin + 1);
return attributes;
}
void renderComponents (const Mat& SWTImage, const std::vector<Component>& components, Mat& output)
{
output.setTo(0);
for (size_t i = 0; i < components.size(); i++) {
const Component& component = components[i];
for (size_t j = 0; j < component.points.size(); j++)
{
const SWTPoint& pt = component.points[j];
output.at<float>(pt.y, pt.x) = SWTImage.at<float>(pt.y, pt.x);
}
}
for(int row = 0; row < output.rows; row++ ){
float* ptr = output.ptr<float>(row);
for ( int col = 0; col < output.cols; col++ ){
if (*ptr == 0) {
*ptr = -1;
}
ptr++;
}
}
float maxVal = 0;
float minVal = (float) FLT_MAX;
for(int row = 0; row < output.rows; row++ ){
const float* ptr = output.ptr<float>(row);
for ( int col = 0; col < output.cols; col++ )
{
float v = ptr[col];
if (v != 0)
{
maxVal = std::max(*ptr, maxVal);
minVal = std::min(*ptr, minVal);
}
}
}
float difference = maxVal - minVal;
for(int row = 0; row < output.rows; row++ ) {
float* ptr = output.ptr<float>(row);
for (int col = 0; col < output.cols; col++)
{
float& v = ptr[col];
if (v < 1) {
v = 1;
} else {
v = (v - minVal)/difference;
}
}
}
}
std::vector<Component> filterComponents(const Mat& SWTImage, const std::vector<std::vector<SWTPoint>>& components, bool skipChecks)
{
const int NUM_THETA = 36; // in 180 (CV_PI)
std::vector<Component> filteredComponents;
filteredComponents.reserve(components.size());
for (size_t i = 0; i < components.size(); i++)
{
const vector<SWTPoint>& component = components[i];
ComponentAttr attributes = getAttributes(component, SWTImage);
if (!skipChecks && attributes.variance > 0.5 * attributes.mean) continue;
if (!skipChecks && attributes.width > 300) continue;
float area = attributes.length * attributes.width;
// compute the rotated bounding box
for (int theta_i = 0; theta_i < (NUM_THETA / 2); theta_i++)
{
float theta = (float)(theta_i * (CV_PI / NUM_THETA));
float
xmin = 1000000,
ymin = 1000000,
xmax = 0,
ymax = 0;
for (size_t j = 0; j < component.size(); j++)
{
// TODO(optimization) use pre-calculated cos/sin table through [theta_i] indexing
float xtemp = component[j].x * cos(theta) + component[j].y * -sin(theta);
float ytemp = component[j].x * sin(theta) + component[j].y * cos(theta);
xmin = std::min(xtemp,xmin);
xmax = std::max(xtemp,xmax);
ymin = std::min(ytemp,ymin);
ymax = std::max(ytemp,ymax);
}
float ltemp = xmax - xmin + 1;
float wtemp = ymax - ymin + 1;
if (ltemp*wtemp < area) {
area = ltemp*wtemp;
attributes.length = ltemp;
attributes.width = wtemp;
}
}
if (!skipChecks && (attributes.length/attributes.width < 1./10. || attributes.length/attributes.width > 10.)) continue;
Component acceptedComponent;
acceptedComponent.length = (int) attributes.length;
acceptedComponent.cx = ((float) (attributes.xmax+attributes.xmin)) / 2;
acceptedComponent.cy = ((float) (attributes.ymax+attributes.ymin)) / 2;
acceptedComponent.BB_pointP.x = attributes.xmin;
acceptedComponent.BB_pointP.y = attributes.ymin;
acceptedComponent.BB_pointQ.x = attributes.xmax;
acceptedComponent.BB_pointQ.y = attributes.ymax;
acceptedComponent.length = attributes.xmax - attributes.xmin + 1;
acceptedComponent.width = attributes.ymax - attributes.ymin + 1;
acceptedComponent.mean = attributes.mean;
acceptedComponent.median = attributes.median;
acceptedComponent.points = component;
filteredComponents.push_back(acceptedComponent);
}
if (!skipChecks){
std::vector<Component> tempComp;
tempComp.reserve(filteredComponents.size());
for (size_t i = 0; i < filteredComponents.size(); i++) {
int count = 0;
Component& compi = filteredComponents[i];
for (size_t j = 0; j < filteredComponents.size(); j++) {
if (i != j) {
Component& compj = filteredComponents[j];
if (compi.BB_pointP.x <= compj.cx && compi.BB_pointQ.x >= compj.cx &&
compi.BB_pointP.y <= compj.cy && compi.BB_pointQ.y >= compj.cy) {
count++;
}
}
}
if (count < 2) {
tempComp.push_back(compi);
}
}
filteredComponents = tempComp;
}
return filteredComponents;
};
void renderComponentBBs(const std::vector<Component>& components, Mat& output)
{
for (size_t i = 0; i < components.size(); i++)
{
const Component& compi = components[i];
Scalar c;
if (i % 3 == 0) {
c = BLUE;
}
else if (i % 3 == 1) {
c = GREEN;
}
else {
c = RED;
}
rectangle(output, Point(compi.BB_pointP.x, compi.BB_pointP.y), Point(compi.BB_pointQ.x, compi.BB_pointQ.y), c, 2);
}
}
vector<cv::Rect> getComponentBBs (const std::vector<Component>& components)
{
vector<cv::Rect> bbs;
for (size_t i = 0; i < components.size(); i++) {
const Component& compi = components[i];
int wd = compi.BB_pointP.x - compi.BB_pointQ.x;
int ht = compi.BB_pointP.y - compi.BB_pointQ.y;
if (wd < 0) wd = -wd;
if (ht < 0) ht = -ht;
bbs.push_back(Rect(min(compi.BB_pointP.x, compi.BB_pointQ.x), min(compi.BB_pointP.y, compi.BB_pointQ.y), wd, ht));
}
return bbs;
}
bool chainSortDist(const ChainedComponent& Chainl, const ChainedComponent& Chainr)
{
return Chainl.chainDist < Chainr.chainDist;
}
bool chainSortLength(const ChainedComponent& Chainl, const ChainedComponent& Chainr)
{
return Chainl.componentIndices.size() < Chainr.componentIndices.size();
}
vector<cv::Rect> findValidChains(const Mat& input_image, const Mat& SWTImage, const std::vector<Component>& components, OutputArray output, std::vector<cv::Rect> & chainedTextRegions)
{
std::vector<ChannelAverage> colorAverages;
colorAverages.reserve(components.size());
for (size_t i = 0; i < components.size(); i++)
{
const Component& compi = components[i];
CV_Assert(!compi.points.empty());
ChannelAverage avgCompi;
avgCompi.Red = 0;
avgCompi.Green = 0;
avgCompi.Blue = 0;
for (size_t j = 0; j < compi.points.size(); j++) {
int x = compi.points[j].x;
int y = compi.points[j].y;
avgCompi.Red += (float) input_image.at<uchar>(y, x*3);
avgCompi.Green += (float) input_image.at<uchar>(y, x*3+1);
avgCompi.Blue += (float) input_image.at<uchar>(y, x*3+2);
}
avgCompi.Red /= compi.points.size();
avgCompi.Green /= compi.points.size();
avgCompi.Blue /= compi.points.size();
colorAverages.push_back(avgCompi);
}
std::vector<ChainedComponent> chains;
for (size_t i = 0; i < components.size(); i++) {
const Component& compi = components[i];
for (size_t j = i+1; j < components.size(); j++) {
const Component& compj = components[j];
if ((compi.median / compj.median <= 2.0 || compj.median / compi.median <= 2.0)
&& (compi.width/compj.width <= 2.0 || compj.width/compi.width <= 2.0)) {
float dist = (compi.cx - compj.cx) * (compi.cx - compj.cx) +
(compi.cy - compj.cy) * (compi.cy - compj.cy);
float colorDist = (colorAverages[i].Red - colorAverages[j].Red) * (colorAverages[i].Red - colorAverages[j].Red) +
(colorAverages[i].Green - colorAverages[j].Green) * (colorAverages[i].Green - colorAverages[j].Green) +
(colorAverages[i].Blue - colorAverages[j].Blue) * (colorAverages[i].Blue - colorAverages[j].Blue);
if (dist < 9*(float)(std::max(std::min(compi.length,compi.width),std::min(compj.length,compj.width)))
*(float)(std::max(std::min(compi.length,compi.width),std::min(compj.length,compj.width))) && colorDist < 1600) {
ChainedComponent chain;
chain.chainIndexA = (int)i;
chain.chainIndexB = (int)j;
vector <int> componentIndices;
componentIndices.push_back((int)i);
componentIndices.push_back((int)j);
chain.componentIndices = componentIndices;
chain.chainDist = dist;
float dx = compi.cx - compj.cx;
float dy = compi.cy - compj.cy;
float mod = sqrt(dx * dx + dy * dy);
dx = dx / mod;
dy = dy / mod;
Direction dir;
dir.x = dx;
dir.y = dy;
chain.dir = dir;
chains.push_back(chain);
}
}
}
}
std::sort(chains.begin(), chains.end(), chainSortDist);
const float alignmentThreshold = (float) CV_PI / 6;
const float alignmentThreshold_cos = cos(alignmentThreshold);
int merges = 1;
while (merges > 0) {
for (size_t i = 0; i < chains.size(); i++) {
chains[i].merged = false;
}
merges = 0;
std::vector<ChainedComponent> chainsAfterMerging;
for (size_t i = 0; i < chains.size(); i++)
{
ChainedComponent& chains_i = chains[i];
for (size_t j = 0; j < chains.size(); j++)
{
ChainedComponent& chains_j = chains[j];
if (i!=j && !chains_i.merged && !chains_j.merged) {
if (chains_i.chainIndexA == chains_j.chainIndexA) {
if (chains_i.dir.x * -chains_j.dir.x + chains_i.dir.y * -chains_j.dir.y > alignmentThreshold_cos) {
chains_i.chainIndexA = chains_j.chainIndexB;
for (std::vector<int>::iterator it = chains_j.componentIndices.begin(); it != chains_j.componentIndices.end(); it++) {
chains_i.componentIndices.push_back(*it);
}
float d_x = components[chains_i.chainIndexA].cx - components[chains_i.chainIndexB].cx;
float d_y = components[chains_i.chainIndexA].cy - components[chains_i.chainIndexB].cy;
chains_i.chainDist = d_x * d_x + d_y * d_y;
float mag = sqrt(d_x*d_x + d_y*d_y);
d_x = d_x / mag;
d_y = d_y / mag;
Direction dir;
dir.x = d_x;
dir.y = d_y;
chains_i.dir = dir;
chains_j.merged = true;
merges++;
}
} else if (chains_i.chainIndexA == chains_j.chainIndexB) {
if (chains_i.dir.x * chains_j.dir.x + chains_i.dir.y * chains_j.dir.y > alignmentThreshold_cos) {
chains_i.chainIndexA = chains_j.chainIndexA;
for (std::vector<int>::iterator it = chains_j.componentIndices.begin(); it != chains_j.componentIndices.end(); it++) {
chains_i.componentIndices.push_back(*it);
}
float d_x = components[chains_i.chainIndexA].cx - components[chains_i.chainIndexB].cx;
float d_y = components[chains_i.chainIndexA].cy - components[chains_i.chainIndexB].cy;
chains_i.chainDist = d_x * d_x + d_y * d_y;
float mag = sqrt(d_x*d_x + d_y*d_y);
d_x = d_x / mag;
d_y = d_y / mag;
Direction dir;
dir.x = d_x;
dir.y = d_y;
chains_i.dir = dir;
chains_j.merged = true;
merges++;
}
} else if (chains_i.chainIndexB == chains_j.chainIndexA) {
if (chains_i.dir.x * chains_j.dir.x + chains_i.dir.y * chains_j.dir.y > alignmentThreshold_cos) {
chains_i.chainIndexB = chains_j.chainIndexB;
for (std::vector<int>::iterator it = chains_j.componentIndices.begin(); it != chains_j.componentIndices.end(); it++) {
chains_i.componentIndices.push_back(*it);
}
float d_x = components[chains_i.chainIndexA].cx - components[chains_i.chainIndexB].cx;
float d_y = components[chains_i.chainIndexA].cy - components[chains_i.chainIndexB].cy;
chains_i.chainDist = d_x * d_x + d_y * d_y;
float mag = sqrt(d_x*d_x + d_y*d_y);
d_x = d_x / mag;
d_y = d_y / mag;
Direction dir;
dir.x = d_x;
dir.y = d_y;
chains_i.dir = dir;
chains_j.merged = true;
merges++;
}
} else if (chains_i.chainIndexB == chains_j.chainIndexB) {
if (chains_i.dir.x * -chains_j.dir.x + chains_i.dir.y * -chains_j.dir.y > alignmentThreshold_cos) {
chains_i.chainIndexB = chains_j.chainIndexA;
for (std::vector<int>::iterator it = chains_j.componentIndices.begin(); it != chains_j.componentIndices.end(); it++) {
chains_i.componentIndices.push_back(*it);
}
float d_x = components[chains_i.chainIndexA].cx - components[chains_i.chainIndexB].cx;
float d_y = components[chains_i.chainIndexA].cy - components[chains_i.chainIndexB].cy;
chains_i.chainDist = d_x * d_x + d_y * d_y;
float mag = sqrt(d_x*d_x + d_y*d_y);
d_x = d_x / mag;
d_y = d_y / mag;
Direction dir;
dir.x = d_x;
dir.y = d_y;
chains_i.dir = dir;
chains_j.merged = true;
merges++;
}
}
}
}
}
std::vector<ChainedComponent> newchains;
for (size_t i = 0; i < chains.size(); i++) {
if (!chains[i].merged) {
newchains.push_back(chains[i]);
}
}
chains = newchains;
std::stable_sort(chains.begin(), chains.end(), chainSortLength);
}
std::vector<ChainedComponent> newchains;
std::vector<std::vector<SWTPoint>> componentsPointsVector;
vector<Component> finalComponents;
finalComponents.reserve(components.size());
std::vector<bool> componentIncluded(components.size(), false);
for (size_t i = 0; i < chains.size(); i++)
{
ChainedComponent& chains_i = chains[i];
if (chains_i.componentIndices.size() >= 3) {
newchains.push_back(chains_i);
int xmin,xmax,ymin,ymax;
xmin = 1000000;
ymin = 1000000;
xmax = 0;
ymax = 0;
for (size_t j = 0; j < chains_i.componentIndices.size(); j++) {
int idx = chains_i.componentIndices[j];
if (componentIncluded[idx])
continue;
componentIncluded[idx] = true;
const Component& acceptedComponent = components[idx];
std::vector<SWTPoint> componentPoints;
for (size_t k = 0; k < acceptedComponent.points.size(); k++)
{
const SWTPoint& pt = acceptedComponent.points[k];
componentPoints.push_back(pt);
xmin = min(xmin, pt.x);
ymin = min(ymin, pt.y);
xmax = max(xmax, pt.x);
ymax = max(ymax, pt.y);
}
componentsPointsVector.push_back(componentPoints);
}
int wd = xmax - xmin;
int ht = ymax - ymin;
chainedTextRegions.push_back(Rect(xmin, ymin, wd, ht));
}
}
finalComponents = filterComponents(SWTImage, componentsPointsVector, true);
chains = newchains;
std::stable_sort(chains.begin(), chains.end(), chainSortLength);
if (output.needed())
{
Mat outTemp(input_image.size(), CV_32FC1);
renderComponents(SWTImage, finalComponents, outTemp);
Mat outTemp_8u;
outTemp.convertTo(outTemp_8u, CV_8UC1, 255.);
cvtColor(outTemp_8u, output, COLOR_GRAY2RGB);
Mat output_ = output.getMat();
renderComponentBBs(finalComponents, output_);
}
return getComponentBBs(finalComponents);
}
} // namespace
void detectTextSWT(InputArray input_, CV_OUT std::vector<cv::Rect>& result, bool dark_on_light, OutputArray & draw /*=noArray()*/, OutputArray & chainBBs /*=noArray()*/)
{
CV_CheckTypeEQ(input_.type(), CV_8UC3, "");
Mat input = input_.getMat();
// Convert to grayscale
Mat grayImage;
cvtColor(input, grayImage, COLOR_BGR2GRAY);
// Create Canny Image
double threshold_low = 175;
double threshold_high = 320;
Mat canny_edge_image;
Canny (grayImage, canny_edge_image, threshold_low, threshold_high, 3);
// Create gradient X, gradient Y
Mat gaussianImage;
grayImage.convertTo(gaussianImage, CV_32FC1, 1./255.);
Mat gradientX;
Mat gradientY;
GaussianBlur(gaussianImage, gaussianImage, Size(5, 5), 0);
Scharr(gaussianImage, gradientX, -1, 1, 0);
Scharr(gaussianImage, gradientY, -1, 0, 1);
GaussianBlur(gradientX, gradientX, Size(3, 3), 0);
GaussianBlur(gradientY, gradientY, Size(3, 3), 0);
std::vector<Ray> rays;
Mat SWTImage( input.size(), CV_32FC1 );
SWTFirstPass (canny_edge_image, gradientX, gradientY, dark_on_light, SWTImage, rays );
SWTSecondPass ( SWTImage, rays );
Mat normalised_image(input.size(), CV_8UC1);
normalizeAndScale(SWTImage, normalised_image);
// Calculate legally connected components from SWT and gradient image.
// return type is a vector of vectors, where each outer vector is a component and
// the inner vector contains the (y,x) of each pixel in that component.
std::vector<std::vector<SWTPoint> > components = getComponents(SWTImage);
std::vector<Component> validComponents = filterComponents(SWTImage, components, false);
vector<cv::Rect> outTextRegions;
result = findValidChains(input, SWTImage, validComponents, draw, outTextRegions);
if (chainBBs.needed()) {
_InputArray(outTextRegions).copyTo(chainBBs);
}
}
}} // namespace
+91
View File
@@ -0,0 +1,91 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "opencv2/imgcodecs.hpp"
namespace opencv_test { namespace {
// Just skip test in case of missed testdata
static cv::String findDataFile(const String& path)
{
return cvtest::findDataFile(path, false);
}
PARAM_TEST_CASE(Detection, std::string, bool)
{
Ptr<ERFilter> er_filter1;
Ptr<ERFilter> er_filter2;
// SetUp doesn't handle SkipTestException
void InitERFilter()
{
String nm1_file = findDataFile("trained_classifierNM1.xml");
String nm2_file = findDataFile("trained_classifierNM2.xml");
// Create ERFilter objects with the 1st and 2nd stage default classifiers
er_filter1 = createERFilterNM1(loadClassifierNM1(nm1_file),16,0.00015f,0.13f,0.2f,true,0.1f);
er_filter2 = createERFilterNM2(loadClassifierNM2(nm2_file),0.5);
}
};
TEST_P(Detection, sample)
{
InitERFilter();
std::string imageName = GET_PARAM(0);
bool anyDirection = GET_PARAM(1);
if (anyDirection)
throw SkipTestException("ERGROUPING_ORIENTATION_ANY mode is not supported");
std::cout << "Image: " << imageName << std::endl;
std::cout << "Orientation: " << (anyDirection ? "any" : "horiz") << std::endl;
Mat src = cv::imread(findDataFile(imageName));
ASSERT_FALSE(src.empty());
// Extract channels to be processed individually
std::vector<Mat> channels;
computeNMChannels(src, channels);
// Append negative channels to detect ER- (bright regions over dark background)
for (size_t c = channels.size(); c > 0; c--)
channels.push_back(255 - channels[c - 1]);
std::vector<std::vector<ERStat> > regions(channels.size());
// Apply the default cascade classifier to each independent channel (could be done in parallel)
for (size_t c = 0; c < channels.size(); c++)
{
er_filter1->run(channels[c], regions[c]);
er_filter2->run(channels[c], regions[c]);
}
// Detect character groups
std::vector< std::vector<Vec2i> > region_groups;
std::vector<Rect> groups_boxes;
if (!anyDirection)
erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_HORIZ);
else
erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_ANY,
findDataFile("trained_classifier_erGrouping.xml"), 0.5);
std::cout << "Found groups: " << groups_boxes.size() << std::endl;
EXPECT_GT(groups_boxes.size(), 3u);
}
INSTANTIATE_TEST_CASE_P(Text, Detection,
testing::Combine(
testing::Values(
"text/scenetext01.jpg",
"text/scenetext02.jpg",
"text/scenetext03.jpg",
"text/scenetext04.jpg",
"text/scenetext05.jpg",
"text/scenetext06.jpg"
),
testing::Bool()
));
}} // namespace
+49
View File
@@ -0,0 +1,49 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
namespace opencv_test { namespace {
TEST (TextDetectionSWT, accuracy_light_on_dark) {
const string dataPath = cvtest::findDataFile("cv/mser/mser_test.png");
Mat image = imread(dataPath, IMREAD_COLOR);
vector<Rect> components;
detectTextSWT(image, components, false);
/* all 5 letter candidates should be identified (R9888) */
EXPECT_EQ(5u, components.size());
}
TEST (TextDetectionSWT, accuracy_dark_on_light) {
const string dataPath = cvtest::findDataFile("cv/mser/mser_test2.png");
Mat image = imread(dataPath, IMREAD_COLOR);
vector<Rect> components;
detectTextSWT(image, components, true);
/* all 3 letter candidates should be identified 2, 5, 8 */
EXPECT_EQ(3u, components.size());
}
TEST (TextDetectionSWT, accuracy_handwriting) {
const string dataPath = cvtest::findDataFile("cv/cloning/Mixed_Cloning/source1.png");
Mat image = imread(dataPath, IMREAD_COLOR);
vector<Rect> components;
detectTextSWT(image, components, true);
/* Handwritten Text is generally more difficult to detect using SWT algorithm due to high variation in stroke width. */
EXPECT_LT(11u, components.size());
/* Although the text contains 15 characters, the current implementation of algorithm outputs 14, including three wrong guesses. So, we check at least 11 (14 - 3) letters are detected.*/
}
TEST (TextDetectionSWT, accuracy_chaining) {
const string dataPath = cvtest::findDataFile("cv/mser/mser_test.png");
Mat image = imread(dataPath, IMREAD_COLOR);
vector<Rect> components;
Mat out(image.size(), CV_8UC3);
vector<Rect> chains;
detectTextSWT(image, components, false, out, chains);
Rect chain = chains[0];
/* Since the word is already segmented and cropped, most of the area is covered by text. It confirms that chaining works. */
EXPECT_LT(0.95 * image.total(), (double)chain.area());
}
}} // namespace
+9
View File
@@ -0,0 +1,9 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
CV_TEST_MAIN("",
cvtest::addDataSearchSubDirectory("contrib"),
cvtest::addDataSearchSubDirectory("contrib/text")
)
+15
View File
@@ -0,0 +1,15 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_TEST_TEXT_PRECOMP_HPP__
#define __OPENCV_TEST_TEXT_PRECOMP_HPP__
#include "opencv2/core.hpp"
#include "opencv2/ts.hpp"
#include "opencv2/text.hpp"
namespace opencv_test {
using namespace cv::text;
}
#endif
+7
View File
@@ -0,0 +1,7 @@
#ifndef __OPENCV_TEXT_CONFIG_HPP__
#define __OPENCV_TEXT_CONFIG_HPP__
// HAVE OCR Tesseract
#cmakedefine HAVE_TESSERACT
#endif
@@ -0,0 +1,116 @@
Tesseract (master) installation by using git-bash (version>=2.14.1) and cmake (version >=3.9.1){#tutorial_install_tesseract}
===============================================================
-# We assume you installed opencv and opencv_contrib in c:/lib using [this tutorials](http://docs.opencv.org/master/d3/d52/tutorial_windows_install.html#tutorial_windows_gitbash_build]
-# You must download [png lib](https://sourceforge.net/projects/libpng/files/libpng16/1.6.32/lpng1632.zip/download) and [zlib](https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib1211.zip/download).
Uncompress lpngx.y.zz in folder lpng and zlib in folder zlib. lpng and zlib must be in same folder as opencv and opencv_contrib.
save this script with name installpngzlib.sh in c:/lib
@code{.bash}
#!/bin/bash
myRepo=$(pwd)
CMAKE_CONFIG_GENERATOR="Visual Studio 14 2015 Win64"
RepoSource=zlib
mkdir Build/$RepoSource
pushd Build/$RepoSource
cmake . -G"Visual Studio 14 2015 Win64" \
-DCMAKE_INSTALL_PREFIX:PATH="$myRepo"/install/zlib -DINSTALL_BIN_DIR:PATH="$myRepo"/install/zlib/bin \
-DINSTALL_INC_DIR:PATH="$myRepo"/install/zlib/include -DINSTALL_LIB_DIR:PATH="$myRepo"/install/zlib/lib "$myRepo"/"$RepoSource"
cmake --build . --config release
cmake --build . --target install --config release
cmake --build . --config debug
cmake --build . --target install --config debug
popd
RepoSource=lpng
mkdir Build/$RepoSource
pushd Build/$RepoSource
cp "$myRepo"/"$RepoSource"/scripts/pnglibconf.h.prebuilt "$myRepo"/"$RepoSource"/pnglibconf.h
cmake . -G"Visual Studio 14 2015 Win64" \
-DZLIB_INCLUDE_DIR:PATH="$myRepo"/install/zlib/include -DZLIB_LIBRARY_DEBUG:FILE="$myRepo"/install/zlib/lib/zlibstaticd.lib \
-Dld-version-script:BOOL=OFF -DPNG_TESTS:BOOL=OFF -DAWK:STRING= \
-DZLIB_LIBRARY_RELEASE:FILE="$myRepo"/install/zlib/lib/zlibstatic.lib -DCMAKE_INSTALL_PREFIX="$myRepo"/Install/"$RepoSource" \
"$myRepo"/"$RepoSource"
cmake --build . --config release
cmake --build . --target install --config release
cmake --build . --config debug
cmake --build . --target install --config debug
popd
@endcode
-# In git command line enter the following command :
@code{.bash}
./installpngzlib.sh
@endcode
-# save this script with name installTesseract.sh in c:/lib
@code{.bash}
#!/bin/bash
function MAJGitRepo
{
if [ ! -d "$myRepo/$1" ]; then
echo "clonning ${1}"
git clone $2
mkdir Build/$1
else
echo "update $1"
cd $1
git pull --rebase
cd ..
fi
}
echo "Installing leptonica and tesseract"
myRepo=$(pwd)
CMAKE_CONFIG_GENERATOR="Visual Studio 14 2015 Win64"
MAJGitRepo leptonica https://github.com/DanBloomberg/leptonica.git
RepoSource=leptonica
pushd Build/$RepoSource
cmake -G"$CMAKE_CONFIG_GENERATOR" -DCMAKE_INSTALL_PREFIX="$myRepo"/Install/leptonica "$myRepo/$RepoSource"
echo "************************* $Source_DIR -->debug"
cmake --build . --config release
cmake --build $RepoSource --target install --config release
popd
RepoSource=tesseract
MAJGitRepo $RepoSource https://github.com/tesseract-ocr/tesseract.git
pushd Build/$RepoSource
cmake -G"$CMAKE_CONFIG_GENERATOR" -DBUILD_TRAINING_TOOLS:BOOL=OFF -DCMAKE_INSTALL_PREFIX="$myRepo"/Install/tesseract -DLeptonica_DIR:PATH="$myRepo"/Install/leptonica/cmake -DPKG_CONFIG_EXECUTABLE:BOOL=OFF "$myRepo"/"$RepoSource"
echo "************************* $Source_DIR -->release"
cmake --build . --config release
cmake --build . --target install --config release
popd
RepoSource=opencv
pushd Build/$RepoSource
CMAKE_OPTIONS='-DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DWITH_CUDA:BOOL=OFF'
cmake -G"$CMAKE_CONFIG_GENERATOR" \
-DTesseract_INCLUDE_DIR:PATH="${myRepo}"/Install/tesseract/include -DTesseract_LIBRARY="${myRepo}"/Install/tesseract/lib/tesseract400.lib -DLept_LIBRARY="${myRepo}"/Install/leptonica/lib/leptonica-1.74.4.lib \
$CMAKE_OPTIONS -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-DINSTALL_CREATE_DISTRIB=ON -DCMAKE_INSTALL_PREFIX="$myRepo"/install/"$RepoSource" "$myRepo/$RepoSource"
echo "************************* $Source_DIR -->devenv debug"
cmake --build . --config debug
echo "************************* $Source_DIR -->devenv release"
cmake --build . --config release
cmake --build . --target install --config release
cmake --build . --target install --config debug
popd
@endcode
In this script I suppose you use VS 2015 in 64 bits
@code{.bash}
CMAKE_CONFIG_GENERATOR="Visual Studio 14 2015 Win64"
@endcode
and leptonica, tesseract will be installed in c:/lib/install
@code{.bash}
-DCMAKE_INSTALL_PREFIX="$myRepo"/install/"$RepoSource" "$myRepo/$RepoSource"
@endcode
with no Perf tests, no tests, no doc, no CUDA and no example
@code{.bash}
CMAKE_OPTIONS='-DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DWITH_CUDA:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF'
@endcode
-# In git command line enter the following command :
@code{.bash}
./installTesseract.sh
@endcode
-# now we need the language files from tesseract. either clone https://github.com/tesseract-ocr/tessdata, or copy only those language files you need to a folder (example c:\\lib\\install\\tesseract\\tessdata). If you don't want to add a new folder you must copy language file in same folder than your executable
-# if you created a new folder, then you must add a new variable, TESSDATA_PREFIX with the value c:\\lib\\install\\tessdata to your system's environment
-# add c:\\Lib\\install\\leptonica\\bin and c:\\Lib\\install\\tesseract\\bin to your PATH environment. If you don't want to modify the PATH then copy tesseract400.dll and leptonica-1.74.4.dll to the same folder than your exe file.
@@ -0,0 +1,10 @@
Text module {#tutorial_table_of_content_text}
=====================
- @subpage tutorial_install_tesseract
*Compatibility:* \> OpenCV 3.4
*Author:* Laurent Berger
Instructions in order to properly setup tesseract on windows for the text module.