#include "stdio.h" #include "iostream" #include "opencv/cv.h" #include "opencv2/opencv.hpp" #include "basicOCR.h" #include "time.h" using namespace std; using namespace cv; void ImageRect(IplImage *srcImg, IplImage *dstImg); int main() { /*basicOCR bor; IplImage *image = cvLoadImage("585.pbm",1); IplImage *gray = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1); cvCvtColor(image,gray,CV_RGB2GRAY); bor.classify(gray,1); //printf("depth = %d\nwidth = %d\nheight = %d\nnChannels = %d\n",image->depth,image->width,image->height,image->nChannels); image = cvLoadImage("608.pbm",1); cvCvtColor(image,gray,CV_RGB2GRAY); bor.classify(gray,1); image = cvLoadImage("test002.jpg",1); IplImage *gray1 = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1); cvCvtColor(image,gray1,CV_RGB2GRAY); bor.classify(gray1,1); */ basicOCR bor; int type; while(scanf("%d",&type)!=EOF) { if(type == -1) break; char path[20]; sprintf(path,"./test00%d.jpg",type); clock_t start,finish; start = clock(); IplImage *srcImage = cvLoadImage(path,1); if(srcImage==NULL){ printf("%s : path is error...",path); continue; } IplImage *gray2 = cvCreateImage(cvSize(128,128),IPL_DEPTH_8U,1); ImageRect(srcImage,gray2); //cvCvtColor(srcImage,gray2,CV_RGB2GRAY); bor.classify(gray2,1); finish = clock(); double duration = (double)(finish-start)/CLOCKS_PER_SEC; printf("检测时间: %f seconds\n",duration); } return 0; } void ImageRect(IplImage *srcImg, IplImage *dstImg) { IplImage *tempImg = cvCreateImage(cvGetSize(srcImg),IPL_DEPTH_8U,1); IplImage *resultImg = cvCreateImage(cvGetSize(srcImg),IPL_DEPTH_8U,1); IplImage *backgroundImg = cvCreateImage(cvSize(128,128),8,1); cvZero(backgroundImg); for(int i = 0; i<backgroundImg->height;i++) { unsigned char *data = (unsigned char*)backgroundImg->imageData+i*backgroundImg->widthStep; for(int j=0; j<backgroundImg->width;j++) { data[j] = 0; } } //cvShowImage("back",backgroundImg); cvCvtColor(srcImg,tempImg,CV_RGB2GRAY); cvThreshold(tempImg,tempImg,220,255,CV_THRESH_BINARY_INV); cvCopy(tempImg,resultImg); CvMemStorage *storage = cvCreateMemStorage(); //cvShowImage("h0",tempImg); CvSeq *contours = NULL; cvFindContours(tempImg,storage,&contours,sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_NONE,cvPoint(0,0)); int area; CvRect rect; while(contours) { rect = cvBoundingRect(contours,0); area = rect.width * rect.height; if(area>50) { printf("x"); //cvRectangle(resultImg,cvPoint(rect.x,rect.y),cvPoint(rect.x+rect.width,rect.y+rect.height),CV_RGB(200,200,200),1,8,0); int mHeight = 60; int mWidth = 60; int mLeft = 40; int mTop = 40; if(rect.height>rect.width) { mWidth = (int)(60.0*rect.width/rect.height); }else{ mHeight = (int)(60.0*rect.height/rect.width); } IplImage *foregroundImg = cvCreateImage(cvSize(mWidth,mHeight),8,1); cvSetImageROI(resultImg,rect); cvSetImageROI(backgroundImg,cvRect(mLeft,mTop,mWidth,mHeight)); cvResize(resultImg,foregroundImg,CV_INTER_NN); cvCopy(foregroundImg,backgroundImg); cvResetImageROI(backgroundImg); cvResetImageROI(resultImg); cvReleaseImage(&foregroundImg); } contours = contours->h_next; } //cvShowImage("h2",resultImg); cvThreshold(backgroundImg,backgroundImg,220,255,CV_THRESH_BINARY_INV); cvSmooth(backgroundImg,backgroundImg,CV_BLUR,3,3,0,0); cvDilate(backgroundImg,backgroundImg,NULL,1); cvErode(backgroundImg,backgroundImg,NULL,2); cvThreshold(backgroundImg,backgroundImg,220,255,CV_THRESH_BINARY); cvCopy(backgroundImg,dstImg,NULL); cvSaveImage("./epsq3.jpg",backgroundImg); //cvShowImage("background",backgroundImg); //cvWaitKey(0); cvReleaseMemStorage(&storage); cvReleaseImage(&tempImg); cvReleaseImage(&resultImg); cvReleaseImage(&backgroundImg); }
时间: 2024-09-30 23:27:45