mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-06 00:46:49 +08:00
Fixed sticky scale factor for images of different sizes
This commit is contained in:
@@ -28,7 +28,6 @@ namespace alpr
|
||||
Detector::Detector(Config* config)
|
||||
{
|
||||
this->config = config;
|
||||
this->scale_factor = 1.0f;
|
||||
|
||||
}
|
||||
|
||||
@@ -56,6 +55,30 @@ namespace alpr
|
||||
return rois;
|
||||
}
|
||||
|
||||
float Detector::computeScaleFactor(int width, int height) {
|
||||
|
||||
float scale_factor = 1.0;
|
||||
|
||||
if (width > config->maxDetectionInputWidth)
|
||||
{
|
||||
// The frame is too wide
|
||||
scale_factor = ((float) config->maxDetectionInputWidth) / ((float) width);
|
||||
|
||||
if (config->debugDetector)
|
||||
std::cout << "Input detection image is too wide. Resizing with scale: " << scale_factor << endl;
|
||||
}
|
||||
else if (height > config->maxDetectionInputHeight)
|
||||
{
|
||||
// The frame is too tall
|
||||
scale_factor = ((float) config->maxDetectionInputHeight) / ((float) height);
|
||||
|
||||
if (config->debugDetector)
|
||||
std::cout << "Input detection image is too tall. Resizing with scale: " << scale_factor << endl;
|
||||
}
|
||||
|
||||
return scale_factor;
|
||||
|
||||
}
|
||||
|
||||
bool rectHasLargerArea(cv::Rect a, cv::Rect b) { return a.area() < b.area(); };
|
||||
|
||||
|
@@ -53,8 +53,8 @@ namespace alpr
|
||||
Config* config;
|
||||
|
||||
bool loaded;
|
||||
float scale_factor;
|
||||
|
||||
float computeScaleFactor(int width, int height);
|
||||
std::vector<PlateRegion> aggregateRegions(std::vector<cv::Rect> regions);
|
||||
|
||||
|
||||
|
@@ -75,39 +75,25 @@ namespace alpr
|
||||
vector<PlateRegion> DetectorCPU::doCascade(Mat frame, int offset_x, int offset_y)
|
||||
{
|
||||
|
||||
|
||||
if (frame.cols > config->maxDetectionInputWidth)
|
||||
{
|
||||
// The frame is too wide
|
||||
this->scale_factor = ((float) config->maxDetectionInputWidth) / ((float) frame.cols);
|
||||
|
||||
if (config->debugDetector)
|
||||
std::cout << "Input detection image is too wide. Resizing with scale: " << this->scale_factor << endl;
|
||||
}
|
||||
else if (frame.rows > config->maxDetectionInputHeight)
|
||||
{
|
||||
// The frame is too tall
|
||||
this->scale_factor = ((float) config->maxDetectionInputHeight) / ((float) frame.rows);
|
||||
|
||||
if (config->debugDetector)
|
||||
std::cout << "Input detection image is too tall. Resizing with scale: " << this->scale_factor << endl;
|
||||
}
|
||||
|
||||
int w = frame.size().width;
|
||||
int h = frame.size().height;
|
||||
|
||||
float scale_factor = computeScaleFactor(w, h);
|
||||
|
||||
vector<Rect> plates;
|
||||
|
||||
equalizeHist( frame, frame );
|
||||
resize(frame, frame, Size(w * this->scale_factor, h * this->scale_factor));
|
||||
|
||||
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) * this->scale_factor;
|
||||
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * this->scale_factor;
|
||||
Size minSize(config->minPlateSizeWidthPx * this->scale_factor, config->minPlateSizeHeightPx * this->scale_factor);
|
||||
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);
|
||||
|
||||
plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
||||
|
@@ -70,39 +70,25 @@ namespace alpr
|
||||
vector<PlateRegion> DetectorCUDA::doCascade(Mat frame, int offset_x, int offset_y)
|
||||
{
|
||||
|
||||
|
||||
if (frame.cols > config->maxDetectionInputWidth)
|
||||
{
|
||||
// The frame is too wide
|
||||
this->scale_factor = ((float) config->maxDetectionInputWidth) / ((float) frame.cols);
|
||||
|
||||
if (config->debugDetector)
|
||||
std::cout << "Input detection image is too wide. Resizing with scale: " << this->scale_factor << endl;
|
||||
}
|
||||
else if (frame.rows > config->maxDetectionInputHeight)
|
||||
{
|
||||
// The frame is too tall
|
||||
this->scale_factor = ((float) config->maxDetectionInputHeight) / ((float) frame.rows);
|
||||
|
||||
if (config->debugDetector)
|
||||
std::cout << "Input detection image is too tall. Resizing with scale: " << this->scale_factor << endl;
|
||||
}
|
||||
|
||||
int w = frame.size().width;
|
||||
int h = frame.size().height;
|
||||
|
||||
float scale_factor = computeScaleFactor(w, h);
|
||||
|
||||
vector<Rect> plates;
|
||||
|
||||
equalizeHist( frame, frame );
|
||||
resize(frame, frame, Size(w * this->scale_factor, h * this->scale_factor));
|
||||
|
||||
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) * this->scale_factor;
|
||||
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * this->scale_factor;
|
||||
Size minSize(config->minPlateSizeWidthPx * this->scale_factor, config->minPlateSizeHeightPx * this->scale_factor);
|
||||
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);
|
||||
|
||||
gpu::GpuMat cudaFrame, plateregions_buffer;
|
||||
Mat plateregions_downloaded;
|
||||
|
Reference in New Issue
Block a user