#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 ("Detection time:%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->imag Edata+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);}
OPENCV cutout block (C interface)