opencv 抠图联通块(c接口)

#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

opencv 抠图联通块(c接口)的相关文章

HDU 2952 Counting Sheep (DFS找联通块)

题目链接:请戳这里.   题目大意及思路:读懂题意就好了,就是DFS找联通块. 没什么好说的,见代码吧. #include<cstdio> #include<cstring> #include<algorithm> #define N 100+5 using namespace std; int n,m; char g[N][N]; int dir[4][2]={1,0,0,1,-1,0,0,-1}; void dfs(int x,int y) { for(int i=

Codeforces 731C. Socks 联通块

C. Socks time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input output: standard output Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have

C. Learning Languages 求联通块的个数

C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue&

Educational Codeforces Round 5 - C. The Labyrinth (dfs联通块操作)

题目链接:http://codeforces.com/contest/616/problem/C 题意就是 给你一个n行m列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个),每个’*‘的结果取模10.要是为’*‘输出结果,否则输出’.‘. 这个题目就是让你求连续的'.'联通块元素个数,求完一个联通块就把这个联通块标个记号(我设了ok[][]二维数组 表示这个位置的元素的联通块的标号,相连的则为同一个标号),之后这个联通块的每个元素的ans都为f(f为联通块元素个数),然

HDU 4738 Caocao&#39;s Bridges ——(找桥,求联通块)

题意:给你一个无向图,给你一个炸弹去炸掉一条边,使得整个图不再联通,你需要派人去安置炸弹,且派去的人至少要比这条边上的人多.问至少要派去多少个,如果没法完成,就输出-1. 分析:如果这个图是已经是多个联通块了,那么一个人都不用去,如果不是,那么只要找出这个无向图上的桥并且哨兵数量最少的那座把它炸了就行(输出这条边上的哨兵数量即可).直接tarjan就可以写. 注意点:1.可能有重边,所以用手写邻接表的方式存图:2.如果一座桥上没有哨兵,那么你也得至少派去一个人去安置炸弹(因为炸弹不会自己飞过去啊

【UVA10765】Doves and bombs (BCC求割点后联通块数量)

题目: 题意: 给了一个联通无向图,现在问去掉某个点,会让图变成几个联通块? 输出的按分出的从多到小,若相等,输出标号从小到大.输出M个. 分析: BCC求割点后联通块数量,Tarjan算法. 联通块的数目在找到一个low[y]>=dfn[x]时累加,最后加一即可. 代码如下: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #inclu

Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量

D. Directed Roads ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1to n. There are n directed roads in the Udayland. i-th of them goes from town i to some other town 

分别利用并查集,DFS和BFS方法求联通块的数量

联通块是指给定n个点,输入a,b(1<=a,b<=n),然后将a,b连接,凡是连接在一起的所有数就是一个联通块: 题意:第一行输入n,m,分别表示有n个数,有输入m对连接点,以下将要输入m行(输入数据到文件截止): 输出:第一行要求输出联通块的个数,并在第二行分别输出每个联通块中点的数量,每个数之间以一个空格隔开. 样例 15 31 42 53 5输出:2 2 3样列2 9 81 22 33 43 74 54 67 87 9输出: 19 如果不明白的话可以画图试试,最多花半个小时,要是早这样不

洛谷OJ 1141 01迷宫 暴力(求点所在的联通块大小)

https://www.luogu.org/problem/show?pid=1141 题意:n*n地图,0可以走到相邻的某个1上,1可以走到相邻的某个0上,m次询问,问(x,y)能走到多少个格子? n<=1e3,m<=1e5.若点a能到b,则b也能到a.显然a,b能到达的点相同,同一个联通块内的ans是相同的,即求点所在的连通块大小 #include <bits/stdc++.h> using namespace std; const int N=2e3+20; int dx[4