1 | initial version |
int main(int argc, char* argv[])
{
Mat dst;
Mat src = imread("e:/sandbox/ocrtext.jpg",0);
threshold(src,src,0,255,THRESH_OTSU);
bitwise_not(src,src);
// cut ROI
dst = src (Rect(10,10,src.cols - 20,src.rows -20));
//projection
vector<int> vecUpX;
vector<int> vecDownX;
projection2(dst,vecUpX,vecDownX,DIRECTION_X,3);
vector<int> vecUpY;
vector<int> vecDownY;
projection2(dst,vecUpY,vecDownY,DIRECTION_Y,3);
waitKey();
}
//get projection of Mat
void projection2(Mat src,vector<int>& vup,vector<int>& vdown,int direction,int gap){
Mat tmp = src.clone();
vector<int> vdate;
if (DIRECTION_X == direction){
for (int i=0;i<tmp.cols;i++){
Mat data = tmp.col(i);
int itmp = countNonZero(data);
vdate.push_back(itmp);
}
}else{
for (int i=0;i<tmp.rows;i++){
Mat data = tmp.row(i);
int itmp = countNonZero(data);
vdate.push_back(itmp);
}
}
if (vdate.size()<=gap)
return;
for (int i=0;i<vdate.size()-gap;i++){
if (vdate[i]>0 && vdate[i+gap]>0){
for (int j=i;j<i+gap;j++){
vdate[j] = 1;
}
i = i+gap-1;
}
}
//up and down edge
for (int i=1;i<vdate.size();i++){
if (vdate[i-1] == 0 && vdate[i]>0)
vup.push_back(i);
if (vdate[i-1]>0 && vdate[i] == 0)
vdown.push_back(i);
}
}
You can get the direction by simplely "projection method".in the code above,the size of vecUpX (and vecDownX) is "13" and the size of vecUpY(and vecDownY) is 1,SO the picture is in DIRECTION Y.