Integrated mask into detection code

This commit is contained in:
Matt Hill
2016-03-14 22:28:46 -04:00
parent 3096fa0da2
commit a0dff3d2af
3 changed files with 38 additions and 9 deletions

View File

@@ -29,12 +29,26 @@ namespace alpr
{ {
this->config = config; this->config = config;
// Load the mask specified in the config if it exists
if (config->detection_mask_image.length() > 0 && fileExists(config->detection_mask_image.c_str()))
{
// Load the image mask
if (config->debugDetector)
cout << "Loading detector mask: " << config->detection_mask_image << endl;
Mat newMask = cv::imread(config->detection_mask_image);
setMask(newMask);
}
} }
Detector::~Detector() Detector::~Detector()
{ {
} }
void Detector::setMask(cv::Mat mask) {
detector_mask.setMask(mask);
}
bool Detector::isLoaded() bool Detector::isLoaded()
{ {

View File

@@ -28,6 +28,8 @@
#include "detector_types.h" #include "detector_types.h"
#include "support/timing.h" #include "support/timing.h"
#include "constants.h" #include "constants.h"
#include "detectormask.h"
#include "prewarp.h"
namespace alpr namespace alpr
{ {
@@ -38,16 +40,20 @@ namespace alpr
{ {
public: public:
Detector(Config* config); Detector(Config* config, PreWarp* prewarp);
virtual ~Detector(); virtual ~Detector();
bool isLoaded(); bool isLoaded();
std::vector<PlateRegion> detect(cv::Mat frame); std::vector<PlateRegion> detect(cv::Mat frame);
virtual std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest); virtual std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
//virtual std::vector<cv::Rect> find_plates(cv::Mat frame)=0;
void setMask(cv::Mat mask);
protected: protected:
Config* config; Config* config;
bool loaded; bool loaded;
DetectorMask detector_mask; DetectorMask detector_mask;

View File

@@ -26,11 +26,10 @@ namespace alpr
{ {
DetectorCPU::DetectorCPU(Config* config) : Detector(config) {
DetectorCPU::DetectorCPU(Config* config, PreWarp* prewarp) : Detector(config, prewarp) { DetectorCPU::DetectorCPU(Config* config, PreWarp* prewarp) : Detector(config, prewarp) {
if( this->plate_cascade.load( get_detector_file() ) ) if( this->plate_cascade.load( get_detector_file() ) )
{ {
this->loaded = true; this->loaded = true;
@@ -60,18 +59,27 @@ namespace alpr
frame.copyTo(frame_gray); frame.copyTo(frame_gray);
} }
// Apply the detection mask if it has been specified by the user
if (detector_mask.mask_loaded)
frame_gray = detector_mask.apply_mask(frame_gray);
vector<PlateRegion> detectedRegions; vector<PlateRegion> detectedRegions;
for (int i = 0; i < regionsOfInterest.size(); i++) for (int i = 0; i < regionsOfInterest.size(); i++)
{ {
Rect roi = regionsOfInterest[i];
// Adjust the ROI to be inside the detection mask (if it exists)
if (detector_mask.mask_loaded)
roi = detector_mask.getRoiInsideMask(roi);
// Sanity check. If roi width or height is less than minimum possible plate size, // Sanity check. If roi width or height is less than minimum possible plate size,
// then skip it // then skip it
if ((regionsOfInterest[i].width < config->minPlateSizeWidthPx) || if ((roi.width < config->minPlateSizeWidthPx) ||
(regionsOfInterest[i].height < config->minPlateSizeHeightPx)) (roi.height < config->minPlateSizeHeightPx))
continue; continue;
Mat cropped = frame_gray(regionsOfInterest[i]); Mat cropped = frame_gray(roi);
vector<PlateRegion> subRegions = doCascade(cropped, regionsOfInterest[i].x, regionsOfInterest[i].y); vector<PlateRegion> subRegions = doCascade(cropped, roi.x, roi.y);
for (int j = 0; j < subRegions.size(); j++) for (int j = 0; j < subRegions.size(); j++)
detectedRegions.push_back(subRegions[j]); detectedRegions.push_back(subRegions[j]);
@@ -79,6 +87,7 @@ namespace alpr
return detectedRegions; return detectedRegions;
} }
vector<PlateRegion> DetectorCPU::doCascade(Mat frame, int offset_x, int offset_y) vector<PlateRegion> DetectorCPU::doCascade(Mat frame, int offset_x, int offset_y)
{ {
@@ -102,7 +111,7 @@ namespace alpr
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * scale_factor; float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * scale_factor;
Size minSize(config->minPlateSizeWidthPx * scale_factor, config->minPlateSizeHeightPx * scale_factor); Size minSize(config->minPlateSizeWidthPx * scale_factor, config->minPlateSizeHeightPx * scale_factor);
Size maxSize(maxWidth, maxHeight); Size maxSize(maxWidth, maxHeight);
plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness, plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness,
CV_HAAR_DO_CANNY_PRUNING, CV_HAAR_DO_CANNY_PRUNING,
//0|CV_HAAR_SCALE_IMAGE, //0|CV_HAAR_SCALE_IMAGE,