2013-09-27 22:28:47 +08:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
*/
|
2013-07-20 07:10:05 +08:00
|
|
|
|
|
|
|
#include "opencv2/opencv.hpp"
|
|
|
|
#include "opencv2/objdetect.hpp"
|
|
|
|
#include "opencv2/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
void show_help_and_exit(const char *cmd);
|
|
|
|
void groups_draw(Mat &src, vector<Rect> &groups);
|
2013-09-27 22:28:47 +08:00
|
|
|
void er_show(vector<Mat> &channels, vector<vector<ERStat> > ®ions);
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
int main(int argc, const char * argv[])
|
2013-07-20 07:10:05 +08:00
|
|
|
{
|
2013-09-27 22:28:47 +08:00
|
|
|
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;
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
if (argc < 2) show_help_and_exit(argv[0]);
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
Mat src = imread(argv[1]);
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
// Extract channels to be processed individually
|
|
|
|
vector<Mat> channels;
|
|
|
|
computeNMChannels(src, channels);
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
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]);
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
// Create ERFilter objects with the 1st and 2nd stage default classifiers
|
2013-09-27 22:28:47 +08:00
|
|
|
Ptr<ERFilter> er_filter1 = createERFilterNM1(loadClassifierNM1("trained_classifierNM1.xml"),16,0.00015,0.13,0.2,true,0.1);
|
|
|
|
Ptr<ERFilter> er_filter2 = createERFilterNM2(loadClassifierNM2("trained_classifierNM2.xml"),0.5);
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
vector<vector<ERStat> > regions(channels.size());
|
|
|
|
// Apply the default cascade classifier to each independent channel (could be done in parallel)
|
2013-09-27 22:28:47 +08:00
|
|
|
cout << "Extracting Class Specific Extremal Regions from " << (int)channels.size() << " channels ..." << endl;
|
|
|
|
cout << " (...) this may take a while (...)" << endl << endl;
|
2013-09-18 05:45:23 +08:00
|
|
|
for (int c=0; c<(int)channels.size(); c++)
|
2013-07-20 07:10:05 +08:00
|
|
|
{
|
2013-09-18 05:45:23 +08:00
|
|
|
er_filter1->run(channels[c], regions[c]);
|
|
|
|
er_filter2->run(channels[c], regions[c]);
|
2013-07-20 07:10:05 +08:00
|
|
|
}
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
// Detect character groups
|
2013-09-27 22:28:47 +08:00
|
|
|
cout << "Grouping extracted ERs ... ";
|
2013-09-18 05:45:23 +08:00
|
|
|
vector<Rect> groups;
|
2013-09-27 22:28:47 +08:00
|
|
|
erGrouping(channels, regions, "trained_classifier_erGrouping.xml", 0.5, groups);
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
// draw groups
|
|
|
|
groups_draw(src, groups);
|
|
|
|
imshow("grouping",src);
|
2013-09-27 22:28:47 +08:00
|
|
|
|
|
|
|
cout << "Done!" << endl << endl;
|
|
|
|
cout << "Press 'e' to show the extracted Extremal Regions, any other key to exit." << endl << endl;
|
|
|
|
if( waitKey (-1) == 101)
|
|
|
|
er_show(channels,regions);
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
// memory clean-up
|
2013-07-20 07:10:05 +08:00
|
|
|
er_filter1.release();
|
2013-09-18 05:45:23 +08:00
|
|
|
er_filter2.release();
|
|
|
|
regions.clear();
|
|
|
|
if (!groups.empty())
|
2013-07-20 07:10:05 +08:00
|
|
|
{
|
2013-09-18 05:45:23 +08:00
|
|
|
groups.clear();
|
2013-07-20 07:10:05 +08:00
|
|
|
}
|
2013-09-18 05:45:23 +08:00
|
|
|
}
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-08-27 17:57:24 +08:00
|
|
|
|
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
// helper functions
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
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);
|
|
|
|
}
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-18 05:45:23 +08:00
|
|
|
void groups_draw(Mat &src, vector<Rect> &groups)
|
|
|
|
{
|
|
|
|
for (int i=groups.size()-1; i>=0; i--)
|
2013-07-20 07:10:05 +08:00
|
|
|
{
|
2013-09-18 05:45:23 +08:00
|
|
|
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 );
|
2013-07-20 07:10:05 +08:00
|
|
|
}
|
2013-09-18 05:45:23 +08:00
|
|
|
}
|
2013-07-20 07:10:05 +08:00
|
|
|
|
2013-09-27 22:28:47 +08:00
|
|
|
void er_show(vector<Mat> &channels, vector<vector<ERStat> > ®ions)
|
2013-09-18 05:45:23 +08:00
|
|
|
{
|
2013-09-27 22:28:47 +08:00
|
|
|
for (int c=0; c<(int)channels.size(); c++)
|
2013-09-18 05:45:23 +08:00
|
|
|
{
|
2013-09-27 22:28:47 +08:00
|
|
|
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[10]; char *buff_ptr = buff;
|
|
|
|
sprintf(buff, "channel %d", c);
|
|
|
|
imshow(buff_ptr, dst);
|
2013-09-18 05:45:23 +08:00
|
|
|
}
|
2013-09-27 22:28:47 +08:00
|
|
|
waitKey(-1);
|
2013-07-20 07:10:05 +08:00
|
|
|
}
|