zoukankan      html  css  js  c++  java
  • kmeans2 做ocr分割

     
    在做ocr的分割部分,然后看到了 kmeans ,将二值化的后白色像素坐标作为特征输入,以此做分类分割。 投影分割神马的就不用了 。这个opencv有支持省事。
     
     
    // kmean.cpp : 定义控制台应用程序的入口点。
    //
    #include "stdafx.h"
    #include <opencv/cv.h>
    #include <opencv/cvaux.h>
    #include <opencv/highgui.h>
    #include <opencv/cxcore.h>
    #include <vector>
    //#pragma comment(lib,"opencv_core231d.lib")
    //#pragma comment(lib,"opencv_features2d231d.lib")
    //#pragma comment(lib,"opencv_flann231d.lib")
    //#pragma comment(lib,"opencv_gpu231d.lib")
    //#pragma comment(lib,"opencv_highgui231d.lib")
    //#pragma comment(lib,"opencv_imgproc231d.lib")
    //#pragma comment(lib,"opencv_ml231d.lib")
    //#pragma comment(lib,"opencv_video231d.lib")
    //#pragma comment(lib,"opencv_objdetect231d.lib")
    //#pragma comment(lib,"opencv_legacy231d.lib")
    #pragma comment(lib,"opencv_core231.lib")
    #pragma comment(lib,"opencv_features2d231.lib")
    #pragma comment(lib,"opencv_flann231.lib")
    #pragma comment(lib,"opencv_gpu231.lib")
    #pragma comment(lib,"opencv_highgui231.lib")
    #pragma comment(lib,"opencv_imgproc231.lib")
    #pragma comment(lib,"opencv_ml231.lib")
    #pragma comment(lib,"opencv_video231.lib")
    #pragma comment(lib,"opencv_objdetect231.lib")
    #pragma comment(lib,"opencv_legacy231.lib")
    int _tmain(int argc, _TCHAR* argv[])
    {
        IplImage * im_src = cvLoadImage("C:\\Documents and Settings\\Administrator\\桌面\\halcon ORC识别铁路沿线标识牌\\白天\\0 6 1 6 1 .jpg",\
            CV_LOAD_IMAGE_GRAYSCALE);
        /*IplImage * im_src = cvLoadImage("C:\\Documents and Settings\\Administrator\\桌面\\halcon ORC识别铁路沿线标识牌\\白天\\0 0 8 .jpg",\
            CV_LOAD_IMAGE_GRAYSCALE);*/

        IplImage * im_show = cvCreateImage(cvGetSize(im_src),8,3);
        cvZero(im_show);
        cvThreshold(im_src,im_src,100,255,CV_THRESH_BINARY);
        
        int nwidth = im_src->width;
        int nheight = im_src->height;
        int nwidthstep = im_src->widthStep;
        
        unsigned char *ppiexl = NULL;
        int ntotal = 0;
        for (int i=0;i<nheight;++i)
        {
            ppiexl =(unsigned char *) im_src->imageData + i * nwidthstep;
            for (int j=0;j<nwidth;++j)
            {
                if (ppiexl[j] ==255)
                {
                    ntotal++;
                }
                
            }
        }
        CvMat * sample = cvCreateMat(ntotal,1,CV_32FC2); 
        CvMat * cluster = cvCreateMat(ntotal,1,CV_32SC1);
        
        int ntag = 0;
        for (int i=0;i<nheight;++i)
        {
            ppiexl =(unsigned char *) im_src->imageData + i * nwidthstep;
            for (int j=0;j<nwidth;++j)
            {
                if (ppiexl[j] ==255)
                {
                    sample->data.fl[ntag*2] = i; //行
                    sample->data.fl[ntag*2+1]=j; //列
                    ntag++;
                }
            }
        }
        cvKMeans2(sample,3,cluster,cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 ),5);
        CvScalar color[4] = {cvScalar(0,255,0,0),
                                cvScalar(0,0,255,0),
                                cvScalar(255,0,0,0),
                                cvScalar(0,255,255,0)};
        for (int i = 0; i < ntotal ;++i)
        {
            int nrow = sample->data.fl[i*2];
            int ncol = sample->data.fl[i*2+1];
            
            cvSet2D(im_show,nrow,ncol,color[cluster->data.i[i]]);
        }
        cvShowImage("s",im_show);
        cvShowImage("ss",im_src);
        cvWaitKey(-1);
        return 0;
    }
     





    作者: 小马_xiao

    出处:http://www.cnblogs.com/xiaomaLV2/>

    关于作者:专注halcon\opencv\机器视觉

    本文版权归作者,未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接

  • 相关阅读:
    CentOS配置启动ssh与开机自启
    CentOS中怎样安装、配置、启动Nginx
    CentOS中配置Mysql表名忽略大小写以及提示:Caused by: org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock的解决
    CentOS中部署jar包时提示:org.quartz.SchedulerException: Couldn't get host name
    CentOS6中怎样将jdk1.7升级到1.8
    CentOS6在使用yum install 时提示镜像源路径不存在:PYCURL ERROR 22
    信息系统项目管理师-项目立项管理考点笔记
    chrome89不再支持/deep/的解决方案
    手写async await
    proxy和reflect
  • 原文地址:https://www.cnblogs.com/xiaomaLV2/p/2721499.html
Copyright © 2011-2022 走看看