|
发表于 2013-11-24 01:46:58
|
显示全部楼层
- // test1122.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "cv.h"
- #include "cxcore.h"
- #include "highgui.h"
- #include "iostream"
- #include <vector>
- #pragma comment(lib,"highgui210d.lib")
- #pragma comment(lib,"cxcore210d.lib")
- #pragma comment(lib,"cv210d.lib")
- using namespace std;
- CvRect CutTopButtom(IplImage *src);
- vector<CvRect>leftright;
- //vector<CvRect>::iterator iter;
- int lstart=0,lend=0,lrun=0;
- int lastStart=0,lastEnd=0;
- IplImage *dst[10];
- CvRect rectRoi;
- void FindLeftRight(IplImage *src);
- void RememberLeftRight(IplImage *src,int lstart,int lend);
- void ShowAndCut(IplImage *src[]);
- IplImage * raw=cvLoadImage("678.bmp",0);
- IplImage * img;
- int _tmain(int argc, _TCHAR* argv[])
- {
- cvSmooth(raw,raw);
- cvThreshold(raw,raw,70,255,CV_THRESH_BINARY);
- IplConvKernel *element=0; //结构元素
- cvMorphologyEx(raw,raw,NULL,element,CV_MOP_OPEN); //开运算
- rectRoi=CutTopButtom(raw);
- img=cvCreateImage(cvGetSize(raw),IPL_DEPTH_8U,1);
- cvCopy(raw,img);
- FindLeftRight(raw);
-
-
- return 0;
- }
- CvRect CutTopButtom(IplImage *src)
- {
-
- int* h=new int[src->height];
- memset(h,0,src->height*4);
- int hstart,hend,x,y,zhongdian;
- CvScalar s;
- for(y=0;y<src->height;y++)
- {
- for(x=0;x<src->width;x++)
- {
- s=cvGet2D(src,y,x);
- if(s.val[0]==0)
- h[y]++;
- }
- }
- for (int j=0;j!=src->height;j++)
- {
- if (h[j]>15)
- {
- zhongdian=j;
- hstart=zhongdian;
- break;
- }
- else
- continue;
- }
- while(zhongdian!=src->height)
- {
- if (h[zhongdian]<10)
- {
- hend=zhongdian;
- break;
- }
- else
- hend=zhongdian;
- zhongdian++;
- }
- CvRect rect=cvRect(0,hstart-10,src->width,hend-hstart+20);
- cvSetImageROI(src,rect);
-
-
- return rect;
- }
- void FindLeftRight(IplImage *src)
- {
- int* v=new int[rectRoi.width];
- memset(v,0,rectRoi.width*4);
- int x,y;
- CvScalar s;
- for(x=0;x<rectRoi.width;x++)
- {
- for(y=0;y<rectRoi.height;y++)
- {
- s=cvGet2D(src,y,x);
- if(s.val[0]==0)
- v[x]++;
- else
- continue;
- }
- }
-
- if (lend<=rectRoi.width)
- {
- for (int i=0;i<rectRoi.width;i++)
- {
- if(v[i]>10)
- {
- lrun=i;
- lstart=lrun;
- break;
- }
- else
- continue;
- }
- if (lstart==lrun)
- {
- for (lrun;lrun<rectRoi.width;lrun++)
- {
- if (v[lrun]<5)
- {
- lend=lrun;
- break;
- }
- else
- continue;
-
- }
- }
- RememberLeftRight(src,lstart,lend);
- }
-
- else
- ShowAndCut(dst);
-
-
-
- }
- void RememberLeftRight(IplImage *src,int lstart,int lend)
- {
-
- if (lastStart==0&&lastEnd==0)
- {
- lastStart=lastStart+lstart;
- lastEnd=lastEnd+lend;
- }
-
- else
- {
- lastStart=lastEnd+lstart;
- lastEnd=lastEnd+lend;
- }
- CvRect Rect=cvRect(lastStart-10,0,lastEnd-lastStart+20,rectRoi.height);
- //CvRect Rect=cvRect(lastStart-5,0,lend-lstart+15,rectRoi.height);
- leftright.push_back(Rect);
- rectRoi=cvRect(lend,0,(rectRoi.width-lend),rectRoi.height);
- cvSetImageROI(src,rectRoi);
- FindLeftRight(src);
-
- }
- void ShowAndCut(IplImage *src[])
- {
-
- for (vector<CvRect>::iterator iter=leftright.begin();iter!=leftright.end();iter++,i++)
- {
- src[i]=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
- cvCopy(img,src[i]);
- cvSetImageROI(src[i],*iter);
- cvNamedWindow("src");
- cvShowImage("src",src[i]);
- cvWaitKey(3000);
-
- }
- }
复制代码 |
|