mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-06 06:46:53 +08:00
Integrated mask into detection code
This commit is contained in:
@@ -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()
|
||||
{
|
||||
|
@@ -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<PlateRegion> detect(cv::Mat frame);
|
||||
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:
|
||||
Config* config;
|
||||
|
||||
|
||||
bool loaded;
|
||||
|
||||
DetectorMask detector_mask;
|
||||
|
@@ -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<PlateRegion> 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<PlateRegion> subRegions = doCascade(cropped, regionsOfInterest[i].x, regionsOfInterest[i].y);
|
||||
Mat cropped = frame_gray(roi);
|
||||
vector<PlateRegion> 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<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;
|
||||
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,
|
||||
|
Reference in New Issue
Block a user