mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-06 03:46:59 +08:00
Reorganized the detection interface to simplify code and reduce boilerplate
This commit is contained in:
@@ -45,77 +45,24 @@ namespace alpr
|
||||
DetectorCPU::~DetectorCPU() {
|
||||
}
|
||||
|
||||
vector<PlateRegion> DetectorCPU::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
||||
{
|
||||
|
||||
Mat frame_gray;
|
||||
|
||||
if (frame.channels() > 2)
|
||||
{
|
||||
cvtColor( frame, frame_gray, CV_BGR2GRAY );
|
||||
}
|
||||
else
|
||||
{
|
||||
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 ((roi.width < config->minPlateSizeWidthPx) ||
|
||||
(roi.height < config->minPlateSizeHeightPx))
|
||||
continue;
|
||||
|
||||
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]);
|
||||
}
|
||||
return detectedRegions;
|
||||
}
|
||||
|
||||
|
||||
vector<PlateRegion> DetectorCPU::doCascade(Mat frame, int offset_x, int offset_y)
|
||||
vector<Rect> DetectorCPU::find_plates(Mat frame, cv::Size min_plate_size, cv::Size max_plate_size)
|
||||
{
|
||||
|
||||
int w = frame.size().width;
|
||||
int h = frame.size().height;
|
||||
|
||||
float scale_factor = computeScaleFactor(w, h);
|
||||
|
||||
vector<Rect> plates;
|
||||
|
||||
equalizeHist( frame, frame );
|
||||
|
||||
if (scale_factor != 1.0)
|
||||
resize(frame, frame, Size(w * scale_factor, h * scale_factor));
|
||||
|
||||
|
||||
//-- Detect plates
|
||||
timespec startTime;
|
||||
getTimeMonotonic(&startTime);
|
||||
|
||||
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 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 maxSize(maxWidth, maxHeight);
|
||||
equalizeHist( frame, frame );
|
||||
|
||||
plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
||||
CV_HAAR_DO_CANNY_PRUNING,
|
||||
//0|CV_HAAR_SCALE_IMAGE,
|
||||
minSize, maxSize );
|
||||
min_plate_size, max_plate_size );
|
||||
|
||||
|
||||
if (config->debugTiming)
|
||||
@@ -125,23 +72,7 @@ namespace alpr
|
||||
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
||||
}
|
||||
|
||||
for( unsigned int i = 0; i < plates.size(); i++ )
|
||||
{
|
||||
plates[i].x = (plates[i].x / scale_factor);
|
||||
plates[i].y = (plates[i].y / scale_factor);
|
||||
plates[i].width = plates[i].width / scale_factor;
|
||||
plates[i].height = plates[i].height / scale_factor;
|
||||
|
||||
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
||||
plates[i] = expandRect(plates[i], 0, 0, w, h);
|
||||
|
||||
plates[i].x = plates[i].x + offset_x;
|
||||
plates[i].y = plates[i].y + offset_y;
|
||||
}
|
||||
|
||||
vector<PlateRegion> orderedRegions = aggregateRegions(plates);
|
||||
|
||||
return orderedRegions;
|
||||
return plates;
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user