【OpenCV】【图像处理】基于轮廓的字符分割
2015-11-03 17:01
621 查看
vector<IplImage*> mycvCharSegment(IplImage* image) { //此函数利用OpenCV中的cvFindContours找出字符的轮廓后进行字符分割,包括汉字的合并处理,但是要求二值化的图像轮廓清晰 vector<IplImage*> characters; //存储分割出来的字符的图像头 IplImage* smoothImg = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1); cvSmooth(image, smoothImg, CV_MEDIAN, 3, 3, 0, 0); /*cvNamedWindow("Smooth", CV_WINDOW_AUTOSIZE); cvShowImage("Smooth", smoothImg);*/ IplImage* contoursImg = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1); cvCopy(smoothImg,contoursImg); CvSeq* contours = NULL; CvMemStorage* storage = cvCreateMemStorage(0); int count = cvFindContours(smoothImg, storage, &contours, sizeof(CvContour), CV_RETR_EXTERNAL); vector<CvRect> rcs; vector<CvRect> comrcs; for ( CvSeq* c = contours; c != NULL; c = c->h_next ) { CvRect rect = cvBoundingRect(c, 0); rcs.push_back(rect); } sort(rcs.begin(), rcs.end(), compareRect); //compareRect是自定义的一个比较函数 vector<CvRect>::iterator itr,itr_next; float aveHeight = mycvGetRectAveHeight(rcs); float aveWidth = mycvGetRectAveWidth(rcs); float maxWidth = mycvGetRectMaxWidth(rcs); float maxHeight = mycvGetRectMaxHeight(rcs); for ( itr = rcs.begin(),itr_next=itr+1; itr != rcs.end(); ) { CvRect rc = *itr; if(itr_next!= rcs.end()) { CvRect rcnext = *(itr_next); if ( (rc.x+rc.width>rcnext.x)||((rc.x<rcnext.x) && (rc.x+rc.width>rcnext.x+rcnext.width)) ) { rc.x = min(rc.x,rcnext.x); rc.y = min(rc.y,rcnext.y); rc.width = max((rc.x+rc.width),(rcnext.x+rcnext.width))-rc.x; rc.height = max((rc.y+rc.height),(rcnext.y+rcnext.height))-rc.y; *itr = rc; itr_next++; } else { comrcs.push_back(rc); itr = itr_next++; } } else { itr++; comrcs.push_back(rc); } } for ( itr = comrcs.begin(); itr != comrcs.end(); itr++ ) { CvRect rc = *itr; cvDrawRect(image, cvPoint(rc.x, rc.y), cvPoint(rc.x + rc.width, rc.y + rc.height), CV_RGB(255, 255, 255)); cvNamedWindow("字符分割", CV_WINDOW_AUTOSIZE); cvShowImage("字符分割", image); IplImage* imgNo = cvCreateImage(cvSize(rc.width , rc.height), IPL_DEPTH_8U, 1); cvSetImageROI(image, rc); cvCopyImage(image, imgNo); cvResetImageROI(image); cvResetImageROI(imgNo); IplImage* dst = cvCreateImage(cvSize(rc.width , rc.height), IPL_DEPTH_8U, 1); cvResize(imgNo, dst); characters.push_back(dst); } return characters; }
相关文章推荐
- 原生JS判断网站服务器是否开启,如果开启就跳转到网站
- Linux下使用Eclipse开发Hadoop应用程序
- Linux下crontab定时执行脚本
- getopt用法常规
- 制作可以 SSH 登录的 Docker 镜像
- java代码调用linux命令,生成.pem公钥私钥证书
- nginx防盗链设置
- hadoop2.6.0下打包mahout调用数据模型程序 运行时报错
- centos7 挂载virtualbox 中无法挂载共享文件
- PostgreSQL在CentOS下的源码安装
- 理解LInux路由
- 针对Apache2handler的几个问题
- 六.Servlet类的基本架构
- 如何找出Linux系统中内置模块的信息
- opencv学习笔记1--主要宏模块学习
- Apache Client使用说明第一章(第一部分)
- centos7安装配置lnmp
- linux chmod 使用方法
- Linux CentOS增加swap分区
- logger4j.properties和log4j.xml的常用配置(干货)