-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuseful.h
127 lines (105 loc) · 3.53 KB
/
useful.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
///****************************************************************************************************************************///
///********************************* Developed By Fazle Rabby Sourav && Ahnaf Rownak ******************************************///
///****************************************************************************************************************************///
#ifndef USEFUL_H
#define USEFUL_H
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace std;
using namespace cv;
void print_roi_image(const Mat &roi) {
int r = roi.rows, c = roi.cols;
const uchar *data;
for (int j = 0; j < r; j++) {
data = roi.ptr<uchar>(j);
for (int i = 0; i < c; i++)
printf(data[i] ? "@" : " ");
printf("\n");
}
printf("\n");
}
void getMatraPos(const vector<int> &hHist, int &maxHHIdx,
int &matraStart, int &matraEnd) {
maxHHIdx = 0;
int r_2 = hHist.size() * 0.4, maxHH = hHist[0];
for (int i = 1; i <= r_2; i++) {
int temp = hHist[i];
if (temp > maxHH) maxHH = temp, maxHHIdx = i;
}
double matraTH = maxHH * 0.75;
matraStart = matraEnd = maxHHIdx;
for (int i = maxHHIdx - 1; i >= 0; i--)
if (hHist[i] >= matraTH) matraStart = i;
else break;
for (int i = maxHHIdx + 1; i <= r_2; i++)
if (hHist[i] >= matraTH) matraEnd = i;
else break;
matraEnd++;
}
void GetPos(const vector<int> &mHist,
vector<int> &start_pos, vector<int> &stop_pos) {
int i, hist, len_mHist = mHist.size();
bool isStart = false, isStop = true;
for (i = 0; i < len_mHist; i++) {
hist = mHist.at(i);
if (hist) {
isStop = false;
if (isStart) continue;
else {
start_pos.push_back(i);
isStart = true;
}
} else {
isStart = false;
if (isStop) continue;
else {
stop_pos.push_back(i);
isStop = true;
}
}
}
}
void getActualWordHeightData(const Mat word, int &wordStartTopPos, int &presentWordHeight) {
int wordHeight = word.rows, wordEndBottomPos;
for (int k = 0; k < wordHeight; k++) {
if (countNonZero(word.row(k))) {
wordStartTopPos = k;
break;
}
}
for (int k = wordHeight - 1; k >= 0; k--) {
if (countNonZero(word.row(k))) {
wordEndBottomPos = k;
break;
}
}
presentWordHeight = wordEndBottomPos - wordStartTopPos;
}
Mat HorVertHistogram(const Mat &src_img, vector<int> &mhist,
bool isHor = true, bool isHistOn = false) {
int sz = (isHor) ? src_img.rows : src_img.cols;
int i, j, v, width, height, maximum = 0;
Mat data, histo;
mhist.clear();
for (j = 0; j < sz; ++j) {
data = (isHor) ? src_img.row(j) : src_img.col(j);
v = countNonZero(data);
mhist.push_back(v);
if (v > maximum) maximum = v;
}
if (isHistOn) {
if (isHor) {
width = maximum, height = sz;
histo = Mat::zeros(Size(width, height), CV_8U);
for (i = 0; i < height; ++i)
line(histo, Point(0, i), Point(mhist.at(i), i), 255);
} else {
width = sz, height = maximum;
histo = Mat::zeros(Size(width, height), CV_8U);
for (i = 0; i < width; ++i)
line(histo, Point(i, maximum), Point(i, maximum - mhist.at(i)), 255);
}
}
return histo;
}
#endif // USEFUL_H