这一节,主要讲通过灰度化,自适应二值化,ROI找轮廓,轮廓筛选,ROI轮廓分割,自动割取样本;
如图:在车牌分割中应用比较广,大致思路可以看我的程序,具体细节可以针对不同的检测物体的大小以及背景更改。
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
using namespace std;
using namespace cv;
#define showSteps false
#define saveModel false
//定义文件夹下视频的数量
#define NumVideo 16
string getFilename(string s) {
char sep = '/';
char sepExt='.';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length( ));
if (i != string::npos) {
string fn= (s.substr(i+1, s.length( ) - i));
size_t j = fn.rfind(sepExt, fn.length( ));
if (i != string::npos) {
return fn.substr(0,j);
}else{
return fn;
}
}else{
return "";
}
}
void adaptiveThreshold(Mat input, Mat bin, int width, int height);
bool verifySizes(RotatedRect mr){
float error=0.6;
//Spain car plate size: 20*40 aspect 360
float aspect=0.5;
//Set a min and max area. All other patchs are discarded
int min= 20*aspect*20; // minimum area 200
int max= 50*aspect*50; // maximum area 1250
//Get only patchs that match to a respect ratio.
float rmin= aspect-aspect*error; //0.5-0.5*0.6 = 0.2
float rmax= aspect+aspect*error; //0.8
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.height>=47 || mr.size.height<=26)||(mr.size.width<=9 || mr.size.width>=31)){
// if( (mr.size.height>=45 || mr.size.height<=24)||(mr.size.width<=6 || mr.size.width>=27) ){
return false;
}else{
return true;
}
}
int main ( int argc, char** argv )
{
//初始化一个视频文件捕捉器
char *video_name = NULL;
CvCapture* capture;
video_name = (char*)malloc(500*sizeof(char)); //动态分配内存
char *filename = NULL;
filename=(char *)malloc(50*sizeof(char)); //动态分配内存
for(int jj=1; jj<=NumVideo; jj++)
{
int i=0; //视频读取标志标志
printf("\n第%d段视频\n",jj);
sprintf_s(video_name, 500, "E:\\video\\%d.avi",jj);//保存的图片名
// sprintf_s(video_name, 500, "I:\\ship\\%d.avi",jj);//保存的图片名
capture = cvCaptureFromAVI(video_name);
// CvCapture* capture = cvCaptureFromAVI("..\\video\\4.avi");
if( capture == NULL ){
printf("视频文件打开失败!!");
continue;
}
int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WI