如何使用投影(直方图轮廓)对字符进行分段 [英] How to segment characters using projection(histogram profile)

查看:106
本文介绍了如何使用投影(直方图轮廓)对字符进行分段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做关于OCR的研究项目,我需要使用水平和垂直直方图轮廓(投影轮廓)来分割角色。这是我尝试过的代码,但我无法分割线条一个文件通过在直方图bin值为零的位置裁剪图像。请帮我解决这个问题。谢谢。



Hi, I''m doing research project regarding OCR and I need to segment characters using horizontal and vertical histogram profile(projection profile).This is the code I have tried but I coudn''t able to segment the lines of a document by crop the image at positions where histogram bin value get zero.Please help me with this problem.Thanks.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <opencv\cv.h>
#include <opencv\cxcore.h>
#include <opencv\highgui.h>


int _tmain(int argc, _TCHAR* argv[])
{
	IplImage *img = cvLoadImage("new.jpg");
	CvSize imgSize =cvGetSize(img);

	//Gray scale
	IplImage *gray=cvCreateImage(cvSize(img->width,img->height),8,1);
	cvCvtColor(img,gray,CV_RGB2GRAY);

	//binary
	IplImage *binary=cvCreateImage(cvSize(img->width,img->height),8,1);
	cvThreshold(gray, binary, 5, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

	double pixel;
	int count=0;
	int height=binary->height;
	int *linecount = new int[height];
	int width=binary->width;
	int *wordcount = new int[width];

	int *HorizontalHistogram = new int[height];
	for(int i = 0; i < height; i++)
    {
        HorizontalHistogram[i] = 0;
    }

	//Line segmentation
	printf("Horizontal Bin Values \n");
	for(int j=0;j<(binary->height);j++){
		count=0;
		for(int i=0;i<(binary->width);i++){
			pixel=cvGetReal2D(binary,j,i);
			if( pixel==0 ){
				HorizontalHistogram[j]++;
				count++;	
			}	
		}
		printf("%d \n", count);
	}
	
	int Hhist_w = height; int Hhist_h = 300;
	int Vhist_w = height; int Vhist_h = 300;
	float range[] = {0,255};
	float *ranges[] = {range};
	int Hhist_size = {binary->height};
	int Vhist_size = {binary->width};
	float min_value,max_value = 0;
	IplImage *histImage1 = cvCreateImage(cvSize(height,300),8,1);
	IplImage *histImage2 = cvCreateImage(cvSize(width,300),8,1);
	cvSet(histImage1,cvScalarAll(255),0);
	cvSet(histImage2,cvScalarAll(255),0);
	CvHistogram *hist = cvCreateHist(1,&Hhist_size,CV_HIST_ARRAY,ranges,1);
	int bin_w1 = cvRound((double)histImage1->width/Hhist_size);
	int bin_w2 = cvRound((double)histImage2->width/Vhist_size);

	for(int i = 0; i < height; i++)
    {
		cvLine(histImage1, cvPoint(bin_w1*(i), Hhist_h),
                              cvPoint(bin_w1*(i), Hhist_h - HorizontalHistogram[i]),
             cvScalar(0,0,0), 1, 8, 0);
    }

	cvNamedWindow("Image:");
	cvShowImage("Image:", img);
	cvNamedWindow("Binary:");
	cvShowImage("Binary:", binary);
	cvNamedWindow("HorizontalHistogram:");
	cvShowImage("HorizontalHistogram:", histImage1);

	cvWaitKey(0);

	cvDestroyWindow("Image:");
	cvReleaseImage(&img);
	cvDestroyWindow("Binary:");
	cvReleaseImage(&binary);
	cvDestroyWindow("HorizontalHistogram:");
	cvReleaseImage(&histImage1);

	return 0;
}

推荐答案

问题实际上OP是不确定如何在计算后从主图像中提取线图像线间隙的位置。由于评论部分(见上文)的讨论,案件可以解决。
The problems was actually OP was uncertain on how to extract the line images from the main image after having calculated the position of the line gaps. As a result of the discussion in the comment section (see above) the case could be resolved.


这篇关于如何使用投影(直方图轮廓)对字符进行分段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆