在上一节中,我们已经提取出了二值图,这一步就是分割图片获取单词了。
比如左边这张图片,我们可以分成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);
}
下一步就是怎么辨识单词了,我现在还没有好主意,如果大家有什么好的想法可以告诉我。