Merge pull request #15 from Silex/cleanup

Cleanup
This commit is contained in:
Matthew Hill
2014-03-20 08:37:11 -05:00
70 changed files with 8186 additions and 8553 deletions

View File

@@ -1,6 +1,5 @@
[common]
ocr_img_size_percent = 1.33333333
state_id_img_size_percent = 2.0
@@ -39,8 +38,6 @@ ocr = 0
postprocess = 0
show_images = 0
;;; Country Specific variables ;;;;
[us]
@@ -70,7 +67,6 @@ template_max_height_px = 60
plateline_sensitivity_vertical = 25
plateline_sensitivity_horizontal = 45
; Regions smaller than this will be disqualified
min_plate_size_width_px = 70
min_plate_size_height_px = 35
@@ -85,7 +81,6 @@ char_analysis_height_range = 0.15
char_analysis_height_step_size = 0.10
char_analysis_height_num_steps = 5
segmentation_min_box_width_px = 8
segmentation_min_charheight_percent = 0.5;
segmentation_max_segment_width_percent_vs_average = 2.0;

View File

@@ -17,33 +17,25 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <stdio.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "tclap/CmdLine.h"
#include "support/filesystem.h"
#include "support/timing.h"
#include "alpr.h"
const std::string MAIN_WINDOW_NAME = "ALPR main window";
const bool SAVE_LAST_VIDEO_STILL = false;
const std::string LAST_VIDEO_STILL_LOCATION = "/tmp/laststill.jpg";
/** Function Headers */
bool detectandshow(Alpr* alpr, cv::Mat frame, std::string region, bool writeJson);
bool measureProcessingTime = false;
int main( int argc, const char** argv )
@@ -57,7 +49,8 @@
std::string country;
int topn;
try {
try
{
TCLAP::CmdLine cmd("OpenAlpr Command Line Utility", ' ', OPENALPR_VERSION);
@@ -93,7 +86,8 @@
topn = topNArg.getValue();
measureProcessingTime = clockSwitch.getValue();
} catch (TCLAP::ArgException &e) // catch any exceptions
}
catch (TCLAP::ArgException &e) // catch any exceptions
{
std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
return 1;
@@ -101,7 +95,6 @@
cv::Mat frame;
Alpr alpr(country, runtimePath);
alpr.setTopN(topn);
@@ -179,7 +172,6 @@
std::cerr << "Image file not found: " << filename << std::endl;
}
}
else if (DirectoryExists(filename.c_str()))
{
@@ -212,27 +204,20 @@
return 1;
}
return 0;
}
bool detectandshow( Alpr* alpr, cv::Mat frame, std::string region, bool writeJson)
{
std::vector<uchar> buffer;
cv::imencode(".bmp", frame, buffer );
timespec startTime;
getTime(&startTime);
std::vector<AlprResult> results = alpr->recognize(buffer);
if (writeJson)
{
std::cout << alpr->toJson(results) << std::endl;
@@ -256,7 +241,6 @@
if (measureProcessingTime)
std::cout << "Total Time to process image: " << diffclock(startTime, endTime) << "ms." << std::endl;
if (results.size() > 0)
return true;
return false;

View File

@@ -50,7 +50,6 @@ int main( int argc, const char** argv )
string outDir;
Mat frame;
//Check if user specify image to process
if(argc == 5)
{
@@ -59,8 +58,9 @@ int main( int argc, const char** argv )
inDir = argv[3];
outDir = argv[4];
}else{
}
else
{
printf("Use:\n\t%s [country] [benchmark name] [img input dir] [results output dir]\n",argv[0]);
printf("\tex: %s us speed ./speed/usimages ./speed\n",argv[0]);
printf("\n");
@@ -68,7 +68,6 @@ int main( int argc, const char** argv )
return 0;
}
if (DirectoryExists(inDir.c_str()) == false)
{
printf("Input dir does not exist\n");
@@ -80,11 +79,9 @@ int main( int argc, const char** argv )
return 0;
}
vector<string> files = getFilesInDir(inDir.c_str());
sort( files.begin(), files.end(), stringCompare );
if (benchmarkName.compare("segocr") == 0)
{
Config* config = new Config(country);
@@ -92,7 +89,6 @@ int main( int argc, const char** argv )
OCR* ocr = new OCR(config);
for (int i = 0; i< files.size(); i++)
{
if (hasEnding(files[i], ".png"))
@@ -129,19 +125,15 @@ int main( int argc, const char** argv )
rotated.copyTo(frame);
}
CharacterSegmenter charSegmenter(frame, charRegion.thresholdsInverted(), config);
ocr->performOCR(charSegmenter.getThresholds(), charSegmenter.characters);
ocr->postProcessor->analyze(statecode, 25);
cout << files[i] << "," << statecode << "," << ocr->postProcessor->bestChars << endl;
imshow("Current LP", frame);
waitKey(5);
}
}
@@ -166,7 +158,6 @@ int main( int argc, const char** argv )
imshow("Current LP", frame);
waitKey(5);
}
}
@@ -197,7 +188,6 @@ int main( int argc, const char** argv )
vector<double> ocrTimes;
vector<double> postProcessTimes;
for (int i = 0; i< files.size(); i++)
{
if (hasEnding(files[i], ".png"))
@@ -207,7 +197,6 @@ int main( int argc, const char** argv )
string fullpath = inDir + "/" + files[i];
frame = imread( fullpath.c_str() );
getTime(&startTime);
alpr.recognize(frame);
getTime(&endTime);
@@ -244,7 +233,6 @@ int main( int argc, const char** argv )
{
lpAnalysisPositiveTimes.push_back(analysisTime);
getTime(&startTime);
ocr.performOCR(lp.charSegmenter->getThresholds(), lp.charSegmenter->characters);
getTime(&endTime);
@@ -273,7 +261,6 @@ int main( int argc, const char** argv )
cout << endl << "---------------------" << endl;
cout << "End to End Time Statistics:" << endl;
outputStats(endToEndTimes);
cout << endl;
@@ -333,7 +320,6 @@ int main( int argc, const char** argv )
imshow("Current LP", frame);
waitKey(5);
}
}

View File

@@ -61,7 +61,6 @@ const int DASHBOARD_COLUMNS = 3;
#endif
void showDashboard(vector<Mat> images, vector<bool> selectedImages, int selectedIndex);
vector<char> showCharSelection(Mat image, vector<Rect> charRegions, string state);
@@ -72,21 +71,20 @@ int main( int argc, const char** argv )
string outDir;
Mat frame;
//Check if user specify image to process
if(argc == 3)
{
inDir = argv[1];
outDir = argv[2];
}else{
}
else
{
printf("Use:\n\t%s indirectory outdirectory\n",argv[0]);
printf("Ex: \n\t%s ./pics/ ./out \n",argv[0]);
return 0;
}
if (DirectoryExists(outDir.c_str()) == false)
{
printf("Output dir does not exist\n");
@@ -125,7 +123,6 @@ int main( int argc, const char** argv )
imshow ("Original", frame);
char statecode[3];
statecode[0] = files[i][0];
statecode[1] = files[i][1];
@@ -155,7 +152,6 @@ int main( int argc, const char** argv )
ocr.postProcessor->analyze(statecodestr, 25);
cout << "OCR results: " << ocr.postProcessor->bestChars << endl;
vector<bool> selectedBoxes(charSegmenter.getThresholds().size());
for (int z = 0; z < charSegmenter.getThresholds().size(); z++)
selectedBoxes[z] = false;
@@ -169,8 +165,6 @@ int main( int argc, const char** argv )
showDashboard(charSegmenter.getThresholds(), selectedBoxes, 0);
char waitkey = (char) waitKey(50);
while (waitkey != 'n' && waitkey != 'p') // Next image
@@ -267,7 +261,6 @@ int main( int argc, const char** argv )
if (i < -1)
i = -1;
}
}
@@ -275,7 +268,6 @@ int main( int argc, const char** argv )
}
void showDashboard(vector<Mat> images, vector<bool> selectedImages, int selectedIndex)
{
vector<Mat> vecCopy;
@@ -315,7 +307,6 @@ vector<char> showCharSelection(Mat image, vector<Rect> charRegions, string state
for (int i = 0; i < charRegions.size(); i++)
humanInputs[i] = (char) SPACE_KEY;
char waitkey = (char) waitKey(50);
while (waitkey != ENTER_KEY && waitkey != ESCAPE_KEY)
{
@@ -365,6 +356,5 @@ vector<char> showCharSelection(Mat image, vector<Rect> charRegions, string state
destroyWindow("Character selector");
return humanInputs;
}

View File

@@ -29,8 +29,6 @@
using namespace std;
using namespace cv;
// Takes a directory full of single char images, and plops them on a big tif files
// Also creates a box file so Tesseract can recognize it
int main( int argc, const char** argv )
@@ -38,18 +36,18 @@ int main( int argc, const char** argv )
string inDir;
//Check if user specify image to process
if(argc == 2)
{
inDir = argv[1];
}else{
}
else
{
printf("Use:\n\t%s input dir \n",argv[0]);
return 0;
}
if (DirectoryExists(inDir.c_str()) == false)
{
printf("Output dir does not exist\n");
@@ -59,7 +57,6 @@ int main( int argc, const char** argv )
cout << "Usage: " << endl;
cout << "\tinputdir -- input dir for benchmark data" << endl;
if (DirectoryExists(inDir.c_str()))
{
const int X_OFFSET = 10;
@@ -78,7 +75,6 @@ int main( int argc, const char** argv )
sort( files.begin(), files.end(), stringCompare );
int tiles_per_row = ((float) (HORIZONTAL_RESOLUTION - (PAGE_MARGIN_X * 2))) / ((float) TILE_WIDTH);
int lines = files.size() / (tiles_per_row);
int vertical_resolution = (lines * TILE_HEIGHT) + (PAGE_MARGIN_Y * 2) ;
@@ -130,7 +126,6 @@ int main( int argc, const char** argv )
Rect cropRect(0, tallestRect.y - Y_OFFSET, tallestRect.width, tallestRect.height);
//cout << "Cropped: " << cropRect.x << ":" << cropRect.y << " -- " << cropRect.width << ":" << cropRect.height << endl;
Mat cropped(characterImg, cropRect);
cvtColor(cropped, cropped, CV_BGR2GRAY);
@@ -141,7 +136,6 @@ int main( int argc, const char** argv )
cropped.copyTo(bigTif(destinationRect));
int x1= destinationRect.x - 2;
int y1 = (vertical_resolution - destinationRect.y - destinationRect.height) - 2;
int x2 = (destinationRect.x + destinationRect.width) + 2;
@@ -156,12 +150,10 @@ int main( int argc, const char** argv )
waitKey(2);
}
}
imwrite("combined.tif", bigTif);
ofstream boxFile("combined.box", std::ios::out);
boxFile << boxFileOut.str();

View File

@@ -45,7 +45,6 @@ int main( int argc, const char** argv )
string outDir;
Mat frame;
//Check if user specify image to process
if(argc == 3 )
{
@@ -53,8 +52,9 @@ int main( int argc, const char** argv )
outDir = argv[2];
outDir = outDir + "/";
}else{
}
else
{
printf("Use:\n\t%s directory \n",argv[0]);
printf("Ex: \n\t%s ./pics/ \n",argv[0]);
return 0;
@@ -99,7 +99,6 @@ int main( int argc, const char** argv )
ostringstream convert; // stream used for the conversion
convert << i; // insert the textual representation of 'Number' in the characters in the stream
string copyCommand = "cp \"" + fullpath + "\" " + outDir + code + convert.str() + ".png";
system( copyCommand.c_str() );
waitKey(50);

View File

@@ -31,45 +31,67 @@ extern "C" {
#include "trex.h"
}
struct TRexParseException{TRexParseException(const TRexChar *c):desc(c){}const TRexChar *desc;};
struct TRexParseException
{
TRexParseException(const TRexChar *c):desc(c) {} const TRexChar *desc;
};
class TRexpp {
class TRexpp
{
public:
TRexpp() { _exp = (TRex *)0; }
~TRexpp() { CleanUp(); }
TRexpp()
{
_exp = (TRex *)0;
}
~TRexpp()
{
CleanUp();
}
// compiles a regular expression
void Compile(const TRexChar *pattern) {
void Compile(const TRexChar *pattern)
{
const TRexChar *error;
CleanUp();
if(!(_exp = trex_compile(pattern,&error)))
throw TRexParseException(error);
}
// return true if the given text match the expression
bool Match(const TRexChar* text) {
bool Match(const TRexChar* text)
{
return _exp?(trex_match(_exp,text) != 0):false;
}
// Searches for the first match of the expression in a zero terminated string
bool Search(const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end) {
bool Search(const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end)
{
return _exp?(trex_search(_exp,text,out_begin,out_end) != 0):false;
}
// Searches for the first match of the expression in a string sarting at text_begin and ending at text_end
bool SearchRange(const TRexChar* text_begin,const TRexChar* text_end,const TRexChar** out_begin, const TRexChar** out_end) {
bool SearchRange(const TRexChar* text_begin,const TRexChar* text_end,const TRexChar** out_begin, const TRexChar** out_end)
{
return _exp?(trex_searchrange(_exp,text_begin,text_end,out_begin,out_end) != 0):false;
}
bool GetSubExp(int n, const TRexChar** out_begin, int *out_len)
{
TRexMatch match;
TRexBool res = _exp?(trex_getsubexp(_exp,n,&match)):TRex_False;
if(res) {
if(res)
{
*out_begin = match.begin;
*out_len = match.len;
return true;
}
return false;
}
int GetSubExpCount() { return _exp?trex_getsubexpcount(_exp):0; }
int GetSubExpCount()
{
return _exp?trex_getsubexpcount(_exp):0;
}
private:
void CleanUp() { if(_exp) trex_free(_exp); _exp = (TRex *)0; }
void CleanUp()
{
if(_exp) trex_free(_exp);
_exp = (TRex *)0;
}
TRex *_exp;
};
#endif //_TREXPP_H_

View File

@@ -20,9 +20,6 @@
#include "alpr.h"
#include "alpr_impl.h"
// ALPR code
Alpr::Alpr(const std::string country, const std::string runtimeDir)
@@ -40,7 +37,6 @@ std::vector<AlprResult> Alpr::recognize(std::string filepath)
}
std::vector<AlprResult> Alpr::recognize(std::vector<unsigned char> imageBuffer)
{
// Not sure if this actually works
@@ -49,13 +45,11 @@ std::vector<AlprResult> Alpr::recognize(std::vector<unsigned char> imageBuffer)
return impl->recognize(img);
}
string Alpr::toJson(const vector< AlprResult > results)
{
return impl->toJson(results);
}
void Alpr::setDetectRegion(bool detectRegion)
{
impl->setDetectRegion(detectRegion);
@@ -74,11 +68,8 @@ bool Alpr::isLoaded()
return true;
}
// Results code
AlprResult::AlprResult()
{

View File

@@ -19,7 +19,6 @@
#include "alpr_impl.h"
AlprImpl::AlprImpl(const std::string country, const std::string runtimeDir)
{
config = new Config(country, runtimeDir);
@@ -68,7 +67,6 @@ AlprImpl::~AlprImpl()
delete ocr;
}
std::vector<AlprResult> AlprImpl::recognize(cv::Mat img)
{
timespec startTime;
@@ -76,10 +74,8 @@ std::vector<AlprResult> AlprImpl::recognize(cv::Mat img)
vector<AlprResult> response;
vector<Rect> plateRegions = plateDetector->detect(img);
// Recognize.
for (int i = 0; i < plateRegions.size(); i++)
@@ -91,7 +87,6 @@ std::vector<AlprResult> AlprImpl::recognize(cv::Mat img)
lp.recognize();
if (lp.confidence > 10)
{
AlprResult plateResult;
@@ -114,7 +109,6 @@ std::vector<AlprResult> AlprImpl::recognize(cv::Mat img)
}
}
ocr->performOCR(lp.charSegmenter->getThresholds(), lp.charSegmenter->characters);
ocr->postProcessor->analyze(plateResult.region, topN);
@@ -162,7 +156,6 @@ std::vector<AlprResult> AlprImpl::recognize(cv::Mat img)
line(img, lp.plateCorners[z], lp.plateCorners[(z + 1) % 4], Scalar(255,0,255), 2);
}
}
else
{
@@ -210,8 +203,6 @@ string AlprImpl::toJson(const vector< AlprResult > results)
return response;
}
cJSON* AlprImpl::createJsonObj(const AlprResult* result)
{
cJSON *root, *coords, *candidates;
@@ -238,7 +229,6 @@ cJSON* AlprImpl::createJsonObj(const AlprResult* result)
cJSON_AddItemToArray(coords, coords_object);
}
cJSON_AddItemToObject(root, "candidates", candidates=cJSON_CreateArray());
for (int i = 0; i < result->topNPlates.size(); i++)
{
@@ -254,7 +244,6 @@ cJSON* AlprImpl::createJsonObj(const AlprResult* result)
return root;
}
void AlprImpl::setDetectRegion(bool detectRegion)
{
this->detectRegion = detectRegion;

View File

@@ -33,14 +33,13 @@
#include "binarize_wolf.h"
// *************************************************************
// glide a window across the image and
// create two maps: mean and standard deviation.
// *************************************************************
float calcLocalStats (Mat &im, Mat &map_m, Mat &map_s, int winx, int winy) {
float calcLocalStats (Mat &im, Mat &map_m, Mat &map_s, int winx, int winy)
{
float m,s,max_s;
long sum, sum_sq;
@@ -52,14 +51,14 @@ float calcLocalStats (Mat &im, Mat &map_m, Mat &map_s, int winx, int winy) {
int y_firstth= wyh;
float winarea = winx*winy;
max_s = 0;
for (int j = y_firstth ; j<=y_lastth; j++)
{
// Calculate the initial window at the beginning of the line
sum = sum_sq = 0;
for (int wy=0 ; wy<winy; wy++)
for (int wx=0 ; wx<winx; wx++) {
for (int wx=0 ; wx<winx; wx++)
{
foo = im.uget(wx,j-wyh+wy);
sum += foo;
sum_sq += foo*foo;
@@ -72,10 +71,12 @@ float calcLocalStats (Mat &im, Mat &map_m, Mat &map_s, int winx, int winy) {
map_s.fset(x_firstth, j, s);
// Shift the window, add and remove new/old values to the histogram
for (int i=1 ; i <= im.cols-winx; i++) {
for (int i=1 ; i <= im.cols-winx; i++)
{
// Remove the left old column and add the right new column
for (int wy=0; wy<winy; ++wy) {
for (int wy=0; wy<winy; ++wy)
{
foo = im.uget(i-1,j-wyh+wy);
sum -= foo;
sum_sq -= foo*foo;
@@ -95,14 +96,13 @@ float calcLocalStats (Mat &im, Mat &map_m, Mat &map_s, int winx, int winy) {
return max_s;
}
/**********************************************************
* The binarization routine
**********************************************************/
void NiblackSauvolaWolfJolion (Mat im, Mat output, NiblackVersion version,
int winx, int winy, float k) {
int winx, int winy, float k)
{
float dR = BINARIZEWOLF_DEFAULTDR;
@@ -129,16 +129,19 @@ void NiblackSauvolaWolfJolion (Mat im, Mat output, NiblackVersion version,
// Create the threshold surface, including border processing
// ----------------------------------------------------
for (int j = y_firstth ; j<=y_lastth; j++) {
for (int j = y_firstth ; j<=y_lastth; j++)
{
// NORMAL, NON-BORDER AREA IN THE MIDDLE OF THE WINDOW:
for (int i=0 ; i <= im.cols-winx; i++) {
for (int i=0 ; i <= im.cols-winx; i++)
{
m = map_m.fget(i+wxh, j);
s = map_s.fget(i+wxh, j);
// Calculate the threshold
switch (version) {
switch (version)
{
case NIBLACK:
th = m + k*s;
@@ -159,7 +162,8 @@ void NiblackSauvolaWolfJolion (Mat im, Mat output, NiblackVersion version,
thsurf.fset(i+wxh,j,th);
if (i==0) {
if (i==0)
{
// LEFT BORDER
for (int i=0; i<=x_firstth; ++i)
thsurf.fset(i,j,th);
@@ -205,8 +209,6 @@ void NiblackSauvolaWolfJolion (Mat im, Mat output, NiblackVersion version,
thsurf.fset(i,u,th);
}
for (int y=0; y<im.rows; ++y)
for (int x=0; x<im.cols; ++x)
{

View File

@@ -29,7 +29,6 @@ CharacterAnalysis::CharacterAnalysis(Mat img, Config* config)
if (this->config->debugCharAnalysis)
cout << "Starting CharacterAnalysis identification" << endl;
if (img.type() != CV_8U)
cvtColor( img, this->img_gray, CV_BGR2GRAY );
else
@@ -49,15 +48,11 @@ CharacterAnalysis::~CharacterAnalysis()
thresholds.clear();
}
void CharacterAnalysis::analyze()
{
thresholds = produceThresholds(img_gray, config);
/*
// Morph Close the gray image to make it easier to detect blobs
int morph_elem = 1;
@@ -73,11 +68,9 @@ void CharacterAnalysis::analyze()
}
*/
timespec startTime;
getTime(&startTime);
for (int i = 0; i < thresholds.size(); i++)
{
vector<vector<Point> > contours;
@@ -95,9 +88,6 @@ void CharacterAnalysis::analyze()
allHierarchy.push_back(hierarchy);
}
if (config->debugTiming)
{
timespec endTime;
@@ -106,7 +96,6 @@ void CharacterAnalysis::analyze()
}
//Mat img_equalized = equalizeBrightness(img_gray);
getTime(&startTime);
for (int i = 0; i < thresholds.size(); i++)
@@ -125,8 +114,6 @@ void CharacterAnalysis::analyze()
cout << " -- Character Analysis Filter Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
this->plateMask = findOuterBoxMask();
if (hasPlateMask)
@@ -138,7 +125,6 @@ void CharacterAnalysis::analyze()
}
}
int bestFitScore = -1;
int bestFitIndex = -1;
for (int i = 0; i < thresholds.size(); i++)
@@ -149,7 +135,6 @@ void CharacterAnalysis::analyze()
int segmentCount = getGoodIndicesCount(charSegments[i]);
if (segmentCount > bestFitScore)
{
bestFitScore = segmentCount;
@@ -165,11 +150,9 @@ void CharacterAnalysis::analyze()
if (this->config->debugCharAnalysis)
cout << "Best fit score: " << bestFitScore << " Index: " << bestFitIndex << endl;
if (bestFitScore <= 1)
return;
//getColorMask(img, allContours, allHierarchy, charSegments);
if (this->config->debugCharAnalysis)
@@ -196,15 +179,11 @@ void CharacterAnalysis::analyze()
cv::Scalar(0,255,0), // in green
1); // with a thickness of 1
displayImage(config, "Matching Contours", img_contours);
}
//charsegments = this->getPossibleCharRegions(img_threshold, allContours, allHierarchy, STARTING_MIN_HEIGHT + (bestFitIndex * HEIGHT_STEP), STARTING_MAX_HEIGHT + (bestFitIndex * HEIGHT_STEP));
this->linePolygon = getBestVotedLines(img_gray, bestContours, bestCharSegments);
if (this->linePolygon.size() > 0)
@@ -223,14 +202,11 @@ void CharacterAnalysis::analyze()
this->charBoxLeft = LineSegment(this->charArea[3].x, this->charArea[3].y, this->charArea[0].x, this->charArea[0].y);
this->charBoxRight = LineSegment(this->charArea[2].x, this->charArea[2].y, this->charArea[1].x, this->charArea[1].y);
}
}
this->thresholdsInverted = isPlateInverted();
}
int CharacterAnalysis::getGoodIndicesCount(vector<bool> goodIndices)
@@ -245,8 +221,6 @@ int CharacterAnalysis::getGoodIndicesCount(vector<bool> goodIndices)
return count;
}
Mat CharacterAnalysis::findOuterBoxMask()
{
double min_parent_area = config->templateHeightPx * config->templateWidthPx * 0.10; // Needs to be at least 10% of the plate area to be considered.
@@ -256,7 +230,6 @@ Mat CharacterAnalysis::findOuterBoxMask()
int bestCharCount = 0;
double lowestArea = 99999999999999;
if (this->config->debugCharAnalysis)
cout << "CharacterAnalysis::findOuterBoxMask" << endl;
@@ -297,15 +270,11 @@ Mat CharacterAnalysis::findOuterBoxMask()
}
}
}
if (this->config->debugCharAnalysis)
cout << "Winning image index is: " << winningIndex << endl;
if (winningIndex != -1 && bestCharCount >= 3)
{
int longestChildIndex = -1;
@@ -327,10 +296,6 @@ Mat CharacterAnalysis::findOuterBoxMask()
}
}
Mat mask = Mat::zeros(thresholds[winningIndex].size(), CV_8U);
// get rid of the outline by drawing a 1 pixel width black line
@@ -343,7 +308,6 @@ Mat CharacterAnalysis::findOuterBoxMask()
0
);
// Morph Open the mask to get rid of any little connectors to non-plate portions
int morph_elem = 2;
int morph_size = 3;
@@ -356,7 +320,6 @@ Mat CharacterAnalysis::findOuterBoxMask()
//element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
//dilate(mask, mask, element);
// Drawing the edge black effectively erodes the image. This may clip off some extra junk from the edges.
// We'll want to do the contour again and find the larges one so that we remove the clipped portion.
@@ -394,8 +357,6 @@ Mat CharacterAnalysis::findOuterBoxMask()
0
);
}
if (this->config->debugCharAnalysis)
@@ -422,11 +383,8 @@ Mat CharacterAnalysis::findOuterBoxMask()
bitwise_not(fullMask, fullMask);
return fullMask;
}
Mat CharacterAnalysis::getCharacterMask()
{
@@ -437,7 +395,6 @@ Mat CharacterAnalysis::getCharacterMask()
if (bestCharSegments[i] == false)
continue;
drawContours(charMask, bestContours,
i, // draw this contour
cv::Scalar(255,255,255), // in
@@ -458,12 +415,9 @@ Mat CharacterAnalysis::getCharacterMask()
);
}
return charMask;
}
// Returns a polygon "stripe" across the width of the character region. The lines are voted and the polygon starts at 0 and extends to image width
vector<Point> CharacterAnalysis::getBestVotedLines(Mat img, vector<vector<Point> > contours, vector<bool> goodIndices)
{
@@ -481,7 +435,6 @@ vector<Point> CharacterAnalysis::getBestVotedLines(Mat img, vector<vector<Point>
charRegions.push_back(boundingRect(contours[i]));
}
// Find the best fit line segment that is parallel with the most char segments
if (charRegions.size() <= 1)
{
@@ -499,7 +452,6 @@ vector<Point> CharacterAnalysis::getBestVotedLines(Mat img, vector<vector<Point>
//Mat tempImg;
//result.copyTo(tempImg);
Rect* leftRect;
Rect* rightRect;
if (charRegions[i].x < charRegions[k].x)
@@ -534,7 +486,6 @@ vector<Point> CharacterAnalysis::getBestVotedLines(Mat img, vector<vector<Point>
//cv::line(tempImg, Point(x1, y1), Point(x2, y2), Scalar(0, 0, 255));
topLines.push_back(LineSegment(x1, y1, x2, y2));
if (leftRect->y > rightRect->y) // Rising line, use the bottom right corner of the rect
{
x1 = leftRect->x + leftRect->width;
@@ -565,7 +516,6 @@ vector<Point> CharacterAnalysis::getBestVotedLines(Mat img, vector<vector<Point>
float SCORING_MIN_THRESHOLD = 0.97;
float SCORING_MAX_THRESHOLD = 1.03;
int curScore = 0;
for (int charidx = 0; charidx < charRegions.size(); charidx++)
{
@@ -588,7 +538,6 @@ vector<Point> CharacterAnalysis::getBestVotedLines(Mat img, vector<vector<Point>
}
// Tie goes to the one with longer line segments
if ((curScore > bestScore) ||
(curScore == bestScore && topLines[i].length > bestScoreDistance))
@@ -600,7 +549,6 @@ vector<Point> CharacterAnalysis::getBestVotedLines(Mat img, vector<vector<Point>
}
}
if (this->config->debugCharAnalysis)
{
cout << "The winning score is: " << bestScore << endl;
@@ -621,21 +569,16 @@ vector<Point> CharacterAnalysis::getBestVotedLines(Mat img, vector<vector<Point>
Point bottomRight = Point(img.cols, bottomLines[bestScoreIndex].getPointAt(img.cols));
Point bottomLeft = Point(0, bottomLines[bestScoreIndex].getPointAt(0));
bestStripe.push_back(topLeft);
bestStripe.push_back(topRight);
bestStripe.push_back(bottomRight);
bestStripe.push_back(bottomLeft);
}
return bestStripe;
}
vector<bool> CharacterAnalysis::filter(Mat img, vector<vector<Point> > contours, vector<Vec4i> hierarchy)
{
static int STARTING_MIN_HEIGHT = round (((float) img.rows) * config->charAnalysisMinPercent);
@@ -643,7 +586,6 @@ vector<bool> CharacterAnalysis::filter(Mat img, vector<vector<Point> > contours,
static int HEIGHT_STEP = round (((float) img.rows) * config->charAnalysisHeightStepSize);
static int NUM_STEPS = config->charAnalysisNumSteps;
vector<bool> charSegments;
int bestFitScore = -1;
for (int i = 0; i < NUM_STEPS; i++)
@@ -655,7 +597,6 @@ vector<bool> CharacterAnalysis::filter(Mat img, vector<vector<Point> > contours,
goodIndices = this->filterByBoxSize(contours, goodIndices, STARTING_MIN_HEIGHT + (i * HEIGHT_STEP), STARTING_MAX_HEIGHT + (i * HEIGHT_STEP));
goodIndicesCount = getGoodIndicesCount(goodIndices);
if ( goodIndicesCount > 0 && goodIndicesCount <= bestFitScore) // Don't bother doing more filtering if we already lost...
continue;
@@ -668,7 +609,6 @@ vector<bool> CharacterAnalysis::filter(Mat img, vector<vector<Point> > contours,
vector<Point> lines = getBestVotedLines(img, contours, goodIndices);
goodIndices = this->filterBetweenLines(img, contours, hierarchy, lines, goodIndices);
int segmentCount = getGoodIndicesCount(goodIndices);
if (segmentCount > bestFitScore)
@@ -678,11 +618,9 @@ vector<bool> CharacterAnalysis::filter(Mat img, vector<vector<Point> > contours,
}
}
return charSegments;
}
// Goes through the contours for the plate and picks out possible char segments based on min/max height
vector<bool> CharacterAnalysis::filterByBoxSize(vector< vector< Point> > contours, vector<bool> goodIndices, int minHeightPx, int maxHeightPx)
{
@@ -690,7 +628,6 @@ vector<bool> CharacterAnalysis::filterByBoxSize(vector< vector< Point> > contour
float idealAspect=config->charWidthMM / config->charHeightMM;
float aspecttolerance=0.25;
vector<bool> includedIndices(contours.size());
for (int j = 0; j < contours.size(); j++)
includedIndices.push_back(false);
@@ -706,7 +643,8 @@ vector<bool> CharacterAnalysis::filterByBoxSize(vector< vector< Point> > contour
float minWidth = mr.height * 0.2;
//Crop image
//Mat auxRoi(img, mr);
if(mr.height >= minHeightPx && mr.height <= maxHeightPx && mr.width > minWidth){
if(mr.height >= minHeightPx && mr.height <= maxHeightPx && mr.width > minWidth)
{
float charAspect= (float)mr.width/(float)mr.height;
@@ -719,8 +657,6 @@ vector<bool> CharacterAnalysis::filterByBoxSize(vector< vector< Point> > contour
}
vector< bool > CharacterAnalysis::filterContourHoles(vector< vector< Point > > contours, vector< Vec4i > hierarchy, vector< bool > goodIndices)
{
@@ -752,7 +688,6 @@ vector< bool > CharacterAnalysis::filterContourHoles(vector< vector< Point > > c
return includedIndices;
}
// Goes through the contours for the plate and picks out possible char segments based on min/max height
// returns a vector of indices corresponding to valid contours
vector<bool> CharacterAnalysis::filterByParentContour( vector< vector< Point> > contours, vector<Vec4i> hierarchy, vector<bool> goodIndices)
@@ -826,7 +761,6 @@ vector<bool> CharacterAnalysis::filterByParentContour( vector< vector< Point> >
return includedIndices;
}
vector<bool> CharacterAnalysis::filterBetweenLines(Mat img, vector<vector<Point> > contours, vector<Vec4i> hierarchy, vector<Point> outerPolygon, vector<bool> goodIndices)
{
static float MIN_AREA_PERCENT_WITHIN_LINES = 0.88;
@@ -835,7 +769,6 @@ vector<bool> CharacterAnalysis::filterBetweenLines(Mat img, vector<vector<Point>
for (int j = 0; j < contours.size(); j++)
includedIndices[j] = false;
if (outerPolygon.size() == 0)
return includedIndices;
@@ -855,7 +788,6 @@ vector<bool> CharacterAnalysis::filterBetweenLines(Mat img, vector<vector<Point>
Mat innerArea = Mat::zeros(img.size(), CV_8U);
fillConvexPoly(outerMask, outerPolygon.data(), outerPolygon.size(), Scalar(255,255,255));
for (int i = 0; i < contours.size(); i++)
{
if (goodIndices[i] == false)
@@ -871,10 +803,8 @@ vector<bool> CharacterAnalysis::filterBetweenLines(Mat img, vector<vector<Point>
0
);
bitwise_and(innerArea, outerMask, innerArea);
vector<vector<Point> > tempContours;
findContours(innerArea, tempContours,
CV_RETR_EXTERNAL, // retrieve the external contours
@@ -889,7 +819,6 @@ vector<bool> CharacterAnalysis::filterBetweenLines(Mat img, vector<vector<Point>
}
if (areaBetweenLines / totalArea >= MIN_AREA_PERCENT_WITHIN_LINES)
{
includedIndices[i] = true;
@@ -909,7 +838,6 @@ std::vector< bool > CharacterAnalysis::filterByOuterMask(vector< vector< Point >
if (hasPlateMask == false)
return goodIndices;
vector<bool> passingIndices;
for (int i = 0; i < goodIndices.size(); i++)
passingIndices.push_back(false);
@@ -952,8 +880,6 @@ std::vector< bool > CharacterAnalysis::filterByOuterMask(vector< vector< Point >
return passingIndices;
}
bool CharacterAnalysis::isPlateInverted()
{
@@ -964,14 +890,12 @@ bool CharacterAnalysis::isPlateInverted()
if (this->config->debugCharAnalysis)
cout << "CharacterAnalysis, plate inverted: MEAN: " << meanVal << " : " << bestThreshold.type() << endl;
if (meanVal[0] < 100) // Half would be 122.5. Give it a little extra oomf before saying it needs inversion. Most states aren't inverted.
return true;
return false;
}
bool CharacterAnalysis::verifySize(Mat r, float minHeightPx, float maxHeightPx)
{
//Char sizes 45x90
@@ -999,7 +923,6 @@ bool CharacterAnalysis::verifySize(Mat r, float minHeightPx, float maxHeightPx)
}
vector<Point> CharacterAnalysis::getCharArea()
{
const int MAX = 100000;

View File

@@ -28,19 +28,15 @@ CharacterRegion::CharacterRegion(Mat img, Config* config)
this->confidence = 0;
if (this->debug)
cout << "Starting CharacterRegion identification" << endl;
timespec startTime;
getTime(&startTime);
charAnalysis = new CharacterAnalysis(img, config);
charAnalysis->analyze();
if (this->debug)
{
vector<Mat> tempDash;
@@ -53,7 +49,6 @@ CharacterRegion::CharacterRegion(Mat img, Config* config)
tempDash.push_back(tmp);
}
Mat bestVal(charAnalysis->bestThreshold.size(), charAnalysis->bestThreshold.type());
charAnalysis->bestThreshold.copyTo(bestVal);
cvtColor(bestVal, bestVal, CV_GRAY2BGR);
@@ -69,8 +64,6 @@ CharacterRegion::CharacterRegion(Mat img, Config* config)
displayImage(config, "Character Region Step 1 Thresholds", drawImageDashboard(tempDash, bestVal.type(), 3));
}
if (this->debug)
{
/*
@@ -95,15 +88,12 @@ CharacterRegion::CharacterRegion(Mat img, Config* config)
cv::Scalar(0,255,0), // in green
1); // with a thickness of 1
displayImage(config, "Matching Contours", img_contours);
*/
}
//charsegments = this->getPossibleCharRegions(img_threshold, allContours, allHierarchy, STARTING_MIN_HEIGHT + (bestFitIndex * HEIGHT_STEP), STARTING_MAX_HEIGHT + (bestFitIndex * HEIGHT_STEP));
if (charAnalysis->linePolygon.size() > 0)
{
@@ -120,7 +110,6 @@ CharacterRegion::CharacterRegion(Mat img, Config* config)
else if (absangle > 1)
confidenceDrainers += (10 - absangle) * 5;
if (confidenceDrainers >= 100)
this->confidence=1;
else
@@ -128,8 +117,6 @@ CharacterRegion::CharacterRegion(Mat img, Config* config)
}
if (config->debugTiming)
{
timespec endTime;
@@ -144,8 +131,6 @@ CharacterRegion::~CharacterRegion()
delete(charAnalysis);
}
Mat CharacterRegion::getPlateMask()
{
return charAnalysis->plateMask;

View File

@@ -34,7 +34,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
timespec startTime;
getTime(&startTime);
Mat img_gray(img.size(), CV_8U);
cvtColor( img, img_gray, CV_BGR2GRAY );
@@ -43,22 +42,14 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
if (invertedColors)
bitwise_not(img_gray, img_gray);
charAnalysis = new CharacterAnalysis(img_gray, config);
charAnalysis->analyze();
if (this->config->debugCharSegmenter)
{
displayImage(config, "CharacterSegmenter Thresholds", drawImageDashboard(charAnalysis->thresholds, CV_8U, 3));
}
if (this->config->debugCharSegmenter)
{
@@ -93,8 +84,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
imgDbgGeneral.push_back(bordered);
}
if (charAnalysis->linePolygon.size() > 0)
{
this->top = LineSegment(charAnalysis->linePolygon[0].x, charAnalysis->linePolygon[0].y, charAnalysis->linePolygon[1].x, charAnalysis->linePolygon[1].y);
@@ -108,7 +97,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
if (charAnalysis->bestCharSegments[i] == false)
continue;
Rect mr = boundingRect(charAnalysis->bestContours[i]);
charWidths.push_back(mr.width);
@@ -122,26 +110,21 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
// Do the histogram analysis to figure out char regions
timespec startTime;
getTime(&startTime);
vector<Mat> allHistograms;
vector<Rect> allBoxes;
for (int i = 0; i < charAnalysis->allContours.size(); i++)
{
Mat histogramMask = Mat::zeros(charAnalysis->thresholds[i].size(), CV_8U);
fillConvexPoly(histogramMask, charAnalysis->linePolygon.data(), charAnalysis->linePolygon.size(), Scalar(255,255,255));
VerticalHistogram vertHistogram(charAnalysis->thresholds[i], histogramMask);
if (this->config->debugCharSegmenter)
{
Mat histoCopy(vertHistogram.histoImg.size(), vertHistogram.histoImg.type());
@@ -154,7 +137,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
float score = 0;
vector<Rect> charBoxes = getHistogramBoxes(vertHistogram, avgCharWidth, avgCharHeight, &score);
if (this->config->debugCharSegmenter)
{
for (int cboxIdx = 0; cboxIdx < charBoxes.size(); cboxIdx++)
@@ -171,7 +153,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
//drawAndWait(&histogramMask);
}
float medianCharWidth = avgCharWidth;
vector<int> widthValues;
// Compute largest char width
@@ -182,7 +163,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
medianCharWidth = median(widthValues.data(), widthValues.size());
if (config->debugTiming)
{
timespec endTime;
@@ -193,7 +173,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
//ColorFilter colorFilter(img, charAnalysis->getCharacterMask());
vector<Rect> candidateBoxes = getBestCharBoxes(charAnalysis->thresholds[0], allBoxes, medianCharWidth);
if (this->config->debugCharSegmenter)
{
// Setup the dashboard images to show the cleaning filters
@@ -211,7 +190,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
}
}
getTime(&startTime);
filterEdgeBoxes(charAnalysis->thresholds, candidateBoxes, medianCharWidth, avgCharHeight);
@@ -238,7 +216,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
if (this->config->debugCharSegmenter)
{
Mat imgDash = drawImageDashboard(charAnalysis->thresholds, CV_8U, 3);
displayImage(config, "Segmentation after cleaning", imgDash);
@@ -250,11 +227,6 @@ CharacterSegmenter::CharacterSegmenter(Mat img, bool invertedColors, Config* con
}
}
if (config->debugTiming)
{
timespec endTime;
@@ -268,11 +240,6 @@ CharacterSegmenter::~CharacterSegmenter()
delete charAnalysis;
}
// Given a histogram and the horizontal line boundaries, respond with an array of boxes where the characters are
// Scores the histogram quality as well based on num chars, char volume, and even separation
vector<Rect> CharacterSegmenter::getHistogramBoxes(VerticalHistogram histogram, float avgCharWidth, float avgCharHeight, float* score)
@@ -328,15 +295,12 @@ vector<Rect> CharacterSegmenter::getHistogramBoxes(VerticalHistogram histogram,
}
return charBoxes;
}
vector<Rect> CharacterSegmenter::getBestCharBoxes(Mat img, vector<Rect> charBoxes, float avgCharWidth)
{
float MAX_SEGMENT_WIDTH = avgCharWidth * 1.55;
// This histogram is based on how many char boxes (from ALL of the many thresholded images) are covering each column
@@ -344,7 +308,6 @@ vector<Rect> CharacterSegmenter::getBestCharBoxes(Mat img, vector<Rect> charBoxe
Mat histoImg = Mat::zeros(Size(img.cols, img.rows), CV_8U);
int columnCount;
for (int col = 0; col < img.cols; col++)
@@ -370,7 +333,6 @@ vector<Rect> CharacterSegmenter::getBestCharBoxes(Mat img, vector<Rect> charBoxe
float bestRowScore = 0;
vector<Rect> bestBoxes;
for (int row = 0; row < histoImg.rows; row++)
{
vector<Rect> validBoxes;
@@ -427,8 +389,6 @@ vector<Rect> CharacterSegmenter::getBestCharBoxes(Mat img, vector<Rect> charBoxe
}
}
if (rowScore > bestRowScore)
{
bestRowScore = rowScore;
@@ -437,8 +397,6 @@ vector<Rect> CharacterSegmenter::getBestCharBoxes(Mat img, vector<Rect> charBoxe
}
}
if (this->config->debugCharSegmenter)
{
cvtColor(histoImg, histoImg, CV_GRAY2BGR);
@@ -455,7 +413,6 @@ vector<Rect> CharacterSegmenter::getBestCharBoxes(Mat img, vector<Rect> charBoxe
}
return bestBoxes;
}
@@ -489,11 +446,9 @@ vector<Rect> CharacterSegmenter::get1DHits(Mat img, int yOffset)
}
return hits;
}
void CharacterSegmenter::removeSmallContours(vector<Mat> thresholds, vector<vector<vector<Point > > > allContours, float avgCharWidth, float avgCharHeight)
{
//const float MIN_CHAR_AREA = 0.02 * avgCharWidth * avgCharHeight; // To clear out the tiny specks
@@ -559,8 +514,6 @@ vector<Rect> CharacterSegmenter::combineCloseBoxes( vector<Rect> charBoxes, floa
newCharBoxes.push_back(charBoxes[i]);
}
}
return newCharBoxes;
@@ -575,7 +528,6 @@ void CharacterSegmenter::cleanCharRegions(vector<Mat> thresholds, vector<Rect> c
Mat mask = getCharBoxMask(thresholds[0], charRegions);
for (int i = 0; i < thresholds.size(); i++)
{
bitwise_and(thresholds[i], mask, thresholds[i]);
@@ -598,7 +550,6 @@ void CharacterSegmenter::cleanCharRegions(vector<Mat> thresholds, vector<Rect> c
const float MIN_SPECKLE_HEIGHT = ((float)charRegions[j].height) * MIN_SPECKLE_HEIGHT_PERCENT;
const float MIN_CONTOUR_AREA = ((float)charRegions[j].area()) * MIN_CONTOUR_AREA_PERCENT;
int tallestContourHeight = 0;
float totalArea = 0;
for (int c = 0; c < contours.size(); c++)
@@ -608,8 +559,6 @@ void CharacterSegmenter::cleanCharRegions(vector<Mat> thresholds, vector<Rect> c
if (charRegions[j].contains(contours[c][0]) == false)
continue;
Rect r = boundingRect(contours[c]);
if (r.height <= MIN_SPECKLE_HEIGHT || r.width <= MIN_SPECKLE_WIDTH_PX)
@@ -629,7 +578,6 @@ void CharacterSegmenter::cleanCharRegions(vector<Mat> thresholds, vector<Rect> c
totalArea += contourArea(contours[c]);
}
//else if (r.height > tallestContourHeight)
//{
@@ -639,8 +587,6 @@ void CharacterSegmenter::cleanCharRegions(vector<Mat> thresholds, vector<Rect> c
}
if (totalArea < MIN_CONTOUR_AREA)
{
// Character is not voluminous enough. Erase it.
@@ -652,7 +598,6 @@ void CharacterSegmenter::cleanCharRegions(vector<Mat> thresholds, vector<Rect> c
rectangle(imgDbgCleanStages[i], boxTop, COLOR_DEBUG_MIN_AREA, -1);
}
rectangle(thresholds[i], charRegions[j], Scalar(0, 0, 0), -1);
}
else if (tallestContourHeight < ((float) charRegions[j].height * MIN_CONTOUR_HEIGHT_PERCENT))
@@ -670,7 +615,6 @@ void CharacterSegmenter::cleanCharRegions(vector<Mat> thresholds, vector<Rect> c
}
Mat closureElement = getStructuringElement( 1,
Size( 2 + 1, 2+1 ),
Point( 1, 1 ) );
@@ -692,7 +636,6 @@ void CharacterSegmenter::cleanCharRegions(vector<Mat> thresholds, vector<Rect> c
}
void CharacterSegmenter::cleanBasedOnColor(vector<Mat> thresholds, Mat colorMask, vector<Rect> charRegions)
{
// If I knock out x% of the contour area from this thing (after applying the color filter)
@@ -710,8 +653,6 @@ void CharacterSegmenter::cleanBasedOnColor(vector<Mat> thresholds, Mat colorMask
bitwise_and(thresholds[i], boxChar, boxChar);
float meanBefore = mean(boxChar, boxChar)[0];
Mat thresholdCopy(thresholds[i].size(), CV_8U);
@@ -738,7 +679,6 @@ void CharacterSegmenter::cleanBasedOnColor(vector<Mat> thresholds, Mat colorMask
// Mat tmpx = drawImageDashboard(tmpDebug, tmpytmp.type(), 1);
//drawAndWait( &tmpx );
cout << "Segmentation Filter Clean by Color: Removed Threshold " << i << " charregion " << j << endl;
cout << "Segmentation Filter Clean by Color: before=" << meanBefore << " after=" << meanAfter << endl;
@@ -751,7 +691,6 @@ void CharacterSegmenter::cleanBasedOnColor(vector<Mat> thresholds, Mat colorMask
}
}
void CharacterSegmenter::cleanMostlyFullBoxes(vector<Mat> thresholds, const vector<Rect> charRegions)
{
float MAX_FILLED = 0.95 * 255;
@@ -825,7 +764,6 @@ vector<Rect> CharacterSegmenter::filterMostlyEmptyBoxes(vector<Mat> thresholds,
height = boundingRect(allPointsInBox).height;
}
if (height >= ((float) charRegions[j].height * MIN_CONTOUR_HEIGHT_PERCENT))
{
boxScores[j] = boxScores[j] + 1;
@@ -835,7 +773,6 @@ vector<Rect> CharacterSegmenter::filterMostlyEmptyBoxes(vector<Mat> thresholds,
drawX(imgDbgCleanStages[i], charRegions[j], COLOR_DEBUG_EMPTYFILTER, 3);
}
}
}
@@ -902,7 +839,6 @@ void CharacterSegmenter::filterEdgeBoxes(vector<Mat> thresholds, const vector<Re
//while (charBoxes.size() > 0 && charBoxes[0].width < MIN_SEGMENT_WIDTH_EDGES)
// charBoxes.erase(charBoxes.begin() + 0);
// TECHNIQUE #1
// Check for long vertical lines. Once the line is too long, mask the whole region
@@ -969,7 +905,6 @@ void CharacterSegmenter::filterEdgeBoxes(vector<Mat> thresholds, const vector<Re
col++;
}
if (leftEdgeX != 0)
leftEdges.push_back(leftEdgeX);
if (rightEdgeX != thresholds[i].cols)
@@ -1007,7 +942,6 @@ void CharacterSegmenter::filterEdgeBoxes(vector<Mat> thresholds, const vector<Re
warpAffine( mask, mask, rot_mat, mask.size() );
}
// If our edge mask covers more than x% of the char region, mask the whole thing...
const float MAX_COVERAGE_PERCENT = 0.6;
int leftCoveragePx = leftEdge - charRegions[0].x;
@@ -1034,7 +968,6 @@ void CharacterSegmenter::filterEdgeBoxes(vector<Mat> thresholds, const vector<Re
bitwise_and(thresholds[i], mask, thresholds[i]);
}
if (this->config->debugCharSegmenter)
{
cout << "Edge Filter: left=" << leftEdge << " right=" << rightEdge << endl;
@@ -1163,7 +1096,6 @@ int CharacterSegmenter::getLongestBlobLengthBetweenLines(Mat img, int col)
}
return longestBlobLength;
}
@@ -1183,7 +1115,6 @@ int CharacterSegmenter::isSkinnyLineInsideBox(Mat threshold, Rect box, vector<ve
Mat boxMask = Mat::zeros(threshold.size(), CV_8U);
rectangle(boxMask, slightlySmallerBox, Scalar(255, 255, 255), -1);
for (int i = 0; i < contours.size(); i++)
{
// Only bother with the big boxes
@@ -1215,7 +1146,6 @@ int CharacterSegmenter::isSkinnyLineInsideBox(Mat threshold, Rect box, vector<ve
if (tallestContourIdx != -1)
{
//cout << "Edge Filter: " << tallestContourHeight << " -- " << avgCharHeight << endl;
if (tallestContourHeight >= avgCharHeight * 0.9 &&
((tallestContourWidth < config->segmentationMinBoxWidthPx) || (tallestContourArea < avgCharWidth * avgCharHeight * 0.1)))
@@ -1230,8 +1160,6 @@ int CharacterSegmenter::isSkinnyLineInsideBox(Mat threshold, Rect box, vector<ve
return -1;
}
Mat CharacterSegmenter::getCharBoxMask(Mat img_threshold, vector<Rect> charBoxes)
{

View File

@@ -24,8 +24,7 @@
#define cJSON__h
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
/* cJSON Types: */
@@ -40,7 +39,8 @@ extern "C"
#define cJSON_IsReference 256
/* The cJSON structure: */
typedef struct cJSON {
typedef struct cJSON
{
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
@@ -53,7 +53,8 @@ typedef struct cJSON {
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
typedef struct cJSON_Hooks {
typedef struct cJSON_Hooks
{
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;

View File

@@ -17,11 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "colorfilter.h"
ColorFilter::ColorFilter(Mat image, Mat characterMask, Config* config)
{
@@ -32,7 +29,6 @@ ColorFilter::ColorFilter(Mat image, Mat characterMask, Config* config)
this->debug = config->debugColorFiler;
this->grayscale = imageIsGrayscale(image);
if (this->debug)
@@ -48,7 +44,6 @@ ColorFilter::ColorFilter(Mat image, Mat characterMask, Config* config)
findCharColors();
if (config->debugTiming)
{
timespec endTime;
@@ -120,8 +115,6 @@ void ColorFilter::findCharColors()
vector<Vec4i> hierarchy;
findContours(erodedCharMask, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
vector<float> hMeans, sMeans, vMeans;
vector<float> hStdDevs, sStdDevs, vStdDevs;
@@ -149,9 +142,6 @@ void ColorFilter::findCharColors()
hierarchy
);
//drawAndWait(&singleCharMask);
Scalar mean;
@@ -174,7 +164,6 @@ void ColorFilter::findCharColors()
sStdDevs.push_back(stddev[1]);
vStdDevs.push_back(stddev[2]);
}
if (hMeans.size() == 0)
@@ -184,11 +173,9 @@ void ColorFilter::findCharColors()
int bestSatIndex = this->getMajorityOpinion(sMeans, .65, 35);
int bestValIndex = this->getMajorityOpinion(vMeans, .65, 30);
if (sMeans[bestSatIndex] < MINIMUM_SATURATION)
return;
bool doHueFilter = false, doSatFilter = false, doValFilter = false;
float hueMin, hueMax;
float satMin, satMax;
@@ -259,8 +246,6 @@ void ColorFilter::findCharColors()
cout << "ColorFilter Val: " << bestValIndex << " : " << setw(7) << vMeans[bestValIndex] << " -- " << valMin << "-" << valMax << endl;
}
Mat imgDebugHueOnly = Mat::zeros(hsv.size(), hsv.type());
Mat imgDebug = Mat::zeros(hsv.size(), hsv.type());
Mat imgDistanceFromCenter = Mat::zeros(hsv.size(), CV_8U);
@@ -318,7 +303,6 @@ void ColorFilter::findCharColors()
else
this->colorMask.at<uchar>(row, col) = 0;
if ((hPasses && sPasses) || (hPasses && vPasses) || (sPasses && vPasses))
{
vDistance = pow(vDistance, 0.9);
@@ -333,8 +317,6 @@ void ColorFilter::findCharColors()
}
}
vector<Mat> debugImagesSet;
if (this->debug)
@@ -346,7 +328,6 @@ void ColorFilter::findCharColors()
debugImagesSet.push_back(addLabel(maskCopy, "color Mask Before"));
}
Mat bigElement = getStructuringElement( 1,
Size( 3 + 1, 3+1 ),
Point( 1, 1 ) );
@@ -378,15 +359,12 @@ void ColorFilter::findCharColors()
debugImagesSet.push_back(addLabel(debugMask, "COLOR Hues off"));
Mat dashboard = drawImageDashboard(debugImagesSet, imgDebugHueOnly.type(), 3);
displayImage(config, "Color Filter Images", dashboard);
}
}
// Goes through an array of values, picks the winner based on the highest percentage of other values that are within the maxValDifference
// Return -1 if it fails.
int ColorFilter::getMajorityOpinion(vector<float> values, float minPercentAgreement, float maxValDifference)

View File

@@ -19,7 +19,6 @@
#include "config.h"
Config::Config(const std::string country, const std::string runtimeBaseDir)
{
this->runtimeBaseDir = runtimeBaseDir;
@@ -93,12 +92,10 @@ void Config::loadValues(string country)
ocrImageWidthPx = round(((float) templateWidthPx) * ocrImagePercent);
ocrImageHeightPx = round(((float)templateHeightPx) * ocrImagePercent);
float stateIdImagePercent = getFloat("common", "state_id_img_size_percent", 100);
stateIdImageWidthPx = round(((float)templateWidthPx) * ocrImagePercent);
stateIdimageHeightPx = round(((float)templateHeightPx) * ocrImagePercent);
charAnalysisMinPercent = getFloat(country, "char_analysis_min_pct", 0);
charAnalysisHeightRange = getFloat(country, "char_analysis_height_range", 0);
charAnalysisHeightStepSize = getFloat(country, "char_analysis_height_step_size", 0);
@@ -150,7 +147,6 @@ void Config::debugOff()
debugPostProcess = false;
}
string Config::getCascadeRuntimeDir()
{
return this->runtimeBaseDir + CASCADE_DIR;
@@ -168,9 +164,6 @@ string Config::getTessdataPrefix()
return "TESSDATA_PREFIX=" + this->runtimeBaseDir + "/ocr/";
}
float Config::getFloat(string section, string key, float defaultValue)
{
const char * pszValue = ini->GetValue(section.c_str(), key.c_str(), NULL /*default*/);

View File

@@ -17,15 +17,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "featurematcher.h"
//const int DEFAULT_QUERY_FEATURES = 305;
//const int DEFAULT_TRAINING_FEATURES = 305;
const float MAX_DISTANCE_TO_MATCH = 100.0f;
FeatureMatcher::FeatureMatcher(Config* config)
{
this->config = config;
@@ -35,7 +32,6 @@ FeatureMatcher::FeatureMatcher(Config* config)
//this->descriptorMatcher = DescriptorMatcher::create( "FlannBased" );
this->detector = new FastFeatureDetector(10, true);
this->extractor = new BRISK(10, 1, 0.9);
}
@@ -52,7 +48,6 @@ FeatureMatcher::~FeatureMatcher()
}
bool FeatureMatcher::isLoaded()
{
if( detector.empty() || extractor.empty() || descriptorMatcher.empty() )
@@ -68,9 +63,6 @@ int FeatureMatcher::numTrainingElements()
return billMapping.size();
}
void FeatureMatcher::surfStyleMatching( const Mat& queryDescriptors, vector<KeyPoint> queryKeypoints,
vector<DMatch>& matches12 )
{
@@ -78,15 +70,12 @@ void FeatureMatcher::surfStyleMatching( const Mat& queryDescriptors, vector<KeyP
this->descriptorMatcher->radiusMatch(queryDescriptors, matchesKnn, MAX_DISTANCE_TO_MATCH);
vector<DMatch> tempMatches;
_surfStyleMatching(queryDescriptors, matchesKnn, tempMatches);
crisscrossFiltering(queryKeypoints, tempMatches, matches12);
}
void FeatureMatcher::_surfStyleMatching(const Mat& queryDescriptors, vector<vector<DMatch> > matchesKnn, vector<DMatch>& matches12)
{
@@ -138,7 +127,6 @@ void FeatureMatcher::_surfStyleMatching(const Mat& queryDescriptors, vector<vect
matches12.push_back(matchesKnn[descInd][0]);
}
//}
}
else if (matchesKnn[descInd].size() == 1)
@@ -156,11 +144,8 @@ void FeatureMatcher::_surfStyleMatching(const Mat& queryDescriptors, vector<vect
//matches12.push_back(match);
//}
}
}
// Compares the matches keypoints for parallel lines. Removes matches that are criss-crossing too much
@@ -196,8 +181,6 @@ void FeatureMatcher::crisscrossFiltering(const vector<KeyPoint> queryKeypoints,
matchIdx.push_back(j);
}
// Iterate through each line (n^2) removing the one with the most criss-crosses until there are none left.
int mostIntersections = 1;
while (mostIntersections > 0 && vlines.size() > 0)
@@ -251,7 +234,6 @@ void FeatureMatcher::crisscrossFiltering(const vector<KeyPoint> queryKeypoints,
}
// Returns true if successful, false otherwise
bool FeatureMatcher::loadRecognitionSet(string country)
{
@@ -259,7 +241,6 @@ bool FeatureMatcher::loadRecognitionSet(string country)
out << config->getKeypointsRuntimeDir() << "/" << country << "/";
string country_dir = out.str();
if (DirectoryExists(country_dir.c_str()))
{
vector<Mat> trainImages;
@@ -283,7 +264,6 @@ bool FeatureMatcher::loadRecognitionSet(string country)
return -1;
}
Mat descriptors;
vector<KeyPoint> keypoints;
@@ -299,7 +279,6 @@ bool FeatureMatcher::loadRecognitionSet(string country)
}
this->descriptorMatcher->add(trainImages);
this->descriptorMatcher->train();
@@ -310,8 +289,6 @@ bool FeatureMatcher::loadRecognitionSet(string country)
}
RecognitionResult FeatureMatcher::recognize( const Mat& queryImg, bool drawOnImage, Mat* outputImage,
bool debug_on, vector<int> debug_matches_array
)
@@ -326,8 +303,6 @@ RecognitionResult FeatureMatcher::recognize( const Mat& queryImg, bool drawOnIma
detector->detect( queryImg, queryKeypoints );
extractor->compute(queryImg, queryKeypoints, queryDescriptors);
if (queryKeypoints.size() <= 5)
{
// Cut it loose if there's less than 5 keypoints... nothing would ever match anyway and it could crash the matcher.
@@ -338,17 +313,17 @@ RecognitionResult FeatureMatcher::recognize( const Mat& queryImg, bool drawOnIma
return result;
}
vector<DMatch> filteredMatches;
surfStyleMatching( queryDescriptors, queryKeypoints, filteredMatches );
// Create and initialize the counts to 0
std::vector<int> bill_match_counts( billMapping.size() );
for (int i = 0; i < billMapping.size(); i++) { bill_match_counts[i] = 0; }
for (int i = 0; i < billMapping.size(); i++)
{
bill_match_counts[i] = 0;
}
for (int i = 0; i < filteredMatches.size(); i++)
{
@@ -356,8 +331,6 @@ RecognitionResult FeatureMatcher::recognize( const Mat& queryImg, bool drawOnIma
//if (filteredMatches[i].imgIdx
}
float max_count = 0; // represented as a percent (0 to 100)
int secondmost_count = 0;
int maxcount_index = -1;
@@ -380,7 +353,6 @@ RecognitionResult FeatureMatcher::recognize( const Mat& queryImg, bool drawOnIma
else if (score > 100)
score = 100;
if (score > 0)
{
result.haswinner = true;
@@ -427,5 +399,4 @@ RecognitionResult FeatureMatcher::recognize( const Mat& queryImg, bool drawOnIma
return result;
}

View File

@@ -38,7 +38,8 @@ using namespace std;
struct RecognitionResult {
struct RecognitionResult
{
bool haswinner;
string winner;
int confidence;

View File

@@ -19,7 +19,6 @@
#include "licenseplatecandidate.h"
LicensePlateCandidate::LicensePlateCandidate(Mat frame, Rect regionOfInterest, Config* config)
{
this->config = config;
@@ -38,7 +37,6 @@ void LicensePlateCandidate::recognize()
{
charSegmenter = NULL;
this->confidence = 0;
int expandX = round(this->plateRegion.width * 0.15);
@@ -46,19 +44,14 @@ void LicensePlateCandidate::recognize()
// expand box by 15% in all directions
Rect expandedRegion = expandRect( this->plateRegion, expandX, expandY, frame.cols, frame.rows) ;
Mat plate_bgr = Mat(frame, expandedRegion);
resize(plate_bgr, plate_bgr, Size(config->templateWidthPx, config->templateHeightPx));
Mat plate_bgr_cleaned = Mat(plate_bgr.size(), plate_bgr.type());
this->cleanupColors(plate_bgr, plate_bgr_cleaned);
CharacterRegion charRegion(plate_bgr, config);
if (charRegion.confidence > 10)
{
@@ -75,13 +68,10 @@ void LicensePlateCandidate::recognize()
{
this->plateCorners = transformPointsToOriginalImage(frame, plate_bgr, expandedRegion, smallPlateCorners);
this->deskewed = deSkewPlate(frame, this->plateCorners);
charSegmenter = new CharacterSegmenter(deskewed, charRegion.thresholdsInverted(), config);
//this->recognizedText = ocr->recognizedText;
//strcpy(this->recognizedText, ocr.recognizedText);
@@ -91,13 +81,8 @@ void LicensePlateCandidate::recognize()
charRegion.confidence = 0;
}
}
// Re-maps the coordinates from the smallImage to the coordinate space of the bigImage.
vector<Point2f> LicensePlateCandidate::transformPointsToOriginalImage(Mat bigImage, Mat smallImage, Rect region, vector<Point> corners)
{
@@ -116,7 +101,6 @@ vector<Point2f> LicensePlateCandidate::transformPointsToOriginalImage(Mat bigIma
return cornerPoints;
}
Mat LicensePlateCandidate::deSkewPlate(Mat inputImage, vector<Point2f> corners)
{
@@ -159,7 +143,6 @@ Mat LicensePlateCandidate::deSkewPlate(Mat inputImage, vector<Point2f> corners)
return deskewed;
}
void LicensePlateCandidate::cleanupColors(Mat inputImage, Mat outputImage)
{
if (this->config->debugGeneral)
@@ -190,10 +173,8 @@ void LicensePlateCandidate::cleanupColors(Mat inputImage, Mat outputImage)
//ycrcb.release();
}
bilateralFilter(intermediate, outputImage, 3, 25, 35);
if (this->config->debugGeneral)
{
displayImage(config, "After cleanup", outputImage);

View File

@@ -17,9 +17,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ocr.h"
OCR::OCR(Config* config)
@@ -36,7 +33,6 @@ OCR::OCR(Config* config)
strcpy(tessdataPrefix.data(), config->getTessdataPrefix().c_str());
putenv(tessdataPrefix.data());
tesseract->Init("", config->ocrLanguage.c_str() );
tesseract->SetVariable("save_blob_choices", "T");
//tesseract->SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNPQRSTUVWXYZ1234567890");
@@ -50,18 +46,14 @@ OCR::~OCR()
delete tesseract;
}
void OCR::performOCR(vector<Mat> thresholds, vector<Rect> charRegions)
{
timespec startTime;
getTime(&startTime);
postProcessor->clear();
for (int i = 0; i < thresholds.size(); i++)
{
@@ -69,7 +61,6 @@ void OCR::performOCR(vector<Mat> thresholds, vector<Rect> charRegions)
bitwise_not(thresholds[i], thresholds[i]);
tesseract->SetImage((uchar*) thresholds[i].data, thresholds[i].size().width, thresholds[i].size().height, thresholds[i].channels(), thresholds[i].step1());
for (int j = 0; j < charRegions.size(); j++)
{
Rect expandedRegion = expandRect( charRegions[j], 2, 2, thresholds[i].cols, thresholds[i].rows) ;
@@ -79,7 +70,8 @@ void OCR::performOCR(vector<Mat> thresholds, vector<Rect> charRegions)
tesseract::ResultIterator* ri = tesseract->GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_SYMBOL;
do {
do
{
const char* symbol = ri->GetUTF8Text(level);
float conf = ri->Confidence(level);
@@ -88,21 +80,21 @@ void OCR::performOCR(vector<Mat> thresholds, vector<Rect> charRegions)
int pointsize = 0;
const char* fontName = ri->WordFontAttributes(&dontcare, &dontcare, &dontcare, &dontcare, &dontcare, &dontcare, &pointsize, &fontindex);
if(symbol != 0 && pointsize >= config->ocrMinFontSize) {
if(symbol != 0 && pointsize >= config->ocrMinFontSize)
{
postProcessor->addLetter(*symbol, j, conf);
if (this->config->debugOcr)
printf("charpos%d: threshold %d: symbol %s, conf: %f font: %s (index %d) size %dpx", j, i, symbol, conf, fontName, fontindex, pointsize);
bool indent = false;
tesseract::ChoiceIterator ci(*ri);
do {
do
{
const char* choice = ci.GetUTF8Text();
postProcessor->addLetter(*choice, j, ci.Confidence());
//letterScores.addScore(*choice, j, ci.Confidence() - MIN_CONFIDENCE);
if (this->config->debugOcr)
{
@@ -112,19 +104,20 @@ void OCR::performOCR(vector<Mat> thresholds, vector<Rect> charRegions)
}
indent = true;
} while(ci.Next());
}
while(ci.Next());
}
if (this->config->debugOcr)
printf("---------------------------------------------\n");
delete[] symbol;
} while((ri->Next(level)));
}
while((ri->Next(level)));
delete ri;
}
}
if (config->debugTiming)
@@ -134,7 +127,4 @@ void OCR::performOCR(vector<Mat> thresholds, vector<Rect> charRegions)
cout << "OCR Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
}

View File

@@ -17,7 +17,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "platecorners.h"
PlateCorners::PlateCorners(Mat inputImage, PlateLines* plateLines, CharacterRegion* charRegion, Config* config)
@@ -27,8 +26,6 @@ PlateCorners::PlateCorners(Mat inputImage, PlateLines* plateLines, CharacterRegi
if (this->config->debugPlateCorners)
cout << "PlateCorners constructor" << endl;
this->inputImage = inputImage;
this->plateLines = plateLines;
this->charRegion = charRegion;
@@ -36,7 +33,6 @@ PlateCorners::PlateCorners(Mat inputImage, PlateLines* plateLines, CharacterRegi
this->bestHorizontalScore = 9999999999999;
this->bestVerticalScore = 9999999999999;
Point topPoint = charRegion->getTopLine().midpoint();
Point bottomPoint = charRegion->getBottomLine().closestPointOnSegmentTo(topPoint);
this->charHeight = distanceBetweenPoints(topPoint, bottomPoint);
@@ -65,7 +61,6 @@ vector<Point> PlateCorners::findPlateCorners()
int horizontalLines = this->plateLines->horizontalLines.size();
int verticalLines = this->plateLines->verticalLines.size();
// layout horizontal lines
for (int h1 = NO_LINE; h1 < horizontalLines; h1++)
{
@@ -89,7 +84,6 @@ vector<Point> PlateCorners::findPlateCorners()
}
}
if (this->config->debugPlateCorners)
{
@@ -100,15 +94,11 @@ vector<Point> PlateCorners::findPlateCorners()
for (int i = 0; i < 4; i++)
circle(imgCorners, charRegion->getCharArea()[i], 2, Scalar(0, 0, 0));
line(imgCorners, this->bestTop.p1, this->bestTop.p2, Scalar(255, 0, 0), 1, CV_AA);
line(imgCorners, this->bestRight.p1, this->bestRight.p2, Scalar(0, 0, 255), 1, CV_AA);
line(imgCorners, this->bestBottom.p1, this->bestBottom.p2, Scalar(0, 0, 255), 1, CV_AA);
line(imgCorners, this->bestLeft.p1, this->bestLeft.p2, Scalar(255, 0, 0), 1, CV_AA);
displayImage(config, "Winning top/bottom Boundaries", imgCorners);
}
@@ -127,8 +117,6 @@ vector<Point> PlateCorners::findPlateCorners()
corners.push_back(bestBottom.intersection(bestRight));
corners.push_back(bestBottom.intersection(bestLeft));
if (config->debugTiming)
{
timespec endTime;
@@ -139,7 +127,6 @@ vector<Point> PlateCorners::findPlateCorners()
return corners;
}
void PlateCorners::scoreVerticals(int v1, int v2)
{
@@ -148,8 +135,6 @@ void PlateCorners::scoreVerticals(int v1, int v2)
LineSegment left;
LineSegment right;
float charHeightToPlateWidthRatio = config->plateWidthMM / config->charHeightMM;
float idealPixelWidth = this->charHeight * (charHeightToPlateWidthRatio * 1.05); // Add 10% so we don't clip any characters
@@ -183,8 +168,6 @@ void PlateCorners::scoreVerticals(int v1, int v2)
score += SCORING_MISSING_SEGMENT_PENALTY_VERTICAL;
}
// Make sure this line is to the left of our license plate letters
if (left.isPointBelowLine(charRegion->getCharBoxLeft().midpoint()) == false)
return;
@@ -193,7 +176,6 @@ void PlateCorners::scoreVerticals(int v1, int v2)
if (right.isPointBelowLine(charRegion->getCharBoxRight().midpoint()))
return;
/////////////////////////////////////////////////////////////////////////
// Score "Distance from the edge...
/////////////////////////////////////////////////////////////////////////
@@ -204,7 +186,6 @@ void PlateCorners::scoreVerticals(int v1, int v2)
float distanceFromEdge = leftDistanceFromEdge + rightDistanceFromEdge;
score += distanceFromEdge * SCORING_VERTICALDISTANCE_FROMEDGE_WEIGHT;
/////////////////////////////////////////////////////////////////////////
// Score "Boxiness" of the 4 lines. How close is it to a parallelogram?
/////////////////////////////////////////////////////////////////////////
@@ -213,12 +194,10 @@ void PlateCorners::scoreVerticals(int v1, int v2)
score += (verticalAngleDiff) * SCORING_BOXINESS_WEIGHT;
//////////////////////////////////////////////////////////////////////////
// SCORE the shape wrt character position and height relative to position
//////////////////////////////////////////////////////////////////////////
Point leftMidLinePoint = left.closestPointOnSegmentTo(charRegion->getCharBoxLeft().midpoint());
Point rightMidLinePoint = right.closestPointOnSegmentTo(charRegion->getCharBoxRight().midpoint());
@@ -230,7 +209,6 @@ void PlateCorners::scoreVerticals(int v1, int v2)
{
float scorecomponent;
if (this->config->debugPlateCorners)
{
cout << "xx xx Score: charHeight " << this->charHeight << endl;
@@ -277,7 +255,6 @@ void PlateCorners::scoreHorizontals(int h1, int h2)
float charHeightToPlateHeightRatio = config->plateHeightMM / config->charHeightMM;
float idealPixelHeight = this->charHeight * charHeightToPlateHeightRatio;
if (h1 == NO_LINE && h2 == NO_LINE)
{
// return;
@@ -308,39 +285,26 @@ void PlateCorners::scoreHorizontals(int h1, int h2)
score += SCORING_MISSING_SEGMENT_PENALTY_HORIZONTAL;
}
// Make sure this line is above our license plate letters
if (top.isPointBelowLine(charRegion->getCharBoxTop().midpoint()) == false)
return;
// Make sure this line is below our license plate letters
if (bottom.isPointBelowLine(charRegion->getCharBoxBottom().midpoint()))
return;
// We now have 4 possible lines. Let's put them to the test and score them...
/////////////////////////////////////////////////////////////////////////
// Score "Boxiness" of the 4 lines. How close is it to a parallelogram?
/////////////////////////////////////////////////////////////////////////
float horizontalAngleDiff = abs(top.angle - bottom.angle);
score += (horizontalAngleDiff) * SCORING_BOXINESS_WEIGHT;
// if (this->debug)
// cout << "PlateCorners boxiness score: " << (horizontalAngleDiff + verticalAngleDiff) * SCORING_BOXINESS_WEIGHT << endl;
//////////////////////////////////////////////////////////////////////////
// SCORE the shape wrt character position and height relative to position
//////////////////////////////////////////////////////////////////////////
@@ -351,7 +315,6 @@ void PlateCorners::scoreHorizontals(int h1, int h2)
// Get the height difference
float heightRatio = charHeight / plateHeightPx;
float idealHeightRatio = (config->charHeightMM / config->plateHeightMM);
//if (leftRatio < MIN_CHAR_HEIGHT_RATIO || leftRatio > MAX_CHAR_HEIGHT_RATIO || rightRatio < MIN_CHAR_HEIGHT_RATIO || rightRatio > MAX_CHAR_HEIGHT_RATIO)
@@ -367,7 +330,6 @@ void PlateCorners::scoreHorizontals(int h1, int h2)
// float idealBottomDistance = charHeight * (BOTTOM_WHITESPACE_HEIGHT_MM / CHARACTER_HEIGHT_MM);
// float distScore = abs(topDistance - idealTopDistance) + abs(bottomDistance - idealBottomDistance);
score += heightRatioDiff * SCORING_PLATEHEIGHT_WEIGHT;
//////////////////////////////////////////////////////////////////////////
@@ -398,14 +360,11 @@ void PlateCorners::scoreHorizontals(int h1, int h2)
float charanglediff = abs(charAngle - top.angle) + abs(charAngle - bottom.angle);
score += charanglediff * SCORING_ANGLE_MATCHES_LPCHARS_WEIGHT;
// if (this->debug)
// cout << "PlateCorners boxiness score: " << charanglediff * SCORING_ANGLE_MATCHES_LPCHARS_WEIGHT << endl;
if (score < this->bestHorizontalScore)
{
float scorecomponent;
@@ -442,6 +401,4 @@ void PlateCorners::scoreHorizontals(int h1, int h2)
bestBottom = LineSegment(bottom.p1.x, bottom.p1.y, bottom.p2.x, bottom.p2.y);
}
}

View File

@@ -19,7 +19,6 @@
#include "platelines.h"
PlateLines::PlateLines(Config* config)
{
this->config = config;
@@ -28,7 +27,6 @@ PlateLines::PlateLines(Config* config)
if (debug)
cout << "PlateLines constructor" << endl;
}
PlateLines::~PlateLines()
@@ -36,18 +34,14 @@ PlateLines::~PlateLines()
}
void PlateLines::processImage(Mat inputImage, float sensitivity)
{
if (this->debug)
cout << "PlateLines findLines" << endl;
timespec startTime;
getTime(&startTime);
Mat smoothed(inputImage.size(), inputImage.type());
inputImage.copyTo(smoothed);
int morph_elem = 2;
@@ -65,12 +59,9 @@ void PlateLines::processImage(Mat inputImage, float sensitivity)
element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
morphologyEx( smoothed, smoothed, MORPH_OPEN, element );
Mat edges(inputImage.size(), inputImage.type());
Canny(smoothed, edges, 66, 133);
vector<LineSegment> hlines = this->getLines(edges, sensitivity, false);
vector<LineSegment> vlines = this->getLines(edges, sensitivity, true);
for (int i = 0; i < hlines.size(); i++)
@@ -78,9 +69,6 @@ void PlateLines::processImage(Mat inputImage, float sensitivity)
for (int i = 0; i < vlines.size(); i++)
this->verticalLines.push_back(vlines[i]);
// if debug is enabled, draw the image
if (this->debug)
{
@@ -109,8 +97,6 @@ void PlateLines::processImage(Mat inputImage, float sensitivity)
displayImage(config, "Hough Lines", dashboard);
}
if (config->debugTiming)
{
timespec endTime;
@@ -119,7 +105,6 @@ void PlateLines::processImage(Mat inputImage, float sensitivity)
}
//smoothed.release();
//////////////// METHOD2!!!!!!!////////////////////
/*
@@ -127,18 +112,13 @@ void PlateLines::processImage(Mat inputImage, float sensitivity)
Mat imgCanny;
GaussianBlur(inputImage, imgBlur, Size(9, 9), 1, 1);
Canny(imgBlur, imgCanny, 10, 30, 3);
//int morph_elem = 2;
//int morph_size = 1;
//Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
morphologyEx( imgCanny, imgCanny, MORPH_CLOSE, element );
Mat imgShaped;
imgCanny.copyTo(imgShaped);
//Find contours of possibles characters
@@ -187,7 +167,6 @@ vector<LineSegment> PlateLines::getLines(Mat edges, bool vertical)
vector<double> errors;
lswms.run(edges, lsegs, errors);
for( size_t i = 0; i < lsegs.size(); i++ )
{
@@ -247,7 +226,6 @@ vector<LineSegment> PlateLines::getLines(Mat edges, bool vertical)
}
*/
vector<LineSegment> PlateLines::getLines(Mat edges, float sensitivityMultiplier, bool vertical)
{
if (this->debug)
@@ -267,7 +245,6 @@ vector<LineSegment> PlateLines::getLines(Mat edges, float sensitivityMultiplier,
HoughLines( edges, allLines, 1, CV_PI/180, sensitivity, 0, 0 );
for( size_t i = 0; i < allLines.size(); i++ )
{
float rho = allLines[i][0], theta = allLines[i][1];
@@ -325,20 +302,14 @@ vector<LineSegment> PlateLines::getLines(Mat edges, float sensitivityMultiplier,
}
}
return filteredLines;
}
Mat PlateLines::customGrayscaleConversion(Mat src)
{
Mat img_hsv;
cvtColor(src,img_hsv,CV_BGR2HSV);
Mat grayscale = Mat(img_hsv.size(), CV_8U );
Mat hue(img_hsv.size(), CV_8U );
@@ -352,7 +323,6 @@ Mat PlateLines::customGrayscaleConversion(Mat src)
int pixval = pow(v, 1.05);
if (pixval > 255)
pixval = 255;
grayscale.at<uchar>(row, col) = pixval;

View File

@@ -19,7 +19,6 @@
#include "postprocess.h"
PostProcess::PostProcess(Config* config)
{
this->config = config;
@@ -29,7 +28,6 @@ PostProcess::PostProcess(Config* config)
std::ifstream infile(filename.str().c_str());
string region, pattern;
while (infile >> region >> pattern)
{
@@ -61,7 +59,8 @@ PostProcess::~PostProcess()
// TODO: Delete all entries in rules vector
map<string, vector<RegexRule*> >::iterator iter;
for (iter = rules.begin(); iter != rules.end(); ++iter) {
for (iter = rules.begin(); iter != rules.end(); ++iter)
{
for (int i = 0; i < iter->second.size(); i++)
{
delete iter->second[i];
@@ -70,7 +69,6 @@ PostProcess::~PostProcess()
}
}
void PostProcess::addLetter(char letter, int charposition, float score)
{
if (score < config->postProcessMinConfidence)
@@ -96,7 +94,6 @@ void PostProcess::insertLetter(char letter, int charposition, float score)
score = score - config->postProcessMinConfidence;
int existingIndex = -1;
if (letters.size() < charposition + 1)
{
@@ -134,7 +131,6 @@ void PostProcess::insertLetter(char letter, int charposition, float score)
}
void PostProcess::clear()
{
for (int i = 0; i < letters.size(); i++)
@@ -157,8 +153,6 @@ void PostProcess::analyze(string templateregion, int topn)
timespec startTime;
getTime(&startTime);
// Get a list of missing positions
for (int i = letters.size() -1; i >= 0; i--)
{
@@ -168,11 +162,9 @@ void PostProcess::analyze(string templateregion, int topn)
}
}
if (letters.size() == 0)
return;
// Sort the letters as they are
for (int i = 0; i < letters.size(); i++)
{
@@ -180,8 +172,6 @@ void PostProcess::analyze(string templateregion, int topn)
sort(letters[i].begin(), letters[i].end(), letterCompare);
}
if (this->config->debugPostProcess)
{
@@ -211,7 +201,6 @@ void PostProcess::analyze(string templateregion, int topn)
vector<Letter> tmp;
findAllPermutations(tmp, 0, config->postProcessMaxSubstitutions);
timespec sortStartTime;
getTime(&sortStartTime);
@@ -228,11 +217,8 @@ void PostProcess::analyze(string templateregion, int topn)
cout << " -- PostProcess Sort Time: " << diffclock(sortStartTime, sortEndTime) << "ms." << endl;
}
matchesTemplate = false;
if (templateregion != "")
{
vector<RegexRule*> regionRules = rules[templateregion];
@@ -251,8 +237,6 @@ void PostProcess::analyze(string templateregion, int topn)
}
}
if (i >= topn - 1)
break;
//if (matchesTemplate || i >= TOP_N - 1)
@@ -289,12 +273,9 @@ void PostProcess::analyze(string templateregion, int topn)
}
if (this->config->debugPostProcess)
{
// Print top words
for (int i = 0; i < allPossibilities.size(); i++)
{
@@ -309,8 +290,6 @@ void PostProcess::analyze(string templateregion, int topn)
cout << allPossibilities.size() << " total permutations" << endl;
}
if (config->debugTiming)
{
timespec endTime;
@@ -372,7 +351,6 @@ vector<int> PostProcess::getMaxDepth(int topn)
nextLeastDropCharPos = getNextLeastDrop(depth);
}
return depth;
}
@@ -465,14 +443,10 @@ void PostProcess::findAllPermutations(vector<Letter> prevletters, int charPos, i
findAllPermutations(prevletters, charPos + 1, substitutionsLeft);
}
}
bool wordCompare( const PPResult &left, const PPResult &right ){
bool wordCompare( const PPResult &left, const PPResult &right )
{
if (left.totalscore < right.totalscore)
return false;
return true;
@@ -486,7 +460,6 @@ bool letterCompare( const Letter &left, const Letter &right )
return true;
}
RegexRule::RegexRule(string region, string pattern)
{
this->original = pattern;
@@ -529,7 +502,6 @@ RegexRule::RegexRule(string region, string pattern)
// cout << "AA Skip position: " << skipPositions[z] << endl;
}
bool RegexRule::match(string text)
{
if (text.length() != numchars)

View File

@@ -17,11 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "regiondetector.h"
RegionDetector::RegionDetector(Config* config)
{
this->config = config;
@@ -38,7 +35,6 @@ RegionDetector::RegionDetector(Config* config)
this->plate_cascade = new CascadeClassifier();
}
if( this->plate_cascade->load( config->getCascadeRuntimeDir() + config->country + ".xml" ) )
{
this->loaded = true;
@@ -49,8 +45,6 @@ RegionDetector::RegionDetector(Config* config)
printf("--(!)Error loading classifier\n");
}
}
RegionDetector::~RegionDetector()
@@ -58,14 +52,11 @@ RegionDetector::~RegionDetector()
delete this->plate_cascade;
}
bool RegionDetector::isLoaded()
{
return this->loaded;
}
vector<Rect> RegionDetector::detect(Mat frame)
{
@@ -77,7 +68,6 @@ vector<Rect> RegionDetector::detect(Mat frame)
return regionsOfInterest;
}
/** @function detectAndDisplay */
vector<Rect> RegionDetector::doCascade(Mat frame)
{
@@ -111,7 +101,6 @@ vector<Rect> RegionDetector::doCascade(Mat frame)
minSize, maxSize );
}
if (config->debugTiming)
{
timespec endTime;
@@ -119,8 +108,6 @@ vector<Rect> RegionDetector::doCascade(Mat frame)
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
for( int i = 0; i < plates.size(); i++ )
{
plates[i].x = plates[i].x / scale_factor;

View File

@@ -99,14 +99,16 @@ typedef unsigned char Boolean; /* 0 or 1 */
#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
typedef enum {
typedef enum
{
conversionOK, /* conversion successful */
sourceExhausted, /* partial character in source, but hit end */
targetExhausted, /* insuff. room in target for conversion */
sourceIllegal /* source sequence is illegal/malformed */
} ConversionResult;
typedef enum {
typedef enum
{
strictConversion = 0,
lenientConversion
} ConversionFlags;

File diff suppressed because it is too large Load Diff

View File

@@ -74,7 +74,8 @@ snippets(
// output all of the items
CSimpleIniA::TNamesDepend::const_iterator i;
for (i = values.begin(); i != values.end(); ++i) {
for (i = values.begin(); i != values.end(); ++i)
{
printf("key-name = '%s'\n", i->pItem);
}

View File

@@ -19,7 +19,6 @@
#include "stateidentifier.h"
StateIdentifier::StateIdentifier(Config* config)
{
this->config = config;
@@ -66,19 +65,16 @@ int StateIdentifier::recognize(Mat img, char* stateCode)
plateImg.copyTo(debugImg);
vector<int> matchesArray(featureMatcher->numTrainingElements());
RecognitionResult result = featureMatcher->recognize(plateImg, true, &debugImg, true, matchesArray );
if (this->config->debugStateId)
{
displayImage(config, "State Identifier1", plateImg);
displayImage(config, "State Identifier", debugImg);
cout << result.haswinner << " : " << result.confidence << " : " << result.winner << endl;
}
if (config->debugTiming)
{
timespec endTime;
@@ -86,12 +82,10 @@ int StateIdentifier::recognize(Mat img, char* stateCode)
cout << "State Identification Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
if (result.haswinner == false)
return 0;
strcpy(stateCode, result.winner.c_str());
return result.confidence;
}

View File

@@ -1,12 +1,13 @@
#include "filesystem.h"
bool hasEnding (std::string const &fullString, std::string const &ending)
{
if (fullString.length() >= ending.length()) {
if (fullString.length() >= ending.length())
{
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
}
else
{
return false;
}
}
@@ -47,14 +48,18 @@ std::vector<std::string> getFilesInDir(const char* dirPath)
std::vector<std::string> files;
struct dirent *ent;
if ((dir = opendir (dirPath)) != NULL) {
if ((dir = opendir (dirPath)) != NULL)
{
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
while ((ent = readdir (dir)) != NULL)
{
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0)
files.push_back(ent->d_name);
}
closedir (dir);
} else {
}
else
{
/* could not open directory */
perror ("");
return files;
@@ -63,8 +68,8 @@ std::vector<std::string> getFilesInDir(const char* dirPath)
return files;
}
bool stringCompare( const std::string &left, const std::string &right ){
bool stringCompare( const std::string &left, const std::string &right )
{
for( std::string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit )
if( tolower( *lit ) < tolower( *rit ) )
return true;

View File

@@ -1,9 +1,7 @@
#include "timing.h"
timespec diff(timespec start, timespec end);
#ifdef WINDOWS
// Windows timing code
@@ -37,20 +35,25 @@ int clock_gettime(int X, timespec *tv)
static int initialized = 0;
static BOOL usePerformanceCounter = 0;
if (!initialized) {
if (!initialized)
{
LARGE_INTEGER performanceFrequency;
initialized = 1;
usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
if (usePerformanceCounter) {
if (usePerformanceCounter)
{
QueryPerformanceCounter(&offset);
frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
} else {
}
else
{
offset = getFILETIMEoffset();
frequencyToMicroseconds = 10.;
}
}
if (usePerformanceCounter) QueryPerformanceCounter(&t);
else {
else
{
GetSystemTimeAsFileTime(&f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
@@ -65,8 +68,6 @@ int clock_gettime(int X, timespec *tv)
return (0);
}
void getTime(timespec* time)
{
clock_gettime(0, time);
@@ -77,17 +78,19 @@ double diffclock(timespec time1,timespec time2)
timespec delta = diff(time1,time2);
double milliseconds = (delta.tv_sec * 1000) + (((double) delta.tv_usec) / 1000.0);
return milliseconds;
}
timespec diff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_usec-start.tv_usec)<0) {
if ((end.tv_usec-start.tv_usec)<0)
{
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_usec = 1000000+end.tv_usec-start.tv_usec;
} else {
}
else
{
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_usec = end.tv_usec-start.tv_usec;
}
@@ -96,8 +99,6 @@ timespec diff(timespec start, timespec end)
#else
void getTime(timespec* time)
{
@@ -120,24 +121,24 @@ double diffclock(timespec time1,timespec time2)
timespec delta = diff(time1,time2);
double milliseconds = (delta.tv_sec * 1000) + (((double) delta.tv_nsec) / 1000000.0);
return milliseconds;
}
timespec diff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
if ((end.tv_nsec-start.tv_nsec)<0)
{
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
}
else
{
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
#endif

View File

@@ -126,7 +126,6 @@ int tinydir_open(tinydir_dir *dir, const char *path)
errno = ENAMETOOLONG;
return -1;
}
/* initialise dir */
dir->_files = NULL;
#ifdef _MSC_VER
@@ -135,7 +134,6 @@ int tinydir_open(tinydir_dir *dir, const char *path)
dir->_d = NULL;
#endif
tinydir_close(dir);
strcpy(dir->path, path);
#ifdef _MSC_VER
strcat(dir->path, "\\*");
@@ -150,7 +148,6 @@ int tinydir_open(tinydir_dir *dir, const char *path)
errno = ENOENT;
goto bail;
}
/* read first file */
dir->has_next = 1;
#ifndef _MSC_VER
@@ -160,9 +157,7 @@ int tinydir_open(tinydir_dir *dir, const char *path)
dir->has_next = 0;
}
#endif
return 0;
bail:
tinydir_close(dir);
return -1;
@@ -175,7 +170,6 @@ int tinydir_open_sorted(tinydir_dir *dir, const char *path)
{
return -1;
}
dir->n_files = 0;
while (dir->has_next)
{
@@ -187,23 +181,18 @@ int tinydir_open_sorted(tinydir_dir *dir, const char *path)
errno = ENOMEM;
goto bail;
}
p_file = &dir->_files[dir->n_files - 1];
if (tinydir_readfile(dir, p_file) == -1)
{
goto bail;
}
if (tinydir_next(dir) == -1)
{
goto bail;
}
}
qsort(dir->_files, dir->n_files, sizeof(tinydir_file), _tinydir_file_cmp);
return 0;
bail:
tinydir_close(dir);
return -1;
@@ -216,7 +205,6 @@ void tinydir_close(tinydir_dir *dir)
{
return;
}
memset(dir->path, 0, sizeof(dir->path));
dir->has_next = 0;
dir->n_files = -1;
@@ -254,7 +242,6 @@ int tinydir_next(tinydir_dir *dir)
errno = ENOENT;
return -1;
}
#ifdef _MSC_VER
if (FindNextFile(dir->_h, &dir->_f) == 0)
#else
@@ -273,7 +260,6 @@ int tinydir_next(tinydir_dir *dir)
}
#endif
}
return 0;
}
@@ -319,7 +305,6 @@ int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file)
errno = ENAMETOOLONG;
return -1;
}
strcpy(file->path, dir->path);
strcat(file->path, "/");
strcpy(file->name,
@@ -360,7 +345,6 @@ int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file)
#else
S_ISREG(file->_s.st_mode);
#endif
return 0;
}
@@ -377,9 +361,7 @@ int tinydir_readfile_n(const tinydir_dir *dir, tinydir_file *file, int i)
errno = ENOENT;
return -1;
}
memcpy(file, &dir->_files[i], sizeof(tinydir_file));
return 0;
}
@@ -397,14 +379,12 @@ int tinydir_open_subdir_n(tinydir_dir *dir, int i)
errno = ENOENT;
return -1;
}
strcpy(path, dir->_files[i].path);
tinydir_close(dir);
if (tinydir_open_sorted(dir, path) == -1)
{
return -1;
}
return 0;
}

View File

@@ -219,7 +219,8 @@ extern "C" {
/* Wide-character version */
struct _wdirent {
struct _wdirent
{
long d_ino; /* Always zero */
unsigned short d_reclen; /* Structure size */
size_t d_namlen; /* Length of name without \0 */
@@ -228,7 +229,8 @@ struct _wdirent {
};
typedef struct _wdirent _wdirent;
struct _WDIR {
struct _WDIR
{
struct _wdirent ent; /* Current directory entry */
WIN32_FIND_DATAW data; /* Private file data */
int cached; /* True if data is valid */
@@ -253,7 +255,8 @@ static void _wrewinddir (_WDIR* dirp);
/* Multi-byte character versions */
struct dirent {
struct dirent
{
long d_ino; /* Always zero */
unsigned short d_reclen; /* Structure size */
size_t d_namlen; /* Length of name without \0 */
@@ -262,7 +265,8 @@ struct dirent {
};
typedef struct dirent dirent;
struct DIR {
struct DIR
{
struct dirent ent;
struct _WDIR *wdirp;
};
@@ -305,50 +309,49 @@ _wopendir(
{
_WDIR *dirp = NULL;
int error;
/* Must have directory name */
if (dirname == NULL || dirname[0] == '\0') {
if (dirname == NULL || dirname[0] == '\0')
{
dirent_set_errno (ENOENT);
return NULL;
}
/* Allocate new _WDIR structure */
dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
if (dirp != NULL) {
if (dirp != NULL)
{
DWORD n;
/* Reset _WDIR structure */
dirp->handle = INVALID_HANDLE_VALUE;
dirp->patt = NULL;
dirp->cached = 0;
/* Compute the length of full path plus zero terminator */
n = GetFullPathNameW (dirname, 0, NULL, NULL);
/* Allocate room for absolute directory name and search pattern */
dirp->patt = (wchar_t*) malloc (sizeof (wchar_t) * n + 16);
if (dirp->patt) {
if (dirp->patt)
{
/*
* Convert relative directory name to an absolute one. This
* allows rewinddir() to function correctly even when current
* working directory is changed between opendir() and rewinddir().
*/
n = GetFullPathNameW (dirname, n, dirp->patt, NULL);
if (n > 0) {
if (n > 0)
{
wchar_t *p;
/* Append search pattern \* to the directory name */
p = dirp->patt + n;
if (dirp->patt < p) {
switch (p[-1]) {
if (dirp->patt < p)
{
switch (p[-1])
{
case '\\':
case '/':
case ':':
/* Directory ends in path separator, e.g. c:\temp\ */
/*NOP*/;
/*NOP*/
;
break;
default:
/* Directory name doesn't end in path separator */
*p++ = '\\';
@@ -356,39 +359,43 @@ _wopendir(
}
*p++ = '*';
*p = '\0';
/* Open directory stream and retrieve the first entry */
if (dirent_first (dirp)) {
if (dirent_first (dirp))
{
/* Directory stream opened successfully */
error = 0;
} else {
}
else
{
/* Cannot retrieve first entry */
error = 1;
dirent_set_errno (ENOENT);
}
} else {
}
else
{
/* Cannot retrieve full path name */
dirent_set_errno (ENOENT);
error = 1;
}
} else {
}
else
{
/* Cannot allocate memory for search pattern */
error = 1;
}
} else {
}
else
{
/* Cannot allocate _WDIR structure */
error = 1;
}
/* Clean up in case of error */
if (error && dirp) {
if (error && dirp)
{
_wclosedir (dirp);
dirp = NULL;
}
return dirp;
}
@@ -407,47 +414,48 @@ _wreaddir(
/* Read next directory entry */
datap = dirent_next (dirp);
if (datap) {
if (datap)
{
size_t n;
DWORD attr;
/* Pointer to directory entry to return */
entp = &dirp->ent;
/*
* Copy file name as wide-character string. If the file name is too
* long to fit in to the destination buffer, then truncate file name
* to PATH_MAX characters and zero-terminate the buffer.
*/
n = 0;
while (n < PATH_MAX && datap->cFileName[n] != 0) {
while (n < PATH_MAX && datap->cFileName[n] != 0)
{
entp->d_name[n] = datap->cFileName[n];
n++;
}
dirp->ent.d_name[n] = 0;
/* Length of file name excluding zero terminator */
entp->d_namlen = n;
/* File type */
attr = datap->dwFileAttributes;
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0)
{
entp->d_type = DT_CHR;
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
}
else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
entp->d_type = DT_DIR;
} else {
}
else
{
entp->d_type = DT_REG;
}
/* Reset dummy fields */
entp->d_ino = 0;
entp->d_reclen = sizeof (struct _wdirent);
} else {
}
else
{
/* Last directory entry read */
entp = NULL;
}
return entp;
@@ -463,25 +471,26 @@ _wclosedir(
_WDIR *dirp)
{
int ok;
if (dirp) {
if (dirp)
{
/* Release search handle */
if (dirp->handle != INVALID_HANDLE_VALUE) {
if (dirp->handle != INVALID_HANDLE_VALUE)
{
FindClose (dirp->handle);
dirp->handle = INVALID_HANDLE_VALUE;
}
/* Release search pattern */
if (dirp->patt) {
if (dirp->patt)
{
free (dirp->patt);
dirp->patt = NULL;
}
/* Release directory structure */
free (dirp);
ok = /*success*/0;
} else {
}
else
{
/* Invalid directory stream */
dirent_set_errno (EBADF);
ok = /*failure*/-1;
@@ -497,12 +506,13 @@ static void
_wrewinddir(
_WDIR* dirp)
{
if (dirp) {
if (dirp)
{
/* Release existing search handle */
if (dirp->handle != INVALID_HANDLE_VALUE) {
if (dirp->handle != INVALID_HANDLE_VALUE)
{
FindClose (dirp->handle);
}
/* Open new search handle */
dirent_first (dirp);
}
@@ -514,21 +524,19 @@ dirent_first(
_WDIR *dirp)
{
WIN32_FIND_DATAW *datap;
/* Open directory and retrieve the first entry */
dirp->handle = FindFirstFileW (dirp->patt, &dirp->data);
if (dirp->handle != INVALID_HANDLE_VALUE) {
if (dirp->handle != INVALID_HANDLE_VALUE)
{
/* a directory entry is now waiting in memory */
datap = &dirp->data;
dirp->cached = 1;
} else {
}
else
{
/* Failed to re-open directory: no directory entry in memory */
dirp->cached = 0;
datap = NULL;
}
return datap;
}
@@ -539,34 +547,34 @@ dirent_next(
_WDIR *dirp)
{
WIN32_FIND_DATAW *p;
/* Get next directory entry */
if (dirp->cached != 0) {
if (dirp->cached != 0)
{
/* A valid directory entry already in memory */
p = &dirp->data;
dirp->cached = 0;
} else if (dirp->handle != INVALID_HANDLE_VALUE) {
}
else if (dirp->handle != INVALID_HANDLE_VALUE)
{
/* Get the next directory entry from stream */
if (FindNextFileW (dirp->handle, &dirp->data) != FALSE) {
if (FindNextFileW (dirp->handle, &dirp->data) != FALSE)
{
/* Got a file */
p = &dirp->data;
} else {
}
else
{
/* The very last entry has been processed or an error occured */
FindClose (dirp->handle);
dirp->handle = INVALID_HANDLE_VALUE;
p = NULL;
}
} else {
}
else
{
/* End of directory stream reached */
p = NULL;
}
return p;
}
@@ -579,35 +587,38 @@ opendir(
{
struct DIR *dirp;
int error;
/* Must have directory name */
if (dirname == NULL || dirname[0] == '\0') {
if (dirname == NULL || dirname[0] == '\0')
{
dirent_set_errno (ENOENT);
return NULL;
}
/* Allocate memory for DIR structure */
dirp = (DIR*) malloc (sizeof (struct DIR));
if (dirp) {
if (dirp)
{
wchar_t wname[PATH_MAX + 1];
size_t n;
/* Convert directory name to wide-character string */
error = dirent_mbstowcs_s(
&n, wname, PATH_MAX + 1, dirname, PATH_MAX);
if (!error) {
if (!error)
{
/* Open directory stream using wide-character name */
dirp->wdirp = _wopendir (wname);
if (dirp->wdirp) {
if (dirp->wdirp)
{
/* Directory stream opened */
error = 0;
} else {
}
else
{
/* Failed to open directory stream */
error = 1;
}
} else {
}
else
{
/*
* Cannot convert file name to wide-character string. This
* occurs if the string contains invalid multi-byte sequences or
@@ -616,18 +627,18 @@ opendir(
*/
error = 1;
}
} else {
}
else
{
/* Cannot allocate DIR structure */
error = 1;
}
/* Clean up in case of error */
if (error && dirp) {
if (error && dirp)
{
free (dirp);
dirp = NULL;
}
return dirp;
}
@@ -653,14 +664,13 @@ readdir(
/* Read next directory entry */
datap = dirent_next (dirp->wdirp);
if (datap) {
if (datap)
{
size_t n;
int error;
/* Attempt to convert file name to multi-byte string */
error = dirent_wcstombs_s(
&n, dirp->ent.d_name, MAX_PATH + 1, datap->cFileName, MAX_PATH);
/*
* If the file name cannot be represented by a multi-byte string,
* then attempt to use old 8+3 file name. This allows traditional
@@ -671,37 +681,40 @@ readdir(
* name unless the file system provides one. At least
* VirtualBox shared folders fail to do this.
*/
if (error && datap->cAlternateFileName[0] != '\0') {
if (error && datap->cAlternateFileName[0] != '\0')
{
error = dirent_wcstombs_s(
&n, dirp->ent.d_name, MAX_PATH + 1, datap->cAlternateFileName,
sizeof (datap->cAlternateFileName) /
sizeof (datap->cAlternateFileName[0]));
}
if (!error) {
if (!error)
{
DWORD attr;
/* Initialize directory entry for return */
entp = &dirp->ent;
/* Length of file name excluding zero terminator */
entp->d_namlen = n - 1;
/* File attributes */
attr = datap->dwFileAttributes;
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0)
{
entp->d_type = DT_CHR;
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
}
else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
entp->d_type = DT_DIR;
} else {
}
else
{
entp->d_type = DT_REG;
}
/* Reset dummy fields */
entp->d_ino = 0;
entp->d_reclen = sizeof (struct dirent);
} else {
}
else
{
/*
* Cannot convert file name to multi-byte string so construct
* an errornous directory entry and return that. Note that
@@ -716,8 +729,9 @@ readdir(
entp->d_ino = 0;
entp->d_reclen = 0;
}
} else {
}
else
{
/* No more directory entries */
entp = NULL;
}
@@ -733,21 +747,19 @@ closedir(
DIR *dirp)
{
int ok;
if (dirp) {
if (dirp)
{
/* Close wide-character directory stream */
ok = _wclosedir (dirp->wdirp);
dirp->wdirp = NULL;
/* Release multi-byte character version */
free (dirp);
} else {
}
else
{
/* Invalid directory stream */
dirent_set_errno (EBADF);
ok = /*failure*/-1;
}
return ok;
}
@@ -773,43 +785,35 @@ dirent_mbstowcs_s(
size_t count)
{
int error;
#if defined(_MSC_VER) && _MSC_VER >= 1400
/* Microsoft Visual Studio 2005 or later */
error = mbstowcs_s (pReturnValue, wcstr, sizeInWords, mbstr, count);
#else
/* Older Visual Studio or non-Microsoft compiler */
size_t n;
/* Convert to wide-character string */
n = mbstowcs (wcstr, mbstr, count);
if (n < sizeInWords) {
if (n < sizeInWords)
{
/* Zero-terminate output buffer */
if (wcstr) {
if (wcstr)
{
wcstr[n] = 0;
}
/* Length of resuting multi-byte string WITH zero terminator */
if (pReturnValue) {
if (pReturnValue)
{
*pReturnValue = n + 1;
}
/* Success */
error = 0;
} else {
}
else
{
/* Could not convert string */
error = 1;
}
#endif
return error;
}
@@ -823,43 +827,35 @@ dirent_wcstombs_s(
size_t count)
{
int error;
#if defined(_MSC_VER) && _MSC_VER >= 1400
/* Microsoft Visual Studio 2005 or later */
error = wcstombs_s (pReturnValue, mbstr, sizeInBytes, wcstr, count);
#else
/* Older Visual Studio or non-Microsoft compiler */
size_t n;
/* Convert to multi-byte string */
n = wcstombs (mbstr, wcstr, count);
if (n < sizeInBytes) {
if (n < sizeInBytes)
{
/* Zero-terminate output buffer */
if (mbstr) {
if (mbstr)
{
mbstr[n] = '\0';
}
/* Lenght of resulting multi-bytes string WITH zero-terminator */
if (pReturnValue) {
if (pReturnValue)
{
*pReturnValue = n + 1;
}
/* Success */
error = 0;
} else {
}
else
{
/* Cannot convert string */
error = 1;
}
#endif
return error;
}
@@ -869,15 +865,11 @@ dirent_set_errno(
int error)
{
#if defined(_MSC_VER)
/* Microsoft Visual Studio */
_set_errno (error);
#else
/* Non-Microsoft compiler */
errno = error;
#endif
}

View File

@@ -51,7 +51,8 @@
typedef unsigned int TRexBool;
typedef struct TRex TRex;
typedef struct {
typedef struct
{
const TRexChar *begin;
int len;
} TRexMatch;

View File

@@ -17,12 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utility.h"
Rect expandRect(Rect original, int expandXPixels, int expandYPixels, int maxX, int maxY)
{
Rect expandedRegion = Rect(original);
@@ -90,8 +86,6 @@ Mat addLabel(Mat input, string label)
return newImage;
}
void drawAndWait(cv::Mat* frame)
{
cv::imshow("Temp Window", *frame);
@@ -137,7 +131,8 @@ vector<Mat> produceThresholds(const Mat img_gray, Config* config)
NiblackSauvolaWolfJolion (img_gray, thresholds[i++], WOLFJOLION, win, win, 0.05 + (k * 0.35));
bitwise_not(thresholds[i-1], thresholds[i-1]);
k = 1; win = 22;
k = 1;
win = 22;
NiblackSauvolaWolfJolion (img_gray, thresholds[i++], WOLFJOLION, win, win, 0.05 + (k * 0.35));
bitwise_not(thresholds[i-1], thresholds[i-1]);
//NiblackSauvolaWolfJolion (img_gray, thresholds[i++], WOLFJOLION, win, win, 0.05 + (k * 0.35));
@@ -151,11 +146,6 @@ vector<Mat> produceThresholds(const Mat img_gray, Config* config)
NiblackSauvolaWolfJolion (img_gray, thresholds[i++], SAUVOLA, 12, 12, 0.18 * k);
bitwise_not(thresholds[i-1], thresholds[i-1]);
if (config->debugTiming)
{
timespec endTime;
@@ -180,7 +170,6 @@ double median(int array[], int arraySize)
return arraySize % 2 ? array[arraySize / 2] : (array[arraySize / 2 - 1] + array[arraySize / 2]) / 2;
}
Mat equalizeBrightness(Mat img)
{
@@ -194,7 +183,6 @@ Mat equalizeBrightness(Mat img)
normalize(img, img, 0, 255, NORM_MINMAX);
img.convertTo(img, CV_8U); // convert back to unsigned int
return img;
}
@@ -229,7 +217,6 @@ void fillMask(Mat img, const Mat mask, Scalar color)
}
}
void drawX(Mat img, Rect rect, Scalar color, int thickness)
{
Point tl(rect.x, rect.y);
@@ -257,7 +244,6 @@ float angleBetweenPoints(Point p1, Point p2)
return atan2((float) deltaY, (float) deltaX) * (180 / CV_PI);
}
Size getSizeMaintainingAspect(Mat inputImg, int maxWidth, int maxHeight)
{
float aspect = ((float) inputImg.cols) / ((float) inputImg.rows);
@@ -272,7 +258,6 @@ Size getSizeMaintainingAspect(Mat inputImg, int maxWidth, int maxHeight)
}
}
LineSegment::LineSegment()
{
init(0, 0, 0, 0);
@@ -302,7 +287,8 @@ void LineSegment::init(int x1, int y1, int x2, int y2)
this->angle = angleBetweenPoints(p1, p2);
}
bool LineSegment::isPointBelowLine( Point tp ){
bool LineSegment::isPointBelowLine( Point tp )
{
return ((p2.x - p1.x)*(tp.y - p1.y) - (p2.y - p1.y)*(tp.x - p1.x)) > 0;
}
@@ -331,7 +317,6 @@ Point LineSegment::intersection(LineSegment line)
float c1, c2;
float intersection_X = -1, intersection_Y= -1;
c1 = p1.y - slope * p1.x; // which is same as y2 - slope * x2
c2 = line.p2.y - line.slope * line.p2.x; // which is same as y2 - slope * x2
@@ -361,8 +346,6 @@ Point LineSegment::intersection(LineSegment line)
}
Point LineSegment::midpoint()
{
// Handle the case where the line is vertical

View File

@@ -23,8 +23,6 @@ VerticalHistogram::VerticalHistogram(Mat inputImage, Mat mask)
{
analyzeImage(inputImage, mask);
}
VerticalHistogram::~VerticalHistogram()
@@ -33,13 +31,11 @@ VerticalHistogram::~VerticalHistogram()
colHeights.clear();
}
void VerticalHistogram::analyzeImage(Mat inputImage, Mat mask)
{
highestPeak = 0;
lowestValley = inputImage.rows;
histoImg = Mat::zeros(inputImage.size(), CV_8U);
int columnCount;
@@ -55,7 +51,6 @@ void VerticalHistogram::analyzeImage(Mat inputImage, Mat mask)
columnCount++;
}
this->colHeights.push_back(columnCount);
if (columnCount < lowestValley)
@@ -63,7 +58,6 @@ void VerticalHistogram::analyzeImage(Mat inputImage, Mat mask)
if (columnCount > highestPeak)
highestPeak = columnCount;
for (; columnCount > 0; columnCount--)
histoImg.at<uchar>(inputImage.rows - columnCount, col) = 255;
@@ -132,7 +126,6 @@ void VerticalHistogram::findValleys()
if (colHeights[i] > relativePeakHeight)
relativePeakHeight = colHeights[i];
prevDirection = FLAT;
}
@@ -181,7 +174,6 @@ HistogramDirection VerticalHistogram::getHistogramDirection(int index)
}
forwardAverage = forwardAverage / ((float) (1 + forwardEndIndex - index));
float diff = forwardAverage - trailingAverage;
float minDiff = ((float) (highestPeak - lowestValley)) * 0.10;

View File

@@ -54,7 +54,8 @@ typedef std::istrstream istringstream;
#include "ArgTraits.h"
#include "StandardTraits.h"
namespace TCLAP {
namespace TCLAP
{
/**
* A virtual base class that defines the essential data for all arguments.
@@ -77,13 +78,21 @@ class Arg
/**
* Indicates whether the rest of the arguments should be ignored.
*/
static bool& ignoreRestRef() { static bool ign = false; return ign; }
static bool& ignoreRestRef()
{
static bool ign = false;
return ign;
}
/**
* The delimiter that separates an argument flag/name from the
* value.
*/
static char& delimiterRef() { static char delim = ' '; return delim; }
static char& delimiterRef()
{
static char delim = ' ';
return delim;
}
protected:
@@ -197,24 +206,36 @@ class Arg
/**
* Begin ignoring arguments since the "--" argument was specified.
*/
static void beginIgnoring() { ignoreRestRef() = true; }
static void beginIgnoring()
{
ignoreRestRef() = true;
}
/**
* Whether to ignore the rest.
*/
static bool ignoreRest() { return ignoreRestRef(); }
static bool ignoreRest()
{
return ignoreRestRef();
}
/**
* The delimiter that separates an argument flag/name from the
* value.
*/
static char delimiter() { return delimiterRef(); }
static char delimiter()
{
return delimiterRef();
}
/**
* The char used as a place holder when SwitchArgs are combined.
* Currently set to the bell char (ASCII 7).
*/
static char blankChar() { return (char)7; }
static char blankChar()
{
return (char)7;
}
/**
* The char that indicates the beginning of a flag. Defaults to '-', but
@@ -223,7 +244,10 @@ class Arg
#ifndef TCLAP_FLAGSTARTCHAR
#define TCLAP_FLAGSTARTCHAR '-'
#endif
static char flagStartChar() { return TCLAP_FLAGSTARTCHAR; }
static char flagStartChar()
{
return TCLAP_FLAGSTARTCHAR;
}
/**
* The sting that indicates the beginning of a flag. Defaults to "-", but
@@ -233,7 +257,10 @@ class Arg
#ifndef TCLAP_FLAGSTARTSTRING
#define TCLAP_FLAGSTARTSTRING "-"
#endif
static const std::string flagStartString() { return TCLAP_FLAGSTARTSTRING; }
static const std::string flagStartString()
{
return TCLAP_FLAGSTARTSTRING;
}
/**
* The sting that indicates the beginning of a name. Defaults to "--", but
@@ -242,18 +269,27 @@ class Arg
#ifndef TCLAP_NAMESTARTSTRING
#define TCLAP_NAMESTARTSTRING "--"
#endif
static const std::string nameStartString() { return TCLAP_NAMESTARTSTRING; }
static const std::string nameStartString()
{
return TCLAP_NAMESTARTSTRING;
}
/**
* The name used to identify the ignore rest argument.
*/
static const std::string ignoreNameString() { return "ignore_rest"; }
static const std::string ignoreNameString()
{
return "ignore_rest";
}
/**
* Sets the delimiter for all arguments.
* \param c - The character that delimits flags/names from values.
*/
static void setDelimiter( char c ) { delimiterRef() = c; }
static void setDelimiter( char c )
{
delimiterRef() = c;
}
/**
* Pure virtual method meant to handle the parsing and value assignment
@@ -416,9 +452,9 @@ ExtractValue(T &destVal, const std::string& strVal, ValueLike vl)
{
static_cast<void>(vl); // Avoid warning about unused vl
std::istringstream is(strVal);
int valuesRead = 0;
while ( is.good() ) {
while ( is.good() )
{
if ( is.peek() != EOF )
#ifdef TCLAP_SETBASE_ZERO
is >> std::setbase(0) >> destVal;
@@ -427,19 +463,14 @@ ExtractValue(T &destVal, const std::string& strVal, ValueLike vl)
#endif
else
break;
valuesRead++;
}
if ( is.fail() )
throw( ArgParseException("Couldn't read argument value "
"from string '" + strVal + "'"));
if ( valuesRead > 1 )
throw( ArgParseException("More than one valid value parsed from "
"string '" + strVal + "'"));
}
/*
@@ -480,7 +511,6 @@ inline Arg::Arg(const std::string& flag,
if ( _flag.length() > 1 )
throw(SpecificationException(
"Argument flag can only be one character long", toString() ) );
if ( _name != ignoreNameString() &&
( _flag == Arg::flagStartString() ||
_flag == Arg::nameStartString() ||
@@ -489,7 +519,6 @@ inline Arg::Arg(const std::string& flag,
Arg::flagStartString() + "' or '" +
Arg::nameStartString() + "' or a space.",
toString() ) );
if ( ( _name.substr( 0, Arg::flagStartString().length() ) == Arg::flagStartString() ) ||
( _name.substr( 0, Arg::nameStartString().length() ) == Arg::nameStartString() ) ||
( _name.find( " ", 0 ) != std::string::npos ) )
@@ -497,7 +526,6 @@ inline Arg::Arg(const std::string& flag,
Arg::flagStartString() + "' or '" +
Arg::nameStartString() + "' or space.",
toString() ) );
}
inline Arg::~Arg() { }
@@ -505,42 +533,31 @@ inline Arg::~Arg() { }
inline std::string Arg::shortID( const std::string& valueId ) const
{
std::string id = "";
if ( _flag != "" )
id = Arg::flagStartString() + _flag;
else
id = Arg::nameStartString() + _name;
if ( _valueRequired )
id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
if ( !_required )
id = "[" + id + "]";
return id;
}
inline std::string Arg::longID( const std::string& valueId ) const
{
std::string id = "";
if ( _flag != "" )
{
id += Arg::flagStartString() + _flag;
if ( _valueRequired )
id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
id += ", ";
}
id += Arg::nameStartString() + _name;
if ( _valueRequired )
id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
return id;
}
inline bool Arg::operator==(const Arg& a) const
@@ -556,21 +573,31 @@ inline std::string Arg::getDescription() const
std::string desc = "";
if ( _required )
desc = "(" + _requireLabel + ") ";
// if ( _valueRequired )
// desc += "(value required) ";
desc += _description;
return desc;
}
inline const std::string& Arg::getFlag() const { return _flag; }
inline const std::string& Arg::getFlag() const
{
return _flag;
}
inline const std::string& Arg::getName() const { return _name; }
inline const std::string& Arg::getName() const
{
return _name;
}
inline bool Arg::isRequired() const { return _required; }
inline bool Arg::isRequired() const
{
return _required;
}
inline bool Arg::isValueRequired() const { return _valueRequired; }
inline bool Arg::isValueRequired() const
{
return _valueRequired;
}
inline bool Arg::isSet() const
{
@@ -580,7 +607,10 @@ inline bool Arg::isSet() const
return false;
}
inline bool Arg::isIgnoreable() const { return _ignoreable; }
inline bool Arg::isIgnoreable() const
{
return _ignoreable;
}
inline void Arg::setRequireLabel( const std::string& s)
{
@@ -599,12 +629,9 @@ inline bool Arg::argMatches( const std::string& argFlag ) const
inline std::string Arg::toString() const
{
std::string s = "";
if ( _flag != "" )
s += Arg::flagStartString() + _flag + " ";
s += "(" + Arg::nameStartString() + _name + ")";
return s;
}
@@ -626,13 +653,11 @@ inline void Arg::trimFlag(std::string& flag, std::string& value) const
stop = i;
break;
}
if ( stop > 1 )
{
value = flag.substr(stop+1);
flag = flag.substr(0,stop);
}
}
/**
@@ -643,7 +668,6 @@ inline bool Arg::_hasBlanks( const std::string& s ) const
for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
if ( s[i] == Arg::blankChar() )
return true;
return false;
}

View File

@@ -27,7 +27,8 @@
#include <string>
#include <exception>
namespace TCLAP {
namespace TCLAP
{
/**
* A simple class that defines and argument exception. Should be caught
@@ -61,7 +62,10 @@ class ArgException : public std::exception
/**
* Returns the error text.
*/
std::string error() const { return ( _errorText ); }
std::string error() const
{
return ( _errorText );
}
/**
* Returns the argument id.
@@ -184,11 +188,15 @@ class SpecificationException : public ArgException
};
class ExitException {
class ExitException
{
public:
ExitException(int estat) : _estat(estat) {}
int getExitStatus() const { return _estat; }
int getExitStatus() const
{
return _estat;
}
private:
int _estat;

View File

@@ -26,7 +26,8 @@
#ifndef TCLAP_ARGTRAITS_H
#define TCLAP_ARGTRAITS_H
namespace TCLAP {
namespace TCLAP
{
// We use two empty structs to get compile type specialization
// function to work
@@ -35,7 +36,8 @@ namespace TCLAP {
* A value like argument value type is a value that can be set using
* operator>>. This is the default value type.
*/
struct ValueLike {
struct ValueLike
{
typedef ValueLike ValueCategory;
virtual ~ValueLike() {}
};
@@ -45,7 +47,8 @@ struct ValueLike {
* operator=(string). Usefull if the value type contains spaces which
* will be broken up into individual tokens by operator>>.
*/
struct StringLike {
struct StringLike
{
virtual ~StringLike() {}
};
@@ -54,7 +57,8 @@ struct StringLike {
* traits. This is a compile time thing and does not add any overhead
* to the inherenting class.
*/
struct StringLikeTrait {
struct StringLikeTrait
{
typedef StringLike ValueCategory;
virtual ~StringLikeTrait() {}
};
@@ -64,7 +68,8 @@ struct StringLikeTrait {
* traits. This is a compile time thing and does not add any overhead
* to the inherenting class.
*/
struct ValueLikeTrait {
struct ValueLikeTrait
{
typedef ValueLike ValueCategory;
virtual ~ValueLikeTrait() {}
};
@@ -76,7 +81,8 @@ struct ValueLikeTrait {
* supported types are StringLike and ValueLike.
*/
template<typename T>
struct ArgTraits {
struct ArgTraits
{
typedef typename T::ValueCategory ValueCategory;
virtual ~ArgTraits() {}
//typedef ValueLike ValueCategory;

View File

@@ -48,7 +48,8 @@
#include <algorithm>
#include <stdlib.h> // Needed for exit(), which isn't defined in some envs.
namespace TCLAP {
namespace TCLAP
{
template<typename T> void DelPtr(T ptr)
{
@@ -346,8 +347,8 @@ inline CmdLine::~CmdLine()
{
ClearContainer(_argDeleteOnExitList);
ClearContainer(_visitorDeleteOnExitList);
if ( !_userSetOutput ) {
if ( !_userSetOutput )
{
delete _output;
_output = 0;
}
@@ -356,11 +357,8 @@ inline CmdLine::~CmdLine()
inline void CmdLine::_constructor()
{
_output = new StdOutput;
Arg::setDelimiter( _delimiter );
Visitor* v;
if ( _helpAndVersion )
{
v = new HelpVisitor( this, &_output );
@@ -370,7 +368,6 @@ inline void CmdLine::_constructor()
add( help );
deleteOnExit(help);
deleteOnExit(v);
v = new VersionVisitor( this, &_output );
SwitchArg* vers = new SwitchArg("","version",
"Displays version information and exits.",
@@ -379,7 +376,6 @@ inline void CmdLine::_constructor()
deleteOnExit(vers);
deleteOnExit(v);
}
v = new IgnoreRestVisitor();
SwitchArg* ignore = new SwitchArg(Arg::flagStartString(),
Arg::ignoreNameString(),
@@ -393,7 +389,6 @@ inline void CmdLine::_constructor()
inline void CmdLine::xorAdd( std::vector<Arg*>& ors )
{
_xorHandler.add( ors );
for (ArgVectorIterator it = ors.begin(); it != ors.end(); it++)
{
(*it)->forceRequired();
@@ -422,9 +417,7 @@ inline void CmdLine::add( Arg* a )
throw( SpecificationException(
"Argument with same flag/name already exists!",
a->longID() ) );
a->addToList( _argList );
if ( a->isRequired() )
_numRequired++;
}
@@ -437,7 +430,6 @@ inline void CmdLine::parse(int argc, const char * const * argv)
std::vector<std::string> args;
for (int i = 0; i < argc; i++)
args.push_back(argv[i]);
parse(args);
}
@@ -445,18 +437,17 @@ inline void CmdLine::parse(std::vector<std::string>& args)
{
bool shouldExit = false;
int estat = 0;
try {
try
{
_progName = args.front();
args.erase(args.begin());
int requiredCount = 0;
for (int i = 0; static_cast<unsigned int>(i) < args.size(); i++)
{
bool matched = false;
for (ArgListIterator it = _argList.begin();
it != _argList.end(); it++) {
it != _argList.end(); it++)
{
if ( (*it)->processArg( &i, args ) )
{
requiredCount += _xorHandler.check( *it );
@@ -464,46 +455,47 @@ inline void CmdLine::parse(std::vector<std::string>& args)
break;
}
}
// checks to see if the argument is an empty combined
// switch and if so, then we've actually matched it
if ( !matched && _emptyCombined( args[i] ) )
matched = true;
if ( !matched && !Arg::ignoreRest() )
throw(CmdLineParseException("Couldn't find match "
"for argument",
args[i]));
}
if ( requiredCount < _numRequired )
missingArgsException();
if ( requiredCount > _numRequired )
throw(CmdLineParseException("Too many arguments!"));
} catch ( ArgException& e ) {
}
catch ( ArgException& e )
{
// If we're not handling the exceptions, rethrow.
if ( !_handleExceptions) {
if ( !_handleExceptions)
{
throw;
}
try {
try
{
_output->failure(*this,e);
} catch ( ExitException &ee ) {
}
catch ( ExitException &ee )
{
estat = ee.getExitStatus();
shouldExit = true;
}
} catch (ExitException &ee) {
}
catch (ExitException &ee)
{
// If we're not handling the exceptions, rethrow.
if ( !_handleExceptions) {
if ( !_handleExceptions)
{
throw;
}
estat = ee.getExitStatus();
shouldExit = true;
}
if (shouldExit)
exit(estat);
}
@@ -512,18 +504,15 @@ inline bool CmdLine::_emptyCombined(const std::string& s)
{
if ( s.length() > 0 && s[0] != Arg::flagStartChar() )
return false;
for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
if ( s[i] != Arg::blankChar() )
return false;
return true;
}
inline void CmdLine::missingArgsException()
{
int count = 0;
std::string missingArgList;
for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++)
{
@@ -535,15 +524,12 @@ inline void CmdLine::missingArgsException()
}
}
missingArgList = missingArgList.substr(0,missingArgList.length()-2);
std::string msg;
if ( count > 1 )
msg = "Required arguments missing: ";
else
msg = "Required argument missing: ";
msg += missingArgList;
throw(CmdLineParseException(msg));
}
@@ -619,7 +605,6 @@ inline void CmdLine::reset()
{
for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ )
(*it)->reset();
_progName.clear();
}

View File

@@ -30,7 +30,8 @@
#include <algorithm>
namespace TCLAP {
namespace TCLAP
{
class Arg;
class CmdLineOutput;

View File

@@ -30,7 +30,8 @@
#include <iomanip>
#include <algorithm>
namespace TCLAP {
namespace TCLAP
{
class CmdLineInterface;
class ArgException;

View File

@@ -29,7 +29,8 @@
#include <iomanip>
#include <algorithm>
namespace TCLAP {
namespace TCLAP
{
/**
* The interface that defines the interaction between the Arg and Constraint.
@@ -61,7 +62,10 @@ class Constraint
* Silences warnings about Constraint being a base class with virtual
* functions but without a virtual destructor.
*/
virtual ~Constraint() { ; }
virtual ~Constraint()
{
;
}
};
} //namespace TCLAP

View File

@@ -34,7 +34,8 @@
#include <tclap/XorHandler.h>
#include <tclap/Arg.h>
namespace TCLAP {
namespace TCLAP
{
/**
* A class that generates DocBook output for usage() method for the
@@ -101,28 +102,21 @@ inline void DocBookOutput::usage(CmdLineInterface& _cmd )
XorHandler xorHandler = _cmd.getXorHandler();
std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
basename(progName);
std::cout << "<?xml version='1.0'?>" << std::endl;
std::cout << "<!DOCTYPE refentry PUBLIC \"-//OASIS//DTD DocBook XML V4.2//EN\"" << std::endl;
std::cout << "\t\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\">" << std::endl << std::endl;
std::cout << "<refentry>" << std::endl;
std::cout << "<refmeta>" << std::endl;
std::cout << "<refentrytitle>" << progName << "</refentrytitle>" << std::endl;
std::cout << "<manvolnum>1</manvolnum>" << std::endl;
std::cout << "</refmeta>" << std::endl;
std::cout << "<refnamediv>" << std::endl;
std::cout << "<refname>" << progName << "</refname>" << std::endl;
std::cout << "<refpurpose>" << _cmd.getMessage() << "</refpurpose>" << std::endl;
std::cout << "</refnamediv>" << std::endl;
std::cout << "<refsynopsisdiv>" << std::endl;
std::cout << "<cmdsynopsis>" << std::endl;
std::cout << "<command>" << progName << "</command>" << std::endl;
// xor
for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
{
@@ -130,45 +124,34 @@ inline void DocBookOutput::usage(CmdLineInterface& _cmd )
for ( ArgVectorIterator it = xorList[i].begin();
it != xorList[i].end(); it++ )
printShortArg((*it));
std::cout << "</group>" << std::endl;
}
// rest of args
for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
if ( !xorHandler.contains( (*it) ) )
printShortArg((*it));
std::cout << "</cmdsynopsis>" << std::endl;
std::cout << "</refsynopsisdiv>" << std::endl;
std::cout << "<refsect1>" << std::endl;
std::cout << "<title>Description</title>" << std::endl;
std::cout << "<para>" << std::endl;
std::cout << _cmd.getMessage() << std::endl;
std::cout << "</para>" << std::endl;
std::cout << "</refsect1>" << std::endl;
std::cout << "<refsect1>" << std::endl;
std::cout << "<title>Options</title>" << std::endl;
std::cout << "<variablelist>" << std::endl;
for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
printLongArg((*it));
std::cout << "</variablelist>" << std::endl;
std::cout << "</refsect1>" << std::endl;
std::cout << "<refsect1>" << std::endl;
std::cout << "<title>Version</title>" << std::endl;
std::cout << "<para>" << std::endl;
std::cout << xversion << std::endl;
std::cout << "</para>" << std::endl;
std::cout << "</refsect1>" << std::endl;
std::cout << "</refentry>" << std::endl;
}
inline void DocBookOutput::failure( CmdLineInterface& _cmd,
@@ -213,22 +196,17 @@ inline void DocBookOutput::printShortArg(Arg* a)
{
std::string lt = "&lt;";
std::string gt = "&gt;";
std::string id = a->shortID();
substituteSpecialChars(id,'<',lt);
substituteSpecialChars(id,'>',gt);
removeChar(id,'[');
removeChar(id,']');
std::string choice = "opt";
if ( a->isRequired() )
choice = "plain";
std::cout << "<arg choice='" << choice << '\'';
if ( a->acceptsMultipleValues() )
std::cout << " rep='repeat'";
std::cout << '>';
if ( !a->getFlag().empty() )
std::cout << a->flagStartChar() << a->getFlag();
@@ -246,20 +224,16 @@ inline void DocBookOutput::printShortArg(Arg* a)
std::cout << "<replaceable>" << arg << "</replaceable>";
}
std::cout << "</arg>" << std::endl;
}
inline void DocBookOutput::printLongArg(Arg* a)
{
std::string lt = "&lt;";
std::string gt = "&gt;";
std::string desc = a->getDescription();
substituteSpecialChars(desc,'<',lt);
substituteSpecialChars(desc,'>',gt);
std::cout << "<varlistentry>" << std::endl;
if ( !a->getFlag().empty() )
{
std::cout << "<term>" << std::endl;
@@ -268,7 +242,6 @@ inline void DocBookOutput::printLongArg(Arg* a)
std::cout << "</option>" << std::endl;
std::cout << "</term>" << std::endl;
}
std::cout << "<term>" << std::endl;
std::cout << "<option>";
std::cout << a->nameStartString() << a->getName();
@@ -285,13 +258,11 @@ inline void DocBookOutput::printLongArg(Arg* a)
}
std::cout << "</option>" << std::endl;
std::cout << "</term>" << std::endl;
std::cout << "<listitem>" << std::endl;
std::cout << "<para>" << std::endl;
std::cout << desc << std::endl;
std::cout << "</para>" << std::endl;
std::cout << "</listitem>" << std::endl;
std::cout << "</varlistentry>" << std::endl;
}

View File

@@ -26,7 +26,8 @@
#include "CmdLineOutput.h"
#include "Visitor.h"
namespace TCLAP {
namespace TCLAP
{
/**
* A Visitor object that calls the usage method of the given CmdLineOutput
@@ -67,7 +68,11 @@ class HelpVisitor: public Visitor
* Calls the usage method of the CmdLineOutput for the
* specified CmdLine.
*/
void visit() { (*_out)->usage(*_cmd); throw ExitException(0); }
void visit()
{
(*_out)->usage(*_cmd);
throw ExitException(0);
}
};

View File

@@ -26,7 +26,8 @@
#include "Visitor.h"
#include "Arg.h"
namespace TCLAP {
namespace TCLAP
{
/**
* A Vistor that tells the CmdLine to begin ignoring arguments after
@@ -44,7 +45,10 @@ class IgnoreRestVisitor: public Visitor
/**
* Sets Arg::_ignoreRest.
*/
void visit() { Arg::beginIgnoring(); }
void visit()
{
Arg::beginIgnoring();
}
};
}

View File

@@ -29,7 +29,8 @@
#include "Arg.h"
#include "Constraint.h"
namespace TCLAP {
namespace TCLAP
{
/**
* An argument that allows multiple values of type T to be specified. Very
* similar to a ValueArg, except a vector of values will be returned
@@ -191,13 +192,19 @@ public:
* Returns an iterator over the values parsed from the command
* line.
*/
const_iterator begin() const { return _values.begin(); }
const_iterator begin() const
{
return _values.begin();
}
/**
* Returns the end of the values parsed from the command
* line.
*/
const_iterator end() const { return _values.end(); }
const_iterator end() const
{
return _values.end();
}
/**
* Returns the a short id string. Used in the usage.
@@ -302,29 +309,27 @@ MultiArg<T>::MultiArg(const std::string& flag,
}
template<class T>
const std::vector<T>& MultiArg<T>::getValue() { return _values; }
const std::vector<T>& MultiArg<T>::getValue()
{
return _values;
}
template<class T>
bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
{
if ( _ignoreable && Arg::ignoreRest() )
return false;
if ( _hasBlanks( args[*i] ) )
return false;
std::string flag = args[*i];
std::string value = "";
trimFlag( flag, value );
if ( argMatches( flag ) )
{
if ( Arg::delimiter() != ' ' && value == "" )
throw( ArgParseException(
"Couldn't find delimiter for this argument!",
toString() ) );
// always take the first one, regardless of start string
if ( value == "" )
{
@@ -337,7 +342,6 @@ bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
}
else
_extractValue( value );
/*
// continuing taking the args until we hit one with a start string
while ( (unsigned int)(*i)+1 < args.size() &&
@@ -345,10 +349,8 @@ bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
_extractValue( args[++(*i)] );
*/
_alreadySet = true;
_checkWithVisitor();
return true;
}
else
@@ -391,20 +393,21 @@ bool MultiArg<T>::isRequired() const
}
else
return false;
}
template<class T>
void MultiArg<T>::_extractValue( const std::string& val )
{
try {
try
{
T tmp;
ExtractValue(tmp, val, typename ArgTraits<T>::ValueCategory());
_values.push_back(tmp);
} catch( ArgParseException &e) {
}
catch( ArgParseException &e)
{
throw ArgParseException(e.error(), toString());
}
if ( _constraint != NULL )
if ( ! _constraint->check( _values.back() ) )
throw( CmdLineParseException( "Value '" + val +

View File

@@ -30,7 +30,8 @@
#include "SwitchArg.h"
namespace TCLAP {
namespace TCLAP
{
/**
* A multiple switch argument. If the switch is set on the command line, then
@@ -150,39 +151,34 @@ _default( init )
parser.add( this );
}
inline int MultiSwitchArg::getValue() { return _value; }
inline int MultiSwitchArg::getValue()
{
return _value;
}
inline bool MultiSwitchArg::processArg(int *i, std::vector<std::string>& args)
{
if ( _ignoreable && Arg::ignoreRest() )
return false;
if ( argMatches( args[*i] ))
{
// so the isSet() method will work
_alreadySet = true;
// Matched argument: increment value.
++_value;
_checkWithVisitor();
return true;
}
else if ( combinedSwitchesMatch( args[*i] ) )
{
// so the isSet() method will work
_alreadySet = true;
// Matched argument: increment value.
++_value;
// Check for more in argument and increment value.
while ( combinedSwitchesMatch( args[*i] ) )
++_value;
_checkWithVisitor();
return false;
}
else

View File

@@ -26,7 +26,8 @@
#include <string>
namespace TCLAP {
namespace TCLAP
{
class OptionalUnlabeledTracker
{
@@ -35,13 +36,23 @@ class OptionalUnlabeledTracker
static void check( bool req, const std::string& argName );
static void gotOptional() { alreadyOptionalRef() = true; }
static void gotOptional()
{
alreadyOptionalRef() = true;
}
static bool& alreadyOptional() { return alreadyOptionalRef(); }
static bool& alreadyOptional()
{
return alreadyOptionalRef();
}
private:
static bool& alreadyOptionalRef() { static bool ct = false; return ct; }
static bool& alreadyOptionalRef()
{
static bool ct = false;
return ct;
}
};
@@ -51,7 +62,6 @@ inline void OptionalUnlabeledTracker::check( bool req, const std::string& argNam
throw( SpecificationException(
"You can't specify ANY Unlabeled Arg following an optional Unlabeled Arg",
argName ) );
if ( !req )
OptionalUnlabeledTracker::gotOptional();
}

View File

@@ -39,7 +39,8 @@
#endif
#endif
namespace TCLAP {
namespace TCLAP
{
// ======================================================================
// Integer types
@@ -49,7 +50,8 @@ namespace TCLAP {
* longs have value-like semantics.
*/
template<>
struct ArgTraits<long> {
struct ArgTraits<long>
{
typedef ValueLike ValueCategory;
};
@@ -57,7 +59,8 @@ struct ArgTraits<long> {
* ints have value-like semantics.
*/
template<>
struct ArgTraits<int> {
struct ArgTraits<int>
{
typedef ValueLike ValueCategory;
};
@@ -65,7 +68,8 @@ struct ArgTraits<int> {
* shorts have value-like semantics.
*/
template<>
struct ArgTraits<short> {
struct ArgTraits<short>
{
typedef ValueLike ValueCategory;
};
@@ -73,7 +77,8 @@ struct ArgTraits<short> {
* chars have value-like semantics.
*/
template<>
struct ArgTraits<char> {
struct ArgTraits<char>
{
typedef ValueLike ValueCategory;
};
@@ -82,7 +87,8 @@ struct ArgTraits<char> {
* long longs have value-like semantics.
*/
template<>
struct ArgTraits<long long> {
struct ArgTraits<long long>
{
typedef ValueLike ValueCategory;
};
#endif
@@ -95,7 +101,8 @@ struct ArgTraits<long long> {
* unsigned longs have value-like semantics.
*/
template<>
struct ArgTraits<unsigned long> {
struct ArgTraits<unsigned long>
{
typedef ValueLike ValueCategory;
};
@@ -103,7 +110,8 @@ struct ArgTraits<unsigned long> {
* unsigned ints have value-like semantics.
*/
template<>
struct ArgTraits<unsigned int> {
struct ArgTraits<unsigned int>
{
typedef ValueLike ValueCategory;
};
@@ -111,7 +119,8 @@ struct ArgTraits<unsigned int> {
* unsigned shorts have value-like semantics.
*/
template<>
struct ArgTraits<unsigned short> {
struct ArgTraits<unsigned short>
{
typedef ValueLike ValueCategory;
};
@@ -119,7 +128,8 @@ struct ArgTraits<unsigned short> {
* unsigned chars have value-like semantics.
*/
template<>
struct ArgTraits<unsigned char> {
struct ArgTraits<unsigned char>
{
typedef ValueLike ValueCategory;
};
@@ -129,7 +139,8 @@ struct ArgTraits<unsigned char> {
* size_ts have value-like semantics.
*/
template<>
struct ArgTraits<size_t> {
struct ArgTraits<size_t>
{
typedef ValueLike ValueCategory;
};
#endif
@@ -140,7 +151,8 @@ struct ArgTraits<size_t> {
* unsigned long longs have value-like semantics.
*/
template<>
struct ArgTraits<unsigned long long> {
struct ArgTraits<unsigned long long>
{
typedef ValueLike ValueCategory;
};
#endif
@@ -153,7 +165,8 @@ struct ArgTraits<unsigned long long> {
* floats have value-like semantics.
*/
template<>
struct ArgTraits<float> {
struct ArgTraits<float>
{
typedef ValueLike ValueCategory;
};
@@ -161,7 +174,8 @@ struct ArgTraits<float> {
* doubles have value-like semantics.
*/
template<>
struct ArgTraits<double> {
struct ArgTraits<double>
{
typedef ValueLike ValueCategory;
};
@@ -173,7 +187,8 @@ struct ArgTraits<double> {
* bools have value-like semantics.
*/
template<>
struct ArgTraits<bool> {
struct ArgTraits<bool>
{
typedef ValueLike ValueCategory;
};
@@ -183,7 +198,8 @@ struct ArgTraits<bool> {
*/
#ifndef TCLAP_DONT_DECLARE_WCHAR_T_ARGTRAITS
template<>
struct ArgTraits<wchar_t> {
struct ArgTraits<wchar_t>
{
typedef ValueLike ValueCategory;
};
#endif
@@ -192,7 +208,8 @@ struct ArgTraits<wchar_t> {
* Strings have string like argument traits.
*/
template<>
struct ArgTraits<std::string> {
struct ArgTraits<std::string>
{
typedef StringLike ValueCategory;
};

View File

@@ -34,7 +34,8 @@
#include "XorHandler.h"
#include "Arg.h"
namespace TCLAP {
namespace TCLAP
{
/**
* A class that isolates any output from the CmdLine object so that it
@@ -109,7 +110,6 @@ inline void StdOutput::version(CmdLineInterface& _cmd)
{
std::string progName = _cmd.getProgramName();
std::string xversion = _cmd.getVersion();
std::cout << std::endl << progName << " version: "
<< xversion << std::endl << std::endl;
}
@@ -117,38 +117,28 @@ inline void StdOutput::version(CmdLineInterface& _cmd)
inline void StdOutput::usage(CmdLineInterface& _cmd )
{
std::cout << std::endl << "USAGE: " << std::endl << std::endl;
_shortUsage( _cmd, std::cout );
std::cout << std::endl << std::endl << "Where: " << std::endl << std::endl;
_longUsage( _cmd, std::cout );
std::cout << std::endl;
}
inline void StdOutput::failure( CmdLineInterface& _cmd,
ArgException& e )
{
std::string progName = _cmd.getProgramName();
std::cerr << "PARSE ERROR: " << e.argId() << std::endl
<< " " << e.error() << std::endl << std::endl;
if ( _cmd.hasHelpAndVersion() )
{
std::cerr << "Brief USAGE: " << std::endl;
_shortUsage( _cmd, std::cerr );
std::cerr << std::endl << "For complete USAGE and HELP type: "
<< std::endl << " " << progName << " --help"
<< std::endl << std::endl;
}
else
usage(_cmd);
throw ExitException(1);
}
@@ -160,9 +150,7 @@ StdOutput::_shortUsage( CmdLineInterface& _cmd,
std::string progName = _cmd.getProgramName();
XorHandler xorHandler = _cmd.getXorHandler();
std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
std::string s = progName + " ";
// first the xor
for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
{
@@ -170,20 +158,16 @@ StdOutput::_shortUsage( CmdLineInterface& _cmd,
for ( ArgVectorIterator it = xorList[i].begin();
it != xorList[i].end(); it++ )
s += (*it)->shortID() + "|";
s[s.length()-1] = '}';
}
// then the rest
for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
if ( !xorHandler.contains( (*it) ) )
s += " " + (*it)->shortID();
// if the program name is too long, then adjust the second line offset
int secondLineOffset = static_cast<int>(progName.length()) + 2;
if ( secondLineOffset > 75/2 )
secondLineOffset = static_cast<int>(75/2);
spacePrint( os, s, 75, 3, secondLineOffset );
}
@@ -195,7 +179,6 @@ StdOutput::_longUsage( CmdLineInterface& _cmd,
std::string message = _cmd.getMessage();
XorHandler xorHandler = _cmd.getXorHandler();
std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
// first the xor
for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
{
@@ -205,13 +188,11 @@ StdOutput::_longUsage( CmdLineInterface& _cmd,
{
spacePrint( os, (*it)->longID(), 75, 3, 3 );
spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
if ( it+1 != xorList[i].end() )
spacePrint(os, "-- OR --", 75, 9, 0);
}
os << std::endl << std::endl;
}
// then the rest
for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
if ( !xorHandler.contains( (*it) ) )
@@ -220,9 +201,7 @@ StdOutput::_longUsage( CmdLineInterface& _cmd,
spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
os << std::endl;
}
os << std::endl;
spacePrint( os, message, 75, 3, 0 );
}
@@ -233,7 +212,6 @@ inline void StdOutput::spacePrint( std::ostream& os,
int secondLineOffset ) const
{
int len = static_cast<int>(s.length());
if ( (len + indentSpaces > maxWidth) && maxWidth > 0 )
{
int allowedLen = maxWidth - indentSpaces;
@@ -245,7 +223,6 @@ inline void StdOutput::spacePrint( std::ostream& os,
// doing it this way to support a VisualC++ 2005 bug
using namespace std;
int stringLen = min<int>( len - start, allowedLen );
// trim the length so it doesn't end in middle of a word
if ( stringLen == allowedLen )
while ( stringLen >= 0 &&
@@ -253,36 +230,28 @@ inline void StdOutput::spacePrint( std::ostream& os,
s[stringLen+start] != ',' &&
s[stringLen+start] != '|' )
stringLen--;
// ok, the word is longer than the line, so just split
// wherever the line ends
if ( stringLen <= 0 )
stringLen = allowedLen;
// check for newlines
for ( int i = 0; i < stringLen; i++ )
if ( s[start+i] == '\n' )
stringLen = i+1;
// print the indent
for ( int i = 0; i < indentSpaces; i++ )
os << " ";
if ( start == 0 )
{
// handle second line offsets
indentSpaces += secondLineOffset;
// adjust allowed len
allowedLen -= secondLineOffset;
}
os << s.substr(start,stringLen) << std::endl;
// so we don't start a line with a space
while ( s[stringLen+start] == ' ' && start < len )
start++;
start += stringLen;
}
}

View File

@@ -29,7 +29,8 @@
#include "Arg.h"
namespace TCLAP {
namespace TCLAP
{
/**
* A simple switch argument. If the switch is set on the command line, then
@@ -155,14 +156,16 @@ inline SwitchArg::SwitchArg(const std::string& flag,
parser.add( this );
}
inline bool SwitchArg::getValue() { return _value; }
inline bool SwitchArg::getValue()
{
return _value;
}
inline bool SwitchArg::lastCombined(std::string& combinedSwitches )
{
for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
if ( combinedSwitches[i] != Arg::blankChar() )
return false;
return true;
}
@@ -172,16 +175,13 @@ inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
if ( combinedSwitches.length() > 0 &&
combinedSwitches[0] != Arg::flagStartString()[0] )
return false;
// make sure it isn't a long name
if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
Arg::nameStartString() )
return false;
// make sure the delimiter isn't in the string
if ( combinedSwitches.find_first_of( Arg::delimiter() ) != std::string::npos )
return false;
// ok, we're not specifying a ValueArg, so we know that we have
// a combined switch list.
for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
@@ -196,7 +196,6 @@ inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
combinedSwitches[i] = Arg::blankChar();
return true;
}
// none of the switches passed in the list match.
return false;
}
@@ -206,17 +205,13 @@ inline void SwitchArg::commonProcessing()
if ( _xorSet )
throw(CmdLineParseException(
"Mutually exclusive argument already set!", toString()));
if ( _alreadySet )
throw(CmdLineParseException("Argument already set!", toString()));
_alreadySet = true;
if ( _value == true )
_value = false;
else
_value = true;
_checkWithVisitor();
}
@@ -224,12 +219,10 @@ inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
{
if ( _ignoreable && Arg::ignoreRest() )
return false;
// if the whole string matches the flag or name string
if ( argMatches( args[*i] ) )
{
commonProcessing();
return true;
}
// if a substring matches the flag as part of a combination
@@ -240,9 +233,7 @@ inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
if ( combinedSwitchesMatch( args[*i] ) )
throw(CmdLineParseException("Argument already set!",
toString()));
commonProcessing();
// We only want to return true if we've found the last combined
// match in the string, otherwise we return true so that other
// switches in the combination will have a chance to match.

View File

@@ -29,7 +29,8 @@
#include "MultiArg.h"
#include "OptionalUnlabeledTracker.h"
namespace TCLAP {
namespace TCLAP
{
/**
* Just like a MultiArg, except that the arguments are unlabeled. Basically,
@@ -244,16 +245,11 @@ UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
template<class T>
bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string>& args)
{
if ( _hasBlanks( args[*i] ) )
return false;
// never ignore an unlabeled multi arg
// always take the first value, regardless of the start string
_extractValue( args[(*i)] );
/*
// continue taking args until we hit the end or a start string
while ( (unsigned int)(*i)+1 < args.size() &&
@@ -261,9 +257,7 @@ bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string>& args)
args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
_extractValue( args[++(*i)] );
*/
_alreadySet = true;
return true;
}

View File

@@ -31,7 +31,8 @@
#include "OptionalUnlabeledTracker.h"
namespace TCLAP {
namespace TCLAP
{
/**
* The basic unlabeled argument that parses a value.
@@ -220,9 +221,7 @@ UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
: ValueArg<T>("", name, desc, req, val, typeDesc, v)
{
_ignoreable = ignoreable;
OptionalUnlabeledTracker::check(req, toString());
}
template<class T>
@@ -280,15 +279,11 @@ UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
template<class T>
bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args)
{
if ( _alreadySet )
return false;
if ( _hasBlanks( args[*i] ) )
return false;
// never ignore an unlabeled arg
_extractValue( args[*i] );
_alreadySet = true;
return true;
@@ -311,7 +306,6 @@ template<class T>
std::string UnlabeledValueArg<T>::longID(const std::string& val) const
{
static_cast<void>(val); // Ignore input, don't warn
// Ideally we would like to be able to use RTTI to return the name
// of the type required for this argument. However, g++ at least,
// doesn't appear to return terribly useful "names" of the types.

View File

@@ -29,7 +29,8 @@
#include "Arg.h"
#include "Constraint.h"
namespace TCLAP {
namespace TCLAP
{
/**
* The basic labeled argument that parses a value.
@@ -319,7 +320,10 @@ ValueArg<T>::ValueArg(const std::string& flag,
* Implementation of getValue().
*/
template<class T>
T& ValueArg<T>::getValue() { return _value; }
T& ValueArg<T>::getValue()
{
return _value;
}
/**
* Implementation of processArg().
@@ -329,15 +333,11 @@ bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
{
if ( _ignoreable && Arg::ignoreRest() )
return false;
if ( _hasBlanks( args[*i] ) )
return false;
std::string flag = args[*i];
std::string value = "";
trimFlag( flag, value );
if ( argMatches( flag ) )
{
if ( _alreadySet )
@@ -350,12 +350,10 @@ bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
throw( CmdLineParseException("Argument already set!",
toString()) );
}
if ( Arg::delimiter() != ' ' && value == "" )
throw( ArgParseException(
"Couldn't find delimiter for this argument!",
toString() ) );
if ( value == "" )
{
(*i)++;
@@ -367,7 +365,6 @@ bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
}
else
_extractValue( value );
_alreadySet = true;
_checkWithVisitor();
return true;
@@ -399,12 +396,14 @@ std::string ValueArg<T>::longID(const std::string& val) const
template<class T>
void ValueArg<T>::_extractValue( const std::string& val )
{
try {
try
{
ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
} catch( ArgParseException &e) {
}
catch( ArgParseException &e)
{
throw ArgParseException(e.error(), toString());
}
if ( _constraint != NULL )
if ( ! _constraint->check( _value ) )
throw( CmdLineParseException( "Value '" + val +

View File

@@ -41,7 +41,8 @@
#error "Need a stringstream (sstream or strstream) to compile!"
#endif
namespace TCLAP {
namespace TCLAP
{
/**
* A Constraint that constrains the Arg to only those values specified
@@ -102,7 +103,6 @@ ValuesConstraint<T>::ValuesConstraint(std::vector<T>& allowed)
{
for ( unsigned int i = 0; i < _allowed.size(); i++ )
{
#if defined(HAVE_SSTREAM)
std::ostringstream os;
#elif defined(HAVE_STRSTREAM)
@@ -110,11 +110,8 @@ ValuesConstraint<T>::ValuesConstraint(std::vector<T>& allowed)
#else
#error "Need a stringstream (sstream or strstream) to compile!"
#endif
os << _allowed[i];
std::string temp( os.str() );
if ( i > 0 )
_typeDesc += "|";
_typeDesc += temp;

View File

@@ -28,7 +28,8 @@
#include "CmdLineOutput.h"
#include "Visitor.h"
namespace TCLAP {
namespace TCLAP
{
/**
* A Vistor that will call the version method of the given CmdLineOutput
@@ -69,7 +70,8 @@ class VersionVisitor: public Visitor
* Calls the version method of the output object using the
* specified CmdLine.
*/
void visit() {
void visit()
{
(*_out)->version(*_cmd);
throw ExitException(0);
}

View File

@@ -23,7 +23,8 @@
#ifndef TCLAP_VISITOR_H
#define TCLAP_VISITOR_H
namespace TCLAP {
namespace TCLAP
{
/**
* A base class that defines the interface for visitors.

View File

@@ -29,7 +29,8 @@
#include <algorithm>
#include <iostream>
namespace TCLAP {
namespace TCLAP
{
/**
* This class handles lists of Arg's that are to be XOR'd on the command
@@ -116,14 +117,12 @@ inline int XorHandler::check( const Arg* a )
throw(CmdLineParseException(
"Mutually exclusive argument already set!",
(*it)->toString()));
// go through and set each arg that is not a
for ( ArgVectorIterator it = _orList[i].begin();
it != _orList[i].end();
it++ )
if ( a != (*it) )
(*it)->xorSet();
// return the number of required args that have now been set
if ( (*ait)->allowMore() )
return 0;
@@ -131,7 +130,6 @@ inline int XorHandler::check( const Arg* a )
return static_cast<int>(_orList[i].size());
}
}
if ( a->isRequired() )
return 1;
else
@@ -146,7 +144,6 @@ inline bool XorHandler::contains( const Arg* a )
it++ )
if ( a == (*it) )
return true;
return false;
}

View File

@@ -34,7 +34,8 @@
#include <tclap/XorHandler.h>
#include <tclap/Arg.h>
namespace TCLAP {
namespace TCLAP
{
/**
* A class that generates a Zsh completion function as output from the usage()
@@ -110,11 +111,9 @@ inline void ZshCompletionOutput::usage(CmdLineInterface& _cmd )
std::string xversion = _cmd.getVersion();
theDelimiter = _cmd.getDelimiter();
basename(progName);
std::cout << "#compdef " << progName << std::endl << std::endl <<
"# " << progName << " version " << _cmd.getVersion() << std::endl << std::endl <<
"_arguments -s -S";
for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
{
if ( (*it)->shortID().at(0) == '<' )
@@ -122,7 +121,6 @@ inline void ZshCompletionOutput::usage(CmdLineInterface& _cmd )
else if ( (*it)->getFlag() != "-" )
printOption((*it), getMutexList(_cmd, *it));
}
std::cout << std::endl;
}
@@ -164,7 +162,6 @@ inline void ZshCompletionOutput::basename( std::string& s )
inline void ZshCompletionOutput::printArg(Arg* a)
{
static int count = 1;
std::cout << " \\" << std::endl << " '";
if ( a->acceptsMultipleValues() )
std::cout << '*';
@@ -173,7 +170,6 @@ inline void ZshCompletionOutput::printArg(Arg* a)
std::cout << ':';
if ( !a->isRequired() )
std::cout << ':';
std::cout << a->getName() << ':';
std::map<std::string, std::string>::iterator compArg = common.find(a->getName());
if ( compArg != common.end() )
@@ -192,7 +188,6 @@ inline void ZshCompletionOutput::printOption(Arg* a, std::string mutex)
std::string flag = a->flagStartChar() + a->getFlag();
std::string name = a->nameStartString() + a->getName();
std::string desc = a->getDescription();
// remove full stop and capitalisation from description as
// this is the convention for zsh function
if (!desc.compare(0, 12, "(required) "))
@@ -212,9 +207,7 @@ inline void ZshCompletionOutput::printOption(Arg* a, std::string mutex)
{
desc.replace(0, 1, 1, tolower(desc.at(0)));
}
std::cout << " \\" << std::endl << " '" << mutex;
if ( a->getFlag().empty() )
{
std::cout << name;
@@ -227,7 +220,6 @@ inline void ZshCompletionOutput::printOption(Arg* a, std::string mutex)
std::cout << "=-";
quoteSpecialChars(desc);
std::cout << '[' << desc << ']';
if ( a->isValueRequired() )
{
std::string arg = a->shortID();
@@ -264,7 +256,6 @@ inline void ZshCompletionOutput::printOption(Arg* a, std::string mutex)
}
}
}
std::cout << '\'';
}
@@ -272,18 +263,15 @@ inline std::string ZshCompletionOutput::getMutexList( CmdLineInterface& _cmd, Ar
{
XorHandler xorHandler = _cmd.getXorHandler();
std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
if (a->getName() == "help" || a->getName() == "version")
{
return "(-)";
}
std::ostringstream list;
if ( a->acceptsMultipleValues() )
{
list << '*';
}
for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
{
for ( ArgVectorIterator it = xorList[i].begin();
@@ -309,13 +297,12 @@ inline std::string ZshCompletionOutput::getMutexList( CmdLineInterface& _cmd, Ar
return list.str();
}
}
// wasn't found in xor list
if (!a->getFlag().empty()) {
if (!a->getFlag().empty())
{
list << "(" << a->flagStartChar() << a->getFlag() << ' ' <<
a->nameStartString() << a->getName() << ')';
}
return list.str();
}