Changed indents to be consistent

This commit is contained in:
Matt Hill
2015-03-18 18:32:07 -04:00
parent c04dd4104d
commit fb3c93a162
2 changed files with 143 additions and 156 deletions

View File

@@ -1,167 +1,155 @@
/* /*
* Copyright (c) 2014 New Designs Unlimited, LLC * Copyright (c) 2014 New Designs Unlimited, LLC
* Opensource Automated License Plate Recognition [http://www.openalpr.com] * Opensource Automated License Plate Recognition [http://www.openalpr.com]
* *
* This file is part of OpenAlpr. * This file is part of OpenAlpr.
* *
* OpenAlpr is free software: you can redistribute it and/or modify * OpenAlpr is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License * it under the terms of the GNU Affero General Public License
* version 3 as published by the Free Software Foundation * version 3 as published by the Free Software Foundation
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "detectormorph.h" #include "detectormorph.h"
using namespace cv; using namespace cv;
using namespace std; using namespace std;
namespace alpr namespace alpr {
{
DetectorMorph::DetectorMorph(Config* config) : Detector(config) {
DetectorMorph::DetectorMorph(Config* config) : Detector(config) { this->loaded = true;
}
this->loaded = true; DetectorMorph::~DetectorMorph() {
} }
vector<PlateRegion> DetectorMorph::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest) {
DetectorMorph::~DetectorMorph() { Mat frame_gray;
} cvtColor(frame, frame_gray, CV_BGR2GRAY);
vector<PlateRegion> DetectorMorph::detect(Mat frame, std::vector<cv::Rect> regionsOfInterest) vector<PlateRegion> detectedRegions;
{ 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));
Mat frame_gray; img_result = frame_gray - img_open;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
vector<PlateRegion> detectedRegions; if (config->debugDetector && config->debugShowImages) {
for (int i = 0; i < regionsOfInterest.size(); i++) imshow("Opening", img_result);
{ }
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; //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) if (config->debugDetector && config->debugShowImages) {
{ imshow("Threshold Detector", img_threshold);
imshow("Opening", img_result); }
}
//threshold image using otsu thresholding Mat circularElement = getStructuringElement(cv::MORPH_ELLIPSE, Size(5, 5));
Mat img_threshold, img_open2; morphologyEx(img_threshold, img_open2, CV_MOP_OPEN, circularElement, cv::Point(-1, -1));
threshold(img_result, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); Mat rectElement = getStructuringElement(cv::MORPH_RECT, Size(20, 4));
morphologyEx(img_open2, img_threshold, CV_MOP_CLOSE, rectElement, cv::Point(-1, -1));
if (config->debugDetector && config->debugShowImages) if (config->debugDetector && config->debugShowImages) {
{ imshow("Close", img_threshold);
imshow("Threshold Detector", img_threshold); waitKey(0);
} }
Mat circularElement = getStructuringElement(cv::MORPH_ELLIPSE, Size(5, 5)); //Find contours of possibles plates
morphologyEx(img_threshold, img_open2, CV_MOP_OPEN, circularElement, cv::Point(-1, -1)); vector< vector< Point> > contours;
Mat rectElement = getStructuringElement(cv::MORPH_RECT, Size(20, 4)); findContours(img_threshold,
morphologyEx(img_open2, img_threshold, CV_MOP_CLOSE, rectElement, cv::Point(-1, -1)); contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // all pixels of each contours
if (config->debugDetector && config->debugShowImages) //Start to iterate to each contour founded
{ vector<vector<Point> >::iterator itc = contours.begin();
imshow("Close", img_threshold); vector<RotatedRect> rects;
waitKey(0);
}
//Find contours of possibles plates //Remove patch that are no inside limits of aspect ratio and area.
vector< vector< Point> > contours; while (itc != contours.end()) {
findContours(img_threshold, //Create bounding rect of object
contours, // a vector of contours RotatedRect mr = minAreaRect(Mat(*itc));
CV_RETR_EXTERNAL, // retrieve the external contours if (!CheckSizes(mr))
CV_CHAIN_APPROX_NONE); // all pixels of each contours itc = contours.erase(itc);
else {
++itc;
if (itc == contours.end()) continue;
rects.push_back(mr);
}
}
//Start to iterate to each contour founded //Now prunning based on checking all candidate plates for a min/max number of blobs
vector<vector<Point> >::iterator itc = contours.begin(); Mat img_crop, img_crop_b;
vector<RotatedRect> rects; vector< vector< Point> > plateBlobs;
for (int i = 0; i < rects.size(); i++) {
RotatedRect PlateRect = rects[i];
Size rect_size = PlateRect.size;
//Remove patch that are no inside limits of aspect ratio and area. //Crop area around candidate plate
while (itc != contours.end()) getRectSubPix(frame_gray, rect_size, PlateRect.center, img_crop);
{ //Thresholding
//Create bounding rect of object threshold(img_crop, img_crop_b, 0, 255, cv::THRESH_BINARY_INV + cv::THRESH_OTSU);
RotatedRect mr = minAreaRect(Mat(*itc));
if (!CheckSizes(mr))
itc = contours.erase(itc);
else
{
++itc;
if (itc == contours.end()) continue;
rects.push_back(mr);
}
}
//Now prunning based on checking all candidate plates for a min/max number of blobs findContours(img_crop_b,
Mat img_crop, img_crop_b; plateBlobs, // a vector of contours
vector< vector< Point> > plateBlobs; CV_RETR_EXTERNAL, // retrieve the external contours
for (int i = 0; i < rects.size(); i++) CV_CHAIN_APPROX_NONE); // all pixels of each contours
{
RotatedRect PlateRect = rects[i];
Size rect_size = PlateRect.size;
//Crop area around candidate plate int numBlobs = plateBlobs.size();
getRectSubPix(frame_gray, rect_size, PlateRect.center, img_crop);
//Thresholding
threshold(img_crop, img_crop_b, 0, 255, cv::THRESH_BINARY_INV + cv::THRESH_OTSU);
findContours(img_crop_b, //If too much or too little might not be a true plate
plateBlobs, // a vector of contours if (numBlobs < 3 || numBlobs > 20) continue;
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // all pixels of each contours
int numBlobs = plateBlobs.size(); PlateRegion PlateReg;
//If too much or too little might not be a true plate PlateReg.rect = PlateRect.boundingRect();
if (numBlobs < 3 || numBlobs > 20) continue;
PlateRegion PlateReg; detectedRegions.push_back(PlateReg);
PlateReg.rect = PlateRect.boundingRect(); }
detectedRegions.push_back(PlateReg); }
} return detectedRegions;
}
} bool DetectorMorph::CheckSizes(RotatedRect& mr) {
return detectedRegions; float error = 1.2;
}
bool DetectorMorph::CheckSizes(RotatedRect& mr) float aspect = config->plateWidthMM / config->plateHeightMM;
{ //Set a min and max area. All other patchs are discarded
int min = 10 * aspect * 10; // minimum area
int max = 100 * aspect * 100; // maximum area
//Get only patchs that match to a respect ratio.
float rmin = 3.0;
float rmax = 7.0;
float error = 1.2; int area = mr.size.height * mr.size.width;
float r = (float) mr.size.width / (float) mr.size.height;
if (r < 1)
r = (float) mr.size.height / (float) mr.size.width;
float aspect = config->plateWidthMM / config->plateHeightMM; if ((area < min || area > max) || (r < rmin || r > rmax) ||
//Set a min and max area. All other patchs are discarded (mr.size.width < 20 || mr.size.width > 300))
int min = 10 * aspect * 10; // minimum area return false;
int max = 100 * aspect * 100; // maximum area else
//Get only patchs that match to a respect ratio. return true;
float rmin = 3.0;
float rmax = 7.0;
int area = mr.size.height * mr.size.width; }
float r = (float)mr.size.width / (float)mr.size.height;
if (r<1)
r = (float)mr.size.height / (float)mr.size.width;
if ((area < min || area > max) || (r < rmin || r > rmax) ||
(mr.size.width < 20 || mr.size.width > 300) )
return false;
else
return true;
}
} }

View File

@@ -1,21 +1,21 @@
/* /*
* Copyright (c) 2014 New Designs Unlimited, LLC * Copyright (c) 2014 New Designs Unlimited, LLC
* Opensource Automated License Plate Recognition [http://www.openalpr.com] * Opensource Automated License Plate Recognition [http://www.openalpr.com]
* *
* This file is part of OpenAlpr. * This file is part of OpenAlpr.
* *
* OpenAlpr is free software: you can redistribute it and/or modify * OpenAlpr is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License * it under the terms of the GNU Affero General Public License
* version 3 as published by the Free Software Foundation * version 3 as published by the Free Software Foundation
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef OPENALPR_DETECTORMORPH_H #ifndef OPENALPR_DETECTORMORPH_H
#define OPENALPR_DETECTORMORPH_H #define OPENALPR_DETECTORMORPH_H
@@ -31,20 +31,19 @@
#include "detector.h" #include "detector.h"
namespace alpr namespace alpr {
{
class DetectorMorph : public Detector { class DetectorMorph : public Detector {
public: public:
DetectorMorph(Config* config); DetectorMorph(Config* config);
virtual ~DetectorMorph(); virtual ~DetectorMorph();
std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest); std::vector<PlateRegion> detect(cv::Mat frame, std::vector<cv::Rect> regionsOfInterest);
private: private:
bool CheckSizes(cv::RotatedRect& mr); bool CheckSizes(cv::RotatedRect& mr);
}; };
} }