opencv 摳圖聯通塊(c介面)

來源:互聯網
上載者:User

標籤:copy   sig   eps   int   roi   namespace   ems   void   pen   

#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);}

  

opencv 摳圖聯通塊(c介面)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.