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;
}
//
#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\机器视觉
本文版权归作者,未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接