mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-06 08:56:55 +08:00
Reorganized the detection interface to simplify code and reduce boilerplate
This commit is contained in:
@@ -64,9 +64,80 @@ namespace alpr
|
|||||||
|
|
||||||
vector<PlateRegion> Detector::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
vector<PlateRegion> Detector::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
||||||
{
|
{
|
||||||
// Must be implemented by subclass
|
|
||||||
std::vector<PlateRegion> rois;
|
Mat frame_gray;
|
||||||
return rois;
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
int w = cropped.size().width;
|
||||||
|
int h = cropped.size().height;
|
||||||
|
int offset_x = roi.x;
|
||||||
|
int offset_y = roi.y;
|
||||||
|
float scale_factor = computeScaleFactor(w, h);
|
||||||
|
|
||||||
|
if (scale_factor != 1.0)
|
||||||
|
resize(cropped, cropped, Size(w * scale_factor, h * scale_factor));
|
||||||
|
|
||||||
|
|
||||||
|
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * scale_factor;
|
||||||
|
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * scale_factor;
|
||||||
|
Size minPlateSize(config->minPlateSizeWidthPx * scale_factor, config->minPlateSizeHeightPx * scale_factor);
|
||||||
|
Size maxPlateSize(maxWidth, maxHeight);
|
||||||
|
|
||||||
|
vector<Rect> allRegions = find_plates(cropped, minPlateSize, maxPlateSize);
|
||||||
|
|
||||||
|
// Aggregate the Rect regions into a hierarchical representation
|
||||||
|
for( unsigned int i = 0; i < allRegions.size(); i++ )
|
||||||
|
{
|
||||||
|
allRegions[i].x = (allRegions[i].x / scale_factor);
|
||||||
|
allRegions[i].y = (allRegions[i].y / scale_factor);
|
||||||
|
allRegions[i].width = allRegions[i].width / scale_factor;
|
||||||
|
allRegions[i].height = allRegions[i].height / scale_factor;
|
||||||
|
|
||||||
|
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
||||||
|
allRegions[i] = expandRect(allRegions[i], 0, 0, w, h);
|
||||||
|
|
||||||
|
allRegions[i].x = allRegions[i].x + offset_x;
|
||||||
|
allRegions[i].y = allRegions[i].y + offset_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<PlateRegion> orderedRegions = aggregateRegions(allRegions);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (int j = 0; j < orderedRegions.size(); j++)
|
||||||
|
detectedRegions.push_back(orderedRegions[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return detectedRegions;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Detector::get_detector_file() {
|
std::string Detector::get_detector_file() {
|
||||||
|
@@ -45,9 +45,9 @@ namespace alpr
|
|||||||
|
|
||||||
bool isLoaded();
|
bool isLoaded();
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame);
|
std::vector<PlateRegion> detect(cv::Mat frame);
|
||||||
virtual std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
||||||
|
|
||||||
//virtual std::vector<cv::Rect> find_plates(cv::Mat frame)=0;
|
virtual std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size)=0;
|
||||||
|
|
||||||
void setMask(cv::Mat mask);
|
void setMask(cv::Mat mask);
|
||||||
|
|
||||||
|
@@ -45,77 +45,24 @@ namespace alpr
|
|||||||
DetectorCPU::~DetectorCPU() {
|
DetectorCPU::~DetectorCPU() {
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<PlateRegion> DetectorCPU::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
|
||||||
|
|
||||||
|
|
||||||
|
vector<Rect> DetectorCPU::find_plates(Mat frame, cv::Size min_plate_size, cv::Size max_plate_size)
|
||||||
{
|
{
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
|
|
||||||
int w = frame.size().width;
|
|
||||||
int h = frame.size().height;
|
|
||||||
|
|
||||||
float scale_factor = computeScaleFactor(w, h);
|
|
||||||
|
|
||||||
vector<Rect> plates;
|
vector<Rect> plates;
|
||||||
|
|
||||||
equalizeHist( frame, frame );
|
|
||||||
|
|
||||||
if (scale_factor != 1.0)
|
|
||||||
resize(frame, frame, Size(w * scale_factor, h * scale_factor));
|
|
||||||
|
|
||||||
//-- Detect plates
|
//-- Detect plates
|
||||||
timespec startTime;
|
timespec startTime;
|
||||||
getTimeMonotonic(&startTime);
|
getTimeMonotonic(&startTime);
|
||||||
|
|
||||||
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * scale_factor;
|
equalizeHist( frame, frame );
|
||||||
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,
|
plate_cascade.detectMultiScale( frame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
||||||
CV_HAAR_DO_CANNY_PRUNING,
|
CV_HAAR_DO_CANNY_PRUNING,
|
||||||
//0|CV_HAAR_SCALE_IMAGE,
|
//0|CV_HAAR_SCALE_IMAGE,
|
||||||
minSize, maxSize );
|
min_plate_size, max_plate_size );
|
||||||
|
|
||||||
|
|
||||||
if (config->debugTiming)
|
if (config->debugTiming)
|
||||||
@@ -125,23 +72,7 @@ namespace alpr
|
|||||||
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( unsigned int i = 0; i < plates.size(); i++ )
|
return plates;
|
||||||
{
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -39,13 +39,12 @@ namespace alpr
|
|||||||
DetectorCPU(Config* config, PreWarp* prewarp);
|
DetectorCPU(Config* config, PreWarp* prewarp);
|
||||||
virtual ~DetectorCPU();
|
virtual ~DetectorCPU();
|
||||||
|
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
cv::CascadeClassifier plate_cascade;
|
cv::CascadeClassifier plate_cascade;
|
||||||
|
|
||||||
std::vector<PlateRegion> doCascade(cv::Mat frame, int offset_x, int offset_y);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
#include "detectorcuda.h"
|
#include "detectorcuda.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef COMPILE_GPU
|
#ifdef COMPILE_GPU
|
||||||
|
|
||||||
using namespace cv;
|
using namespace cv;
|
||||||
@@ -49,60 +48,22 @@ namespace alpr
|
|||||||
DetectorCUDA::~DetectorCUDA() {
|
DetectorCUDA::~DetectorCUDA() {
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<PlateRegion> DetectorCUDA::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
vector<Rect> DetectorCUDA::find_plates(Mat frame, cv::Size min_plate_size, cv::Size max_plate_size)
|
||||||
{
|
{
|
||||||
|
//-- Detect plates
|
||||||
Mat frame_gray;
|
|
||||||
|
|
||||||
if (frame.channels() > 2)
|
|
||||||
{
|
|
||||||
cvtColor( frame, frame_gray, CV_BGR2GRAY );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
frame.copyTo(frame_gray);
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<PlateRegion> detectedRegions;
|
|
||||||
for (int i = 0; i < regionsOfInterest.size(); i++)
|
|
||||||
{
|
|
||||||
Mat cropped = frame_gray(regionsOfInterest[i]);
|
|
||||||
vector<PlateRegion> subRegions = doCascade(cropped, regionsOfInterest[i].x, regionsOfInterest[i].y);
|
|
||||||
|
|
||||||
for (int j = 0; j < subRegions.size(); j++)
|
|
||||||
detectedRegions.push_back(subRegions[j]);
|
|
||||||
}
|
|
||||||
return detectedRegions;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<PlateRegion> DetectorCUDA::doCascade(Mat frame, int offset_x, int offset_y)
|
|
||||||
{
|
|
||||||
|
|
||||||
int w = frame.size().width;
|
|
||||||
int h = frame.size().height;
|
|
||||||
|
|
||||||
float scale_factor = computeScaleFactor(w, h);
|
|
||||||
|
|
||||||
vector<Rect> plates;
|
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;
|
timespec startTime;
|
||||||
getTimeMonotonic(&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);
|
|
||||||
|
|
||||||
gpu::GpuMat cudaFrame, plateregions_buffer;
|
gpu::GpuMat cudaFrame, plateregions_buffer;
|
||||||
Mat plateregions_downloaded;
|
Mat plateregions_downloaded;
|
||||||
|
|
||||||
cudaFrame.upload(frame);
|
cudaFrame.upload(frame);
|
||||||
int numdetected = cuda_cascade.detectMultiScale(cudaFrame, plateregions_buffer, (double) config->detection_iteration_increase, config->detectionStrictness, minSize);
|
int numdetected = cuda_cascade.detectMultiScale(cudaFrame, plateregions_buffer,
|
||||||
|
(double) config->detection_iteration_increase, config->detectionStrictness,
|
||||||
|
min_plate_size);
|
||||||
|
|
||||||
plateregions_buffer.colRange(0, numdetected).download(plateregions_downloaded);
|
plateregions_buffer.colRange(0, numdetected).download(plateregions_downloaded);
|
||||||
|
|
||||||
for (int i = 0; i < numdetected; ++i)
|
for (int i = 0; i < numdetected; ++i)
|
||||||
@@ -110,8 +71,6 @@ namespace alpr
|
|||||||
plates.push_back(plateregions_downloaded.ptr<cv::Rect>()[i]);
|
plates.push_back(plateregions_downloaded.ptr<cv::Rect>()[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (config->debugTiming)
|
if (config->debugTiming)
|
||||||
{
|
{
|
||||||
timespec endTime;
|
timespec endTime;
|
||||||
@@ -119,24 +78,7 @@ namespace alpr
|
|||||||
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( unsigned int i = 0; i < plates.size(); i++ )
|
return plates;
|
||||||
{
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -46,13 +46,12 @@ namespace alpr
|
|||||||
DetectorCUDA(Config* config, PreWarp* prewarp);
|
DetectorCUDA(Config* config, PreWarp* prewarp);
|
||||||
virtual ~DetectorCUDA();
|
virtual ~DetectorCUDA();
|
||||||
|
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
cv::gpu::CascadeClassifier_GPU cuda_cascade;
|
cv::gpu::CascadeClassifier_GPU cuda_cascade;
|
||||||
|
|
||||||
std::vector<PlateRegion> doCascade(cv::Mat frame, int offset_x, int offset_y);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -47,171 +47,160 @@ namespace alpr {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<PlateRegion> DetectorMorph::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest) {
|
std::vector<cv::Rect> DetectorMorph::find_plates(cv::Mat frame_gray, cv::Size min_plate_size, cv::Size max_plate_size)
|
||||||
|
{
|
||||||
Mat frame_gray,frame_gray_cp;
|
|
||||||
|
|
||||||
if (frame.channels() > 2)
|
|
||||||
{
|
|
||||||
cvtColor( frame, frame_gray, CV_BGR2GRAY );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
frame.copyTo(frame_gray);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Mat frame_gray_cp(frame_gray.size(), frame_gray.type());
|
||||||
frame_gray.copyTo(frame_gray_cp);
|
frame_gray.copyTo(frame_gray_cp);
|
||||||
blur(frame_gray, frame_gray, Size(5, 5));
|
blur(frame_gray, frame_gray, Size(5, 5));
|
||||||
|
|
||||||
vector<PlateRegion> detectedRegions;
|
vector<Rect> plates;
|
||||||
for (int i = 0; i < regionsOfInterest.size(); i++) {
|
|
||||||
Mat img_open, img_result;
|
|
||||||
Mat element = getStructuringElement(MORPH_RECT, Size(30, 4));
|
|
||||||
morphologyEx(frame_gray, img_open, CV_MOP_OPEN, element, cv::Point(-1, -1));
|
|
||||||
|
|
||||||
img_result = frame_gray - img_open;
|
Mat img_open, img_result;
|
||||||
|
Mat element = getStructuringElement(MORPH_RECT, Size(30, 4));
|
||||||
|
morphologyEx(frame_gray, img_open, CV_MOP_OPEN, element, cv::Point(-1, -1));
|
||||||
|
|
||||||
if (config->debugDetector && config->debugShowImages) {
|
img_result = frame_gray - img_open;
|
||||||
imshow("Opening", img_result);
|
|
||||||
|
if (config->debugDetector && config->debugShowImages) {
|
||||||
|
imshow("Opening", img_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
//threshold image using otsu thresholding
|
||||||
|
Mat img_threshold, img_open2;
|
||||||
|
threshold(img_result, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
|
||||||
|
|
||||||
|
if (config->debugDetector && config->debugShowImages) {
|
||||||
|
imshow("Threshold Detector", img_threshold);
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat diamond(5, 5, CV_8U, cv::Scalar(1));
|
||||||
|
|
||||||
|
diamond.at<uchar>(0, 0) = 0;
|
||||||
|
diamond.at<uchar>(0, 1) = 0;
|
||||||
|
diamond.at<uchar>(1, 0) = 0;
|
||||||
|
diamond.at<uchar>(4, 4) = 0;
|
||||||
|
diamond.at<uchar>(3, 4) = 0;
|
||||||
|
diamond.at<uchar>(4, 3) = 0;
|
||||||
|
diamond.at<uchar>(4, 0) = 0;
|
||||||
|
diamond.at<uchar>(4, 1) = 0;
|
||||||
|
diamond.at<uchar>(3, 0) = 0;
|
||||||
|
diamond.at<uchar>(0, 4) = 0;
|
||||||
|
diamond.at<uchar>(0, 3) = 0;
|
||||||
|
diamond.at<uchar>(1, 4) = 0;
|
||||||
|
|
||||||
|
morphologyEx(img_threshold, img_open2, CV_MOP_OPEN, diamond, cv::Point(-1, -1));
|
||||||
|
Mat rectElement = getStructuringElement(cv::MORPH_RECT, Size(13, 4));
|
||||||
|
morphologyEx(img_open2, img_threshold, CV_MOP_CLOSE, rectElement, cv::Point(-1, -1));
|
||||||
|
|
||||||
|
if (config->debugDetector && config->debugShowImages) {
|
||||||
|
imshow("Close", img_threshold);
|
||||||
|
waitKey(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Find contours of possibles plates
|
||||||
|
vector< vector< Point> > contours;
|
||||||
|
findContours(img_threshold,
|
||||||
|
contours, // a vector of contours
|
||||||
|
CV_RETR_EXTERNAL, // retrieve the external contours
|
||||||
|
CV_CHAIN_APPROX_NONE); // all pixels of each contours
|
||||||
|
|
||||||
|
//Start to iterate to each contour founded
|
||||||
|
vector<vector<Point> >::iterator itc = contours.begin();
|
||||||
|
vector<RotatedRect> rects;
|
||||||
|
|
||||||
|
//Remove patch that are no inside limits of aspect ratio and area.
|
||||||
|
while (itc != contours.end()) {
|
||||||
|
//Create bounding rect of object
|
||||||
|
RotatedRect mr = minAreaRect(Mat(*itc));
|
||||||
|
|
||||||
|
if (mr.angle < -45.) {
|
||||||
|
mr.angle += 90.0;
|
||||||
|
swap(mr.size.width, mr.size.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CheckSizes(mr))
|
||||||
|
itc = contours.erase(itc);
|
||||||
|
else {
|
||||||
|
++itc;
|
||||||
|
rects.push_back(mr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Now prunning based on checking all candidate plates for a min/max number of blobsc
|
||||||
|
Mat img_crop, img_crop_b, img_crop_th, img_crop_th_inv;
|
||||||
|
vector< vector< Point> > plateBlobs;
|
||||||
|
vector< vector< Point> > plateBlobsInv;
|
||||||
|
double thresholds[] = { 10, 40, 80, 120, 160, 200, 240 };
|
||||||
|
const int num_thresholds = 7;
|
||||||
|
int numValidChars = 0;
|
||||||
|
Mat rotated;
|
||||||
|
for (int i = 0; i < rects.size(); i++) {
|
||||||
|
numValidChars = 0;
|
||||||
|
RotatedRect PlateRect = rects[i];
|
||||||
|
Size rect_size = PlateRect.size;
|
||||||
|
|
||||||
|
// get the rotation matrix
|
||||||
|
Mat M = getRotationMatrix2D(PlateRect.center, PlateRect.angle, 1.0);
|
||||||
|
// perform the affine transformation
|
||||||
|
warpAffine(frame_gray_cp, rotated, M, frame_gray_cp.size(), INTER_CUBIC);
|
||||||
|
//Crop area around candidate plate
|
||||||
|
getRectSubPix(rotated, rect_size, PlateRect.center, img_crop);
|
||||||
|
|
||||||
|
if (config->debugDetector && config->debugShowImages) {
|
||||||
|
imshow("Tilt Correction", img_crop);
|
||||||
|
waitKey(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
//threshold image using otsu thresholding
|
for (int z = 0; z < num_thresholds; z++) {
|
||||||
Mat img_threshold, img_open2;
|
|
||||||
threshold(img_result, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
|
|
||||||
|
|
||||||
if (config->debugDetector && config->debugShowImages) {
|
cv::threshold(img_crop, img_crop_th, thresholds[z], 255, cv::THRESH_BINARY);
|
||||||
imshow("Threshold Detector", img_threshold);
|
cv::threshold(img_crop, img_crop_th_inv, thresholds[z], 255, cv::THRESH_BINARY_INV);
|
||||||
}
|
|
||||||
|
|
||||||
Mat diamond(5, 5, CV_8U, cv::Scalar(1));
|
findContours(img_crop_th,
|
||||||
|
plateBlobs, // a vector of contours
|
||||||
|
CV_RETR_LIST, // retrieve the contour list
|
||||||
|
CV_CHAIN_APPROX_NONE); // all pixels of each contours
|
||||||
|
|
||||||
diamond.at<uchar>(0, 0) = 0;
|
findContours(img_crop_th_inv,
|
||||||
diamond.at<uchar>(0, 1) = 0;
|
plateBlobsInv, // a vector of contours
|
||||||
diamond.at<uchar>(1, 0) = 0;
|
CV_RETR_LIST, // retrieve the contour list
|
||||||
diamond.at<uchar>(4, 4) = 0;
|
CV_CHAIN_APPROX_NONE); // all pixels of each contours
|
||||||
diamond.at<uchar>(3, 4) = 0;
|
|
||||||
diamond.at<uchar>(4, 3) = 0;
|
|
||||||
diamond.at<uchar>(4, 0) = 0;
|
|
||||||
diamond.at<uchar>(4, 1) = 0;
|
|
||||||
diamond.at<uchar>(3, 0) = 0;
|
|
||||||
diamond.at<uchar>(0, 4) = 0;
|
|
||||||
diamond.at<uchar>(0, 3) = 0;
|
|
||||||
diamond.at<uchar>(1, 4) = 0;
|
|
||||||
|
|
||||||
morphologyEx(img_threshold, img_open2, CV_MOP_OPEN, diamond, cv::Point(-1, -1));
|
int numBlobs = plateBlobs.size();
|
||||||
Mat rectElement = getStructuringElement(cv::MORPH_RECT, Size(13, 4));
|
int numBlobsInv = plateBlobsInv.size();
|
||||||
morphologyEx(img_open2, img_threshold, CV_MOP_CLOSE, rectElement, cv::Point(-1, -1));
|
|
||||||
|
|
||||||
if (config->debugDetector && config->debugShowImages) {
|
float idealAspect = config->avgCharWidthMM / config->avgCharHeightMM;
|
||||||
imshow("Close", img_threshold);
|
for (int j = 0; j < numBlobs; j++) {
|
||||||
waitKey(0);
|
cv::Rect r0 = cv::boundingRect(cv::Mat(plateBlobs[j]));
|
||||||
}
|
|
||||||
|
|
||||||
//Find contours of possibles plates
|
if (ValidateCharAspect(r0, idealAspect))
|
||||||
vector< vector< Point> > contours;
|
numValidChars++;
|
||||||
findContours(img_threshold,
|
}
|
||||||
contours, // a vector of contours
|
|
||||||
CV_RETR_EXTERNAL, // retrieve the external contours
|
|
||||||
CV_CHAIN_APPROX_NONE); // all pixels of each contours
|
|
||||||
|
|
||||||
//Start to iterate to each contour founded
|
for (int j = 0; j < numBlobsInv; j++) {
|
||||||
vector<vector<Point> >::iterator itc = contours.begin();
|
cv::Rect r0 = cv::boundingRect(cv::Mat(plateBlobsInv[j]));
|
||||||
vector<RotatedRect> rects;
|
if (ValidateCharAspect(r0, idealAspect))
|
||||||
|
numValidChars++;
|
||||||
//Remove patch that are no inside limits of aspect ratio and area.
|
}
|
||||||
while (itc != contours.end()) {
|
|
||||||
//Create bounding rect of object
|
|
||||||
RotatedRect mr = minAreaRect(Mat(*itc));
|
|
||||||
|
|
||||||
if (mr.angle < -45.) {
|
|
||||||
mr.angle += 90.0;
|
|
||||||
swap(mr.size.width, mr.size.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!CheckSizes(mr))
|
|
||||||
itc = contours.erase(itc);
|
|
||||||
else {
|
|
||||||
++itc;
|
|
||||||
rects.push_back(mr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Now prunning based on checking all candidate plates for a min/max number of blobsc
|
|
||||||
Mat img_crop, img_crop_b, img_crop_th, img_crop_th_inv;
|
|
||||||
vector< vector< Point> > plateBlobs;
|
|
||||||
vector< vector< Point> > plateBlobsInv;
|
|
||||||
double thresholds[] = { 10, 40, 80, 120, 160, 200, 240 };
|
|
||||||
const int num_thresholds = 7;
|
|
||||||
int numValidChars = 0;
|
|
||||||
Mat rotated;
|
|
||||||
for (int i = 0; i < rects.size(); i++) {
|
|
||||||
numValidChars = 0;
|
|
||||||
RotatedRect PlateRect = rects[i];
|
|
||||||
Size rect_size = PlateRect.size;
|
|
||||||
|
|
||||||
// get the rotation matrix
|
|
||||||
Mat M = getRotationMatrix2D(PlateRect.center, PlateRect.angle, 1.0);
|
|
||||||
// perform the affine transformation
|
|
||||||
warpAffine(frame_gray_cp, rotated, M, frame_gray_cp.size(), INTER_CUBIC);
|
|
||||||
//Crop area around candidate plate
|
|
||||||
getRectSubPix(rotated, rect_size, PlateRect.center, img_crop);
|
|
||||||
|
|
||||||
if (config->debugDetector && config->debugShowImages) {
|
|
||||||
imshow("Tilt Correction", img_crop);
|
|
||||||
waitKey(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int z = 0; z < num_thresholds; z++) {
|
|
||||||
|
|
||||||
cv::threshold(img_crop, img_crop_th, thresholds[z], 255, cv::THRESH_BINARY);
|
|
||||||
cv::threshold(img_crop, img_crop_th_inv, thresholds[z], 255, cv::THRESH_BINARY_INV);
|
|
||||||
|
|
||||||
findContours(img_crop_th,
|
|
||||||
plateBlobs, // a vector of contours
|
|
||||||
CV_RETR_LIST, // retrieve the contour list
|
|
||||||
CV_CHAIN_APPROX_NONE); // all pixels of each contours
|
|
||||||
|
|
||||||
findContours(img_crop_th_inv,
|
|
||||||
plateBlobsInv, // a vector of contours
|
|
||||||
CV_RETR_LIST, // retrieve the contour list
|
|
||||||
CV_CHAIN_APPROX_NONE); // all pixels of each contours
|
|
||||||
|
|
||||||
int numBlobs = plateBlobs.size();
|
|
||||||
int numBlobsInv = plateBlobsInv.size();
|
|
||||||
|
|
||||||
float idealAspect = config->avgCharWidthMM / config->avgCharHeightMM;
|
|
||||||
for (int j = 0; j < numBlobs; j++) {
|
|
||||||
cv::Rect r0 = cv::boundingRect(cv::Mat(plateBlobs[j]));
|
|
||||||
|
|
||||||
if (ValidateCharAspect(r0, idealAspect))
|
|
||||||
numValidChars++;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int j = 0; j < numBlobsInv; j++) {
|
|
||||||
cv::Rect r0 = cv::boundingRect(cv::Mat(plateBlobsInv[j]));
|
|
||||||
if (ValidateCharAspect(r0, idealAspect))
|
|
||||||
numValidChars++;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
//If too much or too lcittle might not be a true plate
|
|
||||||
//if (numBlobs < 3 || numBlobs > 50) continue;
|
|
||||||
if (numValidChars < 4 || numValidChars > 50) continue;
|
|
||||||
|
|
||||||
PlateRegion PlateReg;
|
|
||||||
|
|
||||||
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
|
||||||
Rect bounding_rect = PlateRect.boundingRect();
|
|
||||||
PlateReg.rect = expandRect(bounding_rect, 0, 0, frame.cols, frame.rows);
|
|
||||||
|
|
||||||
|
|
||||||
detectedRegions.push_back(PlateReg);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//If too much or too lcittle might not be a true plate
|
||||||
|
//if (numBlobs < 3 || numBlobs > 50) continue;
|
||||||
|
if (numValidChars < 4 || numValidChars > 50) continue;
|
||||||
|
|
||||||
|
PlateRegion PlateReg;
|
||||||
|
|
||||||
|
// Ensure that the rectangle isn't < 0 or > maxWidth/Height
|
||||||
|
Rect bounding_rect = PlateRect.boundingRect();
|
||||||
|
|
||||||
|
Rect rect_expanded = expandRect(bounding_rect, 0, 0, frame_gray.cols, frame_gray.rows);
|
||||||
|
|
||||||
|
plates.push_back(rect_expanded);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return detectedRegions;
|
return plates;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DetectorMorph::CheckSizes(RotatedRect& mr) {
|
bool DetectorMorph::CheckSizes(RotatedRect& mr) {
|
||||||
|
@@ -38,7 +38,7 @@ namespace alpr {
|
|||||||
DetectorMorph(Config* config, PreWarp* prewarp);
|
DetectorMorph(Config* config, PreWarp* prewarp);
|
||||||
virtual ~DetectorMorph();
|
virtual ~DetectorMorph();
|
||||||
|
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool CheckSizes(cv::RotatedRect& mr);
|
bool CheckSizes(cv::RotatedRect& mr);
|
||||||
|
@@ -98,47 +98,10 @@ namespace alpr
|
|||||||
DetectorOCL::~DetectorOCL() {
|
DetectorOCL::~DetectorOCL() {
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<PlateRegion> DetectorOCL::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest)
|
|
||||||
|
vector<Rect> DetectorOCL::find_plates(Mat orig_frame, cv::Size min_plate_size, cv::Size max_plate_size)
|
||||||
{
|
{
|
||||||
|
|
||||||
Mat frame_gray;
|
|
||||||
|
|
||||||
if (frame.channels() > 2)
|
|
||||||
{
|
|
||||||
cvtColor( frame, frame_gray, CV_BGR2GRAY );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
frame.copyTo(frame_gray);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
vector<PlateRegion> detectedRegions;
|
|
||||||
for (int i = 0; i < regionsOfInterest.size(); i++)
|
|
||||||
{
|
|
||||||
// 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))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Mat cropped = frame_gray(regionsOfInterest[i]);
|
|
||||||
vector<PlateRegion> subRegions = doCascade(cropped, regionsOfInterest[i].x, regionsOfInterest[i].y);
|
|
||||||
|
|
||||||
for (int j = 0; j < subRegions.size(); j++)
|
|
||||||
detectedRegions.push_back(subRegions[j]);
|
|
||||||
}
|
|
||||||
return detectedRegions;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<PlateRegion> DetectorOCL::doCascade(Mat orig_frame, int offset_x, int offset_y)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
int w = orig_frame.size().width;
|
|
||||||
int h = orig_frame.size().height;
|
|
||||||
|
|
||||||
float scale_factor = computeScaleFactor(w, h);
|
|
||||||
|
|
||||||
vector<Rect> plates;
|
vector<Rect> plates;
|
||||||
|
|
||||||
@@ -146,12 +109,6 @@ namespace alpr
|
|||||||
timespec startTime;
|
timespec startTime;
|
||||||
getTimeMonotonic(&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);
|
|
||||||
|
|
||||||
// If we have an OpenCL core available, use it. Otherwise use CPU
|
// If we have an OpenCL core available, use it. Otherwise use CPU
|
||||||
if (ocl_detector_mutex_m.try_lock())
|
if (ocl_detector_mutex_m.try_lock())
|
||||||
{
|
{
|
||||||
@@ -160,12 +117,9 @@ namespace alpr
|
|||||||
|
|
||||||
equalizeHist( openclFrame, openclFrame );
|
equalizeHist( openclFrame, openclFrame );
|
||||||
|
|
||||||
if (scale_factor != 1.0)
|
|
||||||
resize(openclFrame, openclFrame, Size(w * scale_factor, h * scale_factor));
|
|
||||||
|
|
||||||
plate_cascade.detectMultiScale( openclFrame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
plate_cascade.detectMultiScale( openclFrame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
||||||
0,
|
0,
|
||||||
minSize, maxSize );
|
min_plate_size, max_plate_size );
|
||||||
|
|
||||||
ocl_detector_mutex_m.unlock();
|
ocl_detector_mutex_m.unlock();
|
||||||
}
|
}
|
||||||
@@ -173,18 +127,12 @@ namespace alpr
|
|||||||
{
|
{
|
||||||
equalizeHist( orig_frame, orig_frame );
|
equalizeHist( orig_frame, orig_frame );
|
||||||
|
|
||||||
if (scale_factor != 1.0)
|
|
||||||
resize(orig_frame, orig_frame, Size(w * scale_factor, h * scale_factor));
|
|
||||||
|
|
||||||
plate_cascade.detectMultiScale( orig_frame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
plate_cascade.detectMultiScale( orig_frame, plates, config->detection_iteration_increase, config->detectionStrictness,
|
||||||
0,
|
0,
|
||||||
minSize, maxSize );
|
min_plate_size, max_plate_size );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (config->debugTiming)
|
if (config->debugTiming)
|
||||||
{
|
{
|
||||||
timespec endTime;
|
timespec endTime;
|
||||||
@@ -192,23 +140,8 @@ namespace alpr
|
|||||||
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( unsigned int i = 0; i < plates.size(); i++ )
|
return plates;
|
||||||
{
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -42,13 +42,12 @@ namespace alpr
|
|||||||
DetectorOCL(Config* config, PreWarp* prewarp);
|
DetectorOCL(Config* config, PreWarp* prewarp);
|
||||||
virtual ~DetectorOCL();
|
virtual ~DetectorOCL();
|
||||||
|
|
||||||
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
|
std::vector<cv::Rect> find_plates(cv::Mat frame, cv::Size min_plate_size, cv::Size max_plate_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
cv::CascadeClassifier plate_cascade;
|
cv::CascadeClassifier plate_cascade;
|
||||||
|
|
||||||
std::vector<PlateRegion> doCascade(cv::Mat frame, int offset_x, int offset_y);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user