diff --git a/src/openalpr/detection/detector.cpp b/src/openalpr/detection/detector.cpp index 42a4c62..905eb09 100644 --- a/src/openalpr/detection/detector.cpp +++ b/src/openalpr/detection/detector.cpp @@ -29,12 +29,26 @@ namespace alpr { 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() { } + + void Detector::setMask(cv::Mat mask) { + detector_mask.setMask(mask); + } bool Detector::isLoaded() { diff --git a/src/openalpr/detection/detector.h b/src/openalpr/detection/detector.h index d20bfc3..5457530 100644 --- a/src/openalpr/detection/detector.h +++ b/src/openalpr/detection/detector.h @@ -28,6 +28,8 @@ #include "detector_types.h" #include "support/timing.h" #include "constants.h" +#include "detectormask.h" +#include "prewarp.h" namespace alpr { @@ -38,16 +40,20 @@ namespace alpr { public: - Detector(Config* config); + Detector(Config* config, PreWarp* prewarp); virtual ~Detector(); bool isLoaded(); std::vector detect(cv::Mat frame); virtual std::vector detect(cv::Mat frame, std::vector regionsOfInterest); + //virtual std::vector find_plates(cv::Mat frame)=0; + + void setMask(cv::Mat mask); + protected: Config* config; - + bool loaded; DetectorMask detector_mask; diff --git a/src/openalpr/detection/detectorcpu.cpp b/src/openalpr/detection/detectorcpu.cpp index 92ff337..14d25e5 100644 --- a/src/openalpr/detection/detectorcpu.cpp +++ b/src/openalpr/detection/detectorcpu.cpp @@ -26,11 +26,10 @@ namespace alpr { - DetectorCPU::DetectorCPU(Config* config) : Detector(config) { - DetectorCPU::DetectorCPU(Config* config, PreWarp* prewarp) : Detector(config, prewarp) { + if( this->plate_cascade.load( get_detector_file() ) ) { this->loaded = true; @@ -60,18 +59,27 @@ namespace alpr 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 detectedRegions; 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, // then skip it - if ((regionsOfInterest[i].width < config->minPlateSizeWidthPx) || - (regionsOfInterest[i].height < config->minPlateSizeHeightPx)) + if ((roi.width < config->minPlateSizeWidthPx) || + (roi.height < config->minPlateSizeHeightPx)) continue; - Mat cropped = frame_gray(regionsOfInterest[i]); - vector subRegions = doCascade(cropped, regionsOfInterest[i].x, regionsOfInterest[i].y); + Mat cropped = frame_gray(roi); + vector subRegions = doCascade(cropped, roi.x, roi.y); for (int j = 0; j < subRegions.size(); j++) detectedRegions.push_back(subRegions[j]); @@ -79,6 +87,7 @@ namespace alpr return detectedRegions; } + vector 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; Size minSize(config->minPlateSizeWidthPx * scale_factor, config->minPlateSizeHeightPx * scale_factor); Size maxSize(maxWidth, maxHeight); - + plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness, CV_HAAR_DO_CANNY_PRUNING, //0|CV_HAAR_SCALE_IMAGE,