opencv 更新速度一直在加快,最新版本都出到了3.0beta了。MSER一直在用C接口的,最近想试一下3.0的MSER,发现还用不起来,因为封装方法还没搞清楚,也没有太多参考,就用了Qt5.3+opencv2.4.7中的C++接口MSER,当然没有界面,还是控制台程序。总的来说还是比较方便的。
#include <QCoreApplication>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/features2d/features2d.hpp>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "maxRB.h"
#include "FT_XYZ.h"
using namespace cv;
using namespace std;
////////////////////////////////////////////////////////////
//显示颜色
static CvScalar colors[] =
{
{{0,0,255}},
{{0,128,255}},
{{0,255,255}},
{{0,255,0}},
{{255,128,0}},
{{255,255,0}},
{{255,0,0}},
{{255,0,255}},
{{255,255,255}},
{{196,255,255}},
{{255,255,196}}
};
////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
////////////////////////////////////////////////////////////
Mat src,gray;
string path;
ifstream filein("xibanlist.txt");
int imageNumber=0;
while(getline(filein,path))
{
cout<<" image sample load : "<<imageNumber<<" "<<path<<endl;
src = cv::imread(path);
if( src.data == NULL )
{
cout<<" image sample load error: "<<imageNumber<<" "<<path<<endl;
system("pause");
continue;
}
MSER mser( 5, 10, cvRound(0.1*(src.cols)*0.15*(src.rows)),0.25,0.2);
vector<vector<Point>> regions;
//灰度图求MSER
IplImage img =src;
IplImage *maxrb = MAX_RB(&img);
IplImage *ftxyz = FT(&img);
Mat imgmaxrb(maxrb);
Mat imgftxyz(ftxyz);
string temp =path;
int last =temp.find_last_of("\\");
string name = temp.substr(last+1);
string savepath ="D:\\2015-1-19\\xibanyayangben\\";
string savepathMaxRB =savepath;
savepathMaxRB+="MaxRB_";
savepathMaxRB+=name;
string savepathFT =savepath;
savepathFT+="FT_";
savepathFT+=name;
string savepathRoi= savepath;
savepathRoi +="xibian_";
//cv::imwrite(savepathMaxRB,imgmaxrb);
//cv::imwrite(savepathMaxRB,imgmaxrb);
// cvtColor( src, gray, CV_BGR2GRAY );
mser(imgmaxrb, regions, Mat());//提取MSER
cvReleaseImage(&maxrb);
cvReleaseImage(&ftxyz);
vector<vector<Point>>::iterator itc= regions.begin();
while (itc!=regions.end())
{
//限定region
RotatedRect rotater= minAreaRect(*itc);
CvBox2D box = rotater.operator CvBox2D();
Rect rect= boundingRect(*itc);
double WvH=0;
float catSizeH=0;
float catSizeW=0;
catSizeH=box.size.height*0.15;
catSizeW=box.size.width*0.15;
WvH=box.size.height/box.size.width;
if(box.size.width>30&&box.size.height>30
&&box.size.width<200&&box.size.height<200&&WvH>0.3&&WvH<2.5)
++itc;
else
itc= regions.erase(itc);
}
//在灰度图像中用椭圆形绘制组块
QString pth;
for (int i = 0; i < regions.size(); i++)
{
Rect rect= boundingRect(regions[i]);
Mat roi(src,rect);
pth=QString("%1%2%3%4").arg("D:\\2015-1-19\\xibanyayangben\\").arg(name.c_str()).arg(i).arg(".jpg");
string lstname= pth.toStdString();
cout<<lstname<<endl;
cv::imwrite(lstname,roi);
//drawContours(src,regions,i,colors[i%9],-1);
}
// string temp =path;
// int last =temp.find_last_of("\\");
// string name = temp.substr(last+1);
// string savepath ="D:\\ruidianmser\\";
// savepath += name;
// cout<<savepath<<endl;
// cv::imwrite(savepath,src);
regions.clear();
imageNumber++;
}
////////////////////////////////////////////////////////////
return a.exec();
}