输入原始车牌信息

输出mser识别结果

具体算法步骤
第1步:mser结构体定义
struct MSERParams
{
MSERParams(int _delta = 5, int _min_area = 60, int _max_area = 14400,
double _max_variation = 0.25, double _min_diversity = .2,
int _max_evolution = 200, double _area_threshold = 1.01,
double _min_margin = 0.003, int _edge_blur_size = 5)
{
delta = _delta;
minArea = _min_area;
maxArea = _max_area;
maxVariation = _max_variation;
minDiversity = _min_diversity;
maxEvolution = _max_evolution;
areaThreshold = _area_threshold;
minMargin = _min_margin;
edgeBlurSize = _edge_blur_size;
pass2Only = false;
}
int delta;
int minArea;
int maxArea;
double maxVariation;
double minDiversity;
bool pass2Only;
int maxEvolution;
double areaThreshold;
double minMargin;
int edgeBlurSize;
};
static String Legende(const MSERParams &pAct)
{
ostringstream ss;
ss << "Area[" << pAct.minArea << "," << pAct.maxArea << "] ";
ss << "del. [" << pAct.delta << "] ";
ss << "var. [" << pAct.maxVariation << "] ";
ss << "div. [" << (int)pAct.minDiversity << "] ";
ss << "pas. [" << (int)pAct.pass2Only << "] ";
ss << "RGb->evo. [" << pAct.maxEvolution << "] ";
ss << "are. [" << (int)pAct.areaThreshold << "] ";
ss << "mar. [" << (int)pAct.minMargin << "] ";
ss << "siz. [" << pAct.edgeBlurSize << "]";
return ss.str();
}第2步:输入图像及参数定义
Mat imgOrig, img;
Size blurSize(5, 5);
cv::CommandLineParser parser(argc, argv, "{ help h | | }{ @input | | }");
if (parser.has("help"))
{
return 0;
}
string input = parser.get<string>("@input");
if (!input.empty())
{
imgOrig = imread(input, IMREAD_GRAYSCALE);
blur(imgOrig, img, blurSize);
}
//mser参数定义
vector<String> typeDesc;
vector<MSERParams> pMSER;
//创建mser对象
MSERParams params;
params.delta = 10;
params.minArea = 100;
params.maxArea = 5000;
params.maxVariation = 2;
params.minDiversity = 0;
params.pass2Only = true;
typeDesc.push_back("MSER");
pMSER.push_back(params);
params.pass2Only = false;
typeDesc.push_back("MSER");
pMSER.push_back(params);
params.delta = 100;
typeDesc.push_back("MSER");
pMSER.push_back(params);第3步:mser算法检测车牌
vector<MSERParams>::iterator itMSER = pMSER.begin();
Ptr<Feature2D> b;
String label;
vector<String>::iterator itDesc;
Mat result(img.rows, img.cols, CV_8UC3);
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
{
if (*itDesc == "MSER")
{
if (img.type() == CV_8UC3)
{
b = MSER::create(itMSER->delta, itMSER->minArea, itMSER->maxArea, itMSER->maxVariation, itMSER->minDiversity, itMSER->maxEvolution,
itMSER->areaThreshold, itMSER->minMargin, itMSER->edgeBlurSize);
label = Legende(*itMSER);
++itMSER;
}
else
{
b = MSER::create(itMSER->delta, itMSER->minArea, itMSER->maxArea, itMSER->maxVariation, itMSER->minDiversity);
b.dynamicCast<MSER>()->setPass2Only(itMSER->pass2Only);
label = Legende(*itMSER);
++itMSER;
}
}
if (img.type()==CV_8UC3)
{
img.copyTo(result);
}
else
{
vector<Mat> plan;
plan.push_back(img);
plan.push_back(img);
plan.push_back(img);
merge(plan,result);
}
try
{
vector<KeyPoint> keyImg;
vector<Rect> zone;
vector<vector <Point> > region;
Mat desc;
if (b.dynamicCast<MSER>().get())
{
Ptr<MSER> sbd = b.dynamicCast<MSER>();
//检测Region
sbd->detectRegions(img, region, zone);
int nbPixelInMSER=0;
for (vector<vector <Point> >::iterator itr = region.begin(); itr != region.end(); ++itr)
{ //根据检测区域点生成mser+结果
for (vector <Point>::iterator itp = itr->begin(); itp != itr->end(); ++itp)
{
// mser找到的区域涂蓝色
result.at<Vec3b>(itp->y, itp->x) = Vec3b(128, 0, 0);
nbPixelInMSER++;
}
}
cout << "Number of MSER region: " << region.size() << "; Number of pixels in all MSER region: " << nbPixelInMSER << "\n";
}
const string winName = *itDesc + label;
namedWindow(winName, WINDOW_AUTOSIZE);
imshow(winName, result);
imshow("Original", img);
}
catch (const Exception& e)
{
cout << "Feature: " << *itDesc << "\n";
cout << e.msg << endl;
}
waitKey();
}