From 72b5af130e4e9faedf16ff56158182bf2db6a45f Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Tue, 28 Apr 2015 06:00:58 -0400 Subject: [PATCH] Fixed sticky scale factor for images of different sizes --- src/openalpr/detection/detector.cpp | 25 ++++++++++++++++++++- src/openalpr/detection/detector.h | 2 +- src/openalpr/detection/detectorcpu.cpp | 30 +++++++------------------ src/openalpr/detection/detectorcuda.cpp | 30 +++++++------------------ 4 files changed, 41 insertions(+), 46 deletions(-) diff --git a/src/openalpr/detection/detector.cpp b/src/openalpr/detection/detector.cpp index 4c7532d..5f2a9bc 100644 --- a/src/openalpr/detection/detector.cpp +++ b/src/openalpr/detection/detector.cpp @@ -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(); }; diff --git a/src/openalpr/detection/detector.h b/src/openalpr/detection/detector.h index b79a888..392fb36 100644 --- a/src/openalpr/detection/detector.h +++ b/src/openalpr/detection/detector.h @@ -53,8 +53,8 @@ namespace alpr Config* config; bool loaded; - float scale_factor; + float computeScaleFactor(int width, int height); std::vector aggregateRegions(std::vector regions); diff --git a/src/openalpr/detection/detectorcpu.cpp b/src/openalpr/detection/detectorcpu.cpp index da0b568..ae6e794 100644 --- a/src/openalpr/detection/detectorcpu.cpp +++ b/src/openalpr/detection/detectorcpu.cpp @@ -75,39 +75,25 @@ namespace alpr vector 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 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, diff --git a/src/openalpr/detection/detectorcuda.cpp b/src/openalpr/detection/detectorcuda.cpp index 40b8720..39dbfc7 100644 --- a/src/openalpr/detection/detectorcuda.cpp +++ b/src/openalpr/detection/detectorcuda.cpp @@ -70,39 +70,25 @@ namespace alpr vector 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 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;