-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindlinkpixel.cpp
190 lines (176 loc) · 5.75 KB
/
findlinkpixel.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "findlinkpixel.h"
vector<vector<point>> getPoint(const Mat& img, int inRow){
int rows = img.rows;
int columns = img.cols;
vector<point> Q;
vector<vector<point>> Result;
bool open = false;
point pointAdd;
for (int i = 0; i < columns; ++i){
if (img.at<uchar>(inRow, i) > 200 && (i != columns - 1)){
pointAdd.x = inRow;
pointAdd.y = i;
Q.push_back(pointAdd);
open = true;
}
else if (open == true && img.at<uchar>(inRow, i) == 0){
Result.push_back(Q);
Q.clear();
open = false;
}
else if (i == (columns - 1) && img.at<uchar>(inRow, i) > 200) {
pointAdd.x = inRow;
pointAdd.y = i;
Q.push_back(pointAdd);
Result.push_back(Q);
Q.clear();
open = false;
}
else
open = false;
}
return Result;
}
vector<point> findCheckPoints(vector<point> row, int colsLength){
int length = (int)row.size();
vector<point> rowCheck;
if (length == 0)
return rowCheck;
point head = row[0];
point last = row[length - 1];
point pointCheck;
if (head.x > 0) {
pointCheck.x = head.x - 1;
pointCheck.y = head.y - 1;
rowCheck.push_back(pointCheck);
}
vector<point>::iterator item = row.begin(), ite = row.end();
for (; item != ite; ++item){
pointCheck.x = (*item).x - 1;
pointCheck.y = (*item).y;
rowCheck.push_back(pointCheck);
}
if (last.x < colsLength - 1) {
pointCheck.x = last.x - 1;
pointCheck.y = last.y + 1;
rowCheck.push_back(pointCheck);
}
return rowCheck;
}
bool checkRow(const vector<point>& rowCheck, const vector<point>& rowCluster){
for (int i = 0; i < rowCheck.size(); ++i){
for (int j = 0; j < rowCluster.size(); ++j){
if (rowCheck[i].x == rowCluster[j].x && rowCheck[i].y == rowCluster[j].y)
return true;
}
}
return false;
}
vector<point> getRow(const vector<point>& rows, int index){
vector<point> result;
for (int i = 0; i < rows.size(); ++i){
if (rows[i].x == index){
result.push_back(rows[i]);
}
}
return result;
}
vector<point> mergeValue(const vector<point>& mat1, const vector<point>& mat2, int index){
vector<point> result;
vector<point> rows1;
vector<point> rows2;
vector<point>::iterator ite;
for (int k = 0; k < index; ++k){
rows1 = getRow(mat1, k);
rows2 = getRow(mat2, k);
ite = result.end();
result.insert(ite, rows1.begin(), rows1.end());
ite = result.end();
result.insert(ite, rows2.begin(), rows2.end());
}
return result;
}
void removeElement(vector<vector<point>>& matQ, vector<int>& removeArray){
//std::sort(removeArray.begin(), removeArray.end());
for (int index = 0; index < removeArray.size(); ++index){
matQ.erase(matQ.begin() + removeArray[removeArray.size()-index-1]) ;
//matQ.erase(matQ.begin());
}
}
void changeQueue(vector<vector<point>>& matQ, vector<point> checkR, vector<int>& checkRowQueue){
for (int index = 0; index < checkRowQueue.size(); ++index){
if (index==0)
continue;
matQ[checkRowQueue[0]].insert(matQ[checkRowQueue[0]].end(), matQ[checkRowQueue[index]].begin(), matQ[checkRowQueue[index]].end());
}
matQ[checkRowQueue[0]].insert(matQ[checkRowQueue[0]].end(), checkR.begin(), checkR.end());
checkRowQueue.erase(checkRowQueue.begin());
removeElement(matQ, checkRowQueue);
//checkR.clear();
}
void printRow(const vector<point>& row){
for (int i = 0; i < row.size(); ++i){
cout << row[i].y<<" ";
}
}
vector<vector<point>> findLinkPixel(const Mat& img){
vector<vector<point>> Q;
vector<point> rowQ;
vector<int> checkRowQueue;
vector<vector<point>> points;
vector<point> checkPoints;
vector<vector<point>>::iterator it;
for (int rowIndex = 0; rowIndex < img.rows; ++rowIndex){
points = getPoint(img, rowIndex);
if (points.size() == 0){
continue;
}
else if (Q.size() == 0) {
Q = points;
continue;
}
it = points.begin();
for (; it != points.end(); ++it){
checkPoints = findCheckPoints((*it), img.cols);
checkRowQueue.clear();
for (int indexQ = 0; indexQ < Q.size(); ++indexQ){
rowQ = getRow(Q[indexQ], rowIndex - 1);
if (rowQ.size() == 0)
continue;
else if (checkRow(rowQ, checkPoints)){
checkRowQueue.push_back(indexQ);
}
}
if (checkRowQueue.size() == 0){
Q.push_back((*it));
}
else if (checkRowQueue.size() == 1){
Q[checkRowQueue[0]].insert(Q[checkRowQueue[0]].end(), (*it).begin(), (*it).end());
}
else
changeQueue(Q, (*it), checkRowQueue);
}
}
return Q;
}
long countMaxClus(const vector<vector<point>>& img){
int max = 0;
int maxIndex = 0;
for (int index = 0; index < img.size(); ++index){
if (img[index].size() > max){
max = (int)img[index].size();
maxIndex = index;
}
}
return maxIndex;
}
void resultFindClus(vector<vector<point>>& Result, vector<point>& maxClus, int maxIndex, Mat& frame, Mat& matResut){
threshold(frame, frame, 200, 255, THRESH_BINARY);
matResut.setTo(cv::Scalar(0));
Result = findLinkPixel(frame);
maxIndex = countMaxClus(Result);
maxClus = Result[maxIndex];
for (int i = 0; i < maxClus.size(); ++i){
matResut.at<uchar>(maxClus[i].x, maxClus[i].y) = 255;
}
}