opencv实战-图片变文本(2)

在上一节中,我们已经提取出了二值图,这一步就是分割图片获取单词了。

   比如左边这张图片,我们可以分成6个子图,先分成两行,每行又有3部分。

算法的思路很简单,实现起来也不是那么麻烦。


#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <vector>

#define threshold_value 210
#define max_BINARY_value 255
#define SPACE 4

using namespace cv;
using namespace std;

int main( int argc, char** argv) {

	Mat src;
	src = imread(argv[1]);
	imshow( "src", src );
		
	Mat src_gray = Mat(src.size(),src.depth());
    cvtColor(src, src_gray, CV_BGR2GRAY);
    imshow( "gray", src_gray);
    
    Mat src_binary = Mat(src_gray.size(),src_gray.depth());
	threshold(src_gray, src_binary, threshold_value, max_BINARY_value, 0);
    imshow( "binary", src_binary);
    
    //seperate the word
    vector<Mat>Columns;
    bool begin = false;
    int row1 = 0, row2 = 0;
    for( int y = 0; y < src_binary.rows; y++ ) 
    {
    	for( int x = 0; x < src_binary.cols; x++ ) 
    	{
        	if(src_binary.at<unsigned char>(y,x) == 0) 
        	{
        		if(!begin) {
        			begin = true;
        			row1 = y;
        		}
        		break;
        	}
        	else if( x == (src_binary.cols-1) && begin)
        	{
        		begin = false;
        		row2 = y;
        		Mat extract = src_binary(Range(row1, row2+1), Range::all());
        		Columns.push_back(extract);
        	}
        }
        if(y == (src_binary.rows-1) && row2 != y)
        {
        	begin = false;
        	row2 = y;
        	Mat extract = src_binary(Range(row1, row2+1), Range::all());
        	Columns.push_back(extract);
        }
	}

	vector< vector<Mat> >wordss;
	int columnSize = Columns.size();
	for(int i = 0;i < columnSize;i++)
	{
		int column1 = 0, column2 = 0;
		begin = false;
		int space = 0;
		bool black = false;
		vector<Mat>words;
		for( int x = 0; x < Columns[i].cols; x++ ) 
    	{
    		for( int y = 0; y < Columns[i].rows; y++ ) 
    		{
    			if(Columns[i].at<unsigned char>(y,x) == 0) 
        		{
        			if(!begin) {
        				begin = true;
        				column1 = x;
        			}
        			black = true;
        			break;
        		}
   				if(y == (Columns[i].rows-1))
    				black = false;
    		}
    		if(!black)space++;else space = 0;
    		if(space == SPACE || (  (x == (Columns[i].cols-1)) && (column2 != x )) )
    		{
    			column2 = x;
    			begin = false;
    			space = 0;
    			cout << "\n once!@    " << i << "  " << x;
    			Mat extract = Columns[i](Range::all(),Range(column1,column2+1));
    			words.push_back(extract);
    		}
 
    	}
    	wordss.push_back(words);
	}
	cout << "\n" << wordss.size();
	imshow( "result", wordss[0][1]);

	cvWaitKey(0);	
}

第8行的 SPACE用来定义单词之间的最小空格,虽然是硬编码,不过还是比较准确的。


下一步就是怎么辨识单词了,我现在还没有好主意,如果大家有什么好的想法可以告诉我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值