/*
* Copyright (c) 2013 New Designs Unlimited, LLC
* Opensource Automated License Plate Recognition [http://www.openalpr.com]
*
* This file is part of OpenAlpr.
*
* OpenAlpr is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License
* version 3 as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
#ifndef OPENALPR_CHARACTERSEGMENTER_H
#define OPENALPR_CHARACTERSEGMENTER_H
#include "opencv2/imgproc/imgproc.hpp"
#include "constants.h"
#include "binarize_wolf.h"
#include "utility.h"
#include "characterregion.h"
#include "colorfilter.h"
#include "verticalhistogram.h"
#include "config.h"
//const float MIN_BOX_WIDTH_PX = 4; // 4 pixels
const cv::Scalar COLOR_DEBUG_EDGE(0,0,255); // Red
const cv::Scalar COLOR_DEBUG_SPECKLES(0,0,255); // Red
const cv::Scalar COLOR_DEBUG_MIN_HEIGHT(255,0,0); // Blue
const cv::Scalar COLOR_DEBUG_MIN_AREA(255,0,0); // Blue
const cv::Scalar COLOR_DEBUG_FULLBOX(255,255,0); // Blue-green
const cv::Scalar COLOR_DEBUG_COLORFILTER(255,0,255); // Magenta
const cv::Scalar COLOR_DEBUG_EMPTYFILTER(0,255,255); // Yellow
class CharacterSegmenter
{
public:
CharacterSegmenter(PipelineData* pipeline_data);
virtual ~CharacterSegmenter();
int confidence;
private:
Config* config;
PipelineData* pipeline_data;
CharacterAnalysis* charAnalysis;
LineSegment top;
LineSegment bottom;
std::vector imgDbgGeneral;
std::vector imgDbgCleanStages;
std::vector filter(cv::Mat img, std::vector > contours, std::vector hierarchy);
std::vector filterByBoxSize(std::vector< std::vector > contours, std::vector goodIndices, float minHeightPx, float maxHeightPx);
std::vector filterBetweenLines(cv::Mat img, std::vector > contours, std::vector hierarchy, std::vector outerPolygon, std::vector goodIndices);
std::vector filterContourHoles(std::vector > contours, std::vector hierarchy, std::vector goodIndices);
std::vector getBestVotedLines(cv::Mat img, std::vector > contours, std::vector goodIndices);
int getGoodIndicesCount(std::vector goodIndices);
cv::Mat getCharacterMask(cv::Mat img_threshold, std::vector > contours, std::vector hierarchy, std::vector goodIndices);
cv::Mat getCharBoxMask(cv::Mat img_threshold, std::vector charBoxes);
void removeSmallContours(std::vector thresholds, std::vector > > allContours, float avgCharWidth, float avgCharHeight);
cv::Mat getVerticalHistogram(cv::Mat img, cv::Mat mask);
std::vector getHistogramBoxes(VerticalHistogram histogram, float avgCharWidth, float avgCharHeight, float* score);
std::vector getBestCharBoxes(cv::Mat img, std::vector charBoxes, float avgCharWidth);
std::vector combineCloseBoxes( std::vector charBoxes, float avgCharWidth);
std::vector get1DHits(cv::Mat img, int yOffset);
void cleanCharRegions(std::vector thresholds, std::vector charRegions);
void cleanBasedOnColor(std::vector thresholds, cv::Mat colorMask, std::vector charRegions);
void cleanMostlyFullBoxes(std::vector thresholds, const std::vector charRegions);
std::vector filterMostlyEmptyBoxes(std::vector thresholds, const std::vector charRegions);
void filterEdgeBoxes(std::vector thresholds, const std::vector charRegions, float avgCharWidth, float avgCharHeight);
int getLongestBlobLengthBetweenLines(cv::Mat img, int col);
int isSkinnyLineInsideBox(cv::Mat threshold, cv::Rect box, std::vector > contours, std::vector hierarchy, float avgCharWidth, float avgCharHeight);
std::vector getEncapsulatingLines(cv::Mat img, std::vector > contours, std::vector goodIndices);
};
#endif // OPENALPR_CHARACTERSEGMENTER_H