求助!!opencv实现OCR第一步字符区域定位drawContours问题

aa6121223 2017-09-18 04:58:08
使用findContours和drawContiurs一起求解一张图内含有字符区域的轮廓,其中findContours给出的Contours.size给的结果是23,但实际自己从二值化的图中数的是18,然后我用contours[i].size 把这23个轮廓大小都cout了下,刚好有5个0
分割线------------------------------------------------------------------------------------------------------------------------------------------------------------------接下来就是很常规的用for循环 i<contours.size作为跳出的条件去drawcontours,但是报错了,显示的原因是0<contoursIdx<<int last>。 个人觉得是那5个0的问题,就试着把drawContours(TesImg, Contours, i, Scalar(0, 0, 255), 2, 8, hierarchy, 0, Point());里面的contoursIdx项里的 i 分别改成了数字0,1,2,3,4.....结果画出的图依次分别是自己数的的每个对应的字符区,但是改到18的时候就又报错了,和前面的错误一样,所以个人理解是drawContours的上限就是报错里的<(int)last>就是18,但是找不到这个参数的信息,因为findcontours给出的结果是23,虽然包含0但就是23个,用dawcontrous去直接画的话又报错,就不懂为什么,而且这5个0在这23个轮廓里是随机分布的,就不知道该怎么处理才能把图给画出来。。 -----------------------------本人的最终目的是把每个轮廓扣出来单独保存,所以需要每个轮廓的Mat信息,我自己还试着单独加了个if(contours[i].size!=0)就进行drawcontours,但是当i等于18的时候就又报错,换句话说,这里面的i值不能大于18.。。。。如果每次自己重新整理这些轮廓信息(把0剔除)的话感觉太麻烦了, 所以求教该怎么处理最方便呢! 或者有人懂这个last值是什么吗,很好奇他从哪里算出的除去0意外刚好就又18呢?
...全文
366 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-09-19
  • 打赏
  • 举报
回复
仅供参考:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
using namespace std;
using namespace cv;
Mat img,smallImg,gray,bw;
vector<Vec4i> hierarchy;
vector<vector<Point> > contours;
int threshval=128;
Rect r;
Rect maxrect,brect;
int idx,n;
const static Scalar colors[15]={
    CV_RGB(  0,  0,128),
    CV_RGB(  0,128,  0),
    CV_RGB(  0,128,128),
    CV_RGB(128,  0,  0),
    CV_RGB(128,  0,128),
    CV_RGB(128,128,  0),
    CV_RGB(128,128,128),
    CV_RGB(160,160,160),
    CV_RGB(  0,  0,255),
    CV_RGB(  0,255,  0),
    CV_RGB(  0,255,255),
    CV_RGB(255,  0,  0),
    CV_RGB(255,  0,255),
    CV_RGB(255,255,  0),
    CV_RGB(255,255,255),
};
Scalar color;
void gamma_correct(Mat& img, Mat& dst, double gamma) {
	Mat	temp;
	CvMat tmp;

	img.convertTo(temp,	CV_32FC1, 1.0/255.0, 0.0);
	tmp=temp;
	cvPow(&tmp,	&tmp, gamma);
	temp.convertTo(dst , CV_8UC1 , 255.0	, 0.0);
}
int main() {
    cvNamedWindow("display",1);
    img=imread("image.jpg",1);
    r.x=img.cols/10;
    r.y=img.rows/3;
    r.width=img.cols*8/10;
    r.height=img.rows*2/3;
    smallImg=img(r);
    cvtColor(smallImg,gray,CV_BGR2GRAY);
//  medianBlur(gray,gray,5);
    equalizeHist(gray,gray);
    gamma_correct(gray,gray,4.0);
    imshow("display",gray);
    waitKey(0);

	bw=(gray>threshval);
    imshow("display",bw);
    waitKey(0);

	Mat	Structure0=getStructuringElement(MORPH_RECT,Size(3,3));
    erode(bw,bw,Structure0,Point(-1,-1));
	Mat	Structure1=getStructuringElement(MORPH_RECT,Size(6,6));
    dilate(bw,bw,Structure1, Point(-1,-1));
    imshow("display",bw);
    waitKey(0);

    findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
    if (!contours.empty()&&!hierarchy.empty()) {
        idx=0;
        n=0;
        vector<Point> approx;
        for (;idx>=0;idx=hierarchy[idx][0]) {
            color=colors[idx%15];
//          drawContours(smallImg,contours,idx,color,1,8,hierarchy);
            approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
            const Point* p = &approx[0];
            int m=(int)approx.size();
            polylines(smallImg, &p, &m, 1, true, color);
            circle(smallImg,Point(p[0].x,p[0].y),3,color);
            circle(smallImg,Point(p[1].x,p[1].y),2,color);
			for	(int i=2;i<m;i++) circle(smallImg,Point(p[i].x,p[i].y),1,color);
            n++;
            if (1==n) {
                maxrect=boundingRect(Mat(contours[idx]));
            } else {
                brect=boundingRect(Mat(contours[idx]));
                CvRect mr(maxrect),br(brect);
                maxrect=cvMaxRect(&mr,&br);
            }
        }
        circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
    }
    imshow("display",smallImg);
    waitKey(0);
    cvDestroyWindow("display");
    return 0;
}

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧