-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUtils.cpp
199 lines (165 loc) · 5.7 KB
/
Utils.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
191
192
193
194
195
196
/*----------------------------------------------------------------------------*/
/* Copyright (C) 2015 Alexandre Campo */
/* */
/* This file is part of USE Tracker. */
/* */
/* USE Tracker is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* USE Tracker is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with USE Tracker. If not, see <http://www.gnu.org/licenses/>. */
/*----------------------------------------------------------------------------*/
#include "Utils.h"
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
void EqualizeToRGB (Mat& src, Mat& dst)
{
unsigned char min = 255, max = 0;
for (int i = 0; i < src.rows * src.cols; i++)
{
unsigned char v = src.data[i];
if (v < min) min = v;
if (v > max) max = v;
}
for (int y = 0; y < src.rows; y++)
{
unsigned char* row = src.ptr<unsigned char>(y);
unsigned char* row2 = dst.ptr<unsigned char>(y);
for (int x = 0; x < src.cols; x++)
{
float v = row[x];
v = (v - min) / (max - min) * 255.0;
row2[x*3] = (unsigned char)(v);
row2[x*3+1] = (unsigned char)(v);
row2[x*3+2] = (unsigned char)(v);
}
}
}
void EqualizeBW (Mat& src, Mat& dst)
{
unsigned char min = 255, max = 0;
for (int i = 0; i < src.rows * src.cols; i++)
{
unsigned char v = src.data[i];
if (v < min) min = v;
if (v > max) max = v;
}
for (int y = 0; y < src.rows; y++)
{
unsigned char* row = src.ptr<unsigned char>(y);
for (int x = 0; x < src.cols; x++)
{
float v = row[x];
v = (v - min) / (max - min) * 255.0;
row[x] = (unsigned char)(v);
}
}
}
void SaveMatToPNG (Mat& mat, const char* filename)
{
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
imwrite(filename, mat, compression_params);
}
// The code below comes from https://github.com/bsdnoobz/zhang-suen-thinning/blob/master/thinning.cpp
// And http://opencv-code.com/quick-tips/implementation-of-thinning-algorithm-in-opencv/
// thanks to the author
/**
* Perform one thinning iteration.
* Normally you wouldn't call this function directly from your code.
*
* Parameters:
* im Binary image with range = [0,1]
* iter 0=even, 1=odd
*/
void ThinningIteration(cv::Mat& img, int iter)
{
cv::Mat marker = cv::Mat::zeros(img.size(), CV_8UC1);
int nRows = img.rows;
int nCols = img.cols;
if (img.isContinuous()) {
nCols *= nRows;
nRows = 1;
}
int x, y;
uchar *pAbove;
uchar *pCurr;
uchar *pBelow;
uchar *nw, *no, *ne; // north (pAbove)
uchar *we, *me, *ea;
uchar *sw, *so, *se; // south (pBelow)
uchar *pDst;
// initialize row pointers
pAbove = NULL;
pCurr = img.ptr<uchar>(0);
pBelow = img.ptr<uchar>(1);
for (y = 1; y < img.rows-1; ++y) {
// shift the rows up by one
pAbove = pCurr;
pCurr = pBelow;
pBelow = img.ptr<uchar>(y+1);
pDst = marker.ptr<uchar>(y);
// initialize col pointers
no = &(pAbove[0]);
ne = &(pAbove[1]);
me = &(pCurr[0]);
ea = &(pCurr[1]);
so = &(pBelow[0]);
se = &(pBelow[1]);
for (x = 1; x < img.cols-1; ++x) {
// shift col pointers left by one (scan left to right)
nw = no;
no = ne;
ne = &(pAbove[x+1]);
we = me;
me = ea;
ea = &(pCurr[x+1]);
sw = so;
so = se;
se = &(pBelow[x+1]);
int A = (*no == 0 && *ne == 1) + (*ne == 0 && *ea == 1) +
(*ea == 0 && *se == 1) + (*se == 0 && *so == 1) +
(*so == 0 && *sw == 1) + (*sw == 0 && *we == 1) +
(*we == 0 && *nw == 1) + (*nw == 0 && *no == 1);
int B = *no + *ne + *ea + *se + *so + *sw + *we + *nw;
int m1 = iter == 0 ? (*no * *ea * *so) : (*no * *ea * *we);
int m2 = iter == 0 ? (*ea * *so * *we) : (*no * *so * *we);
if (A == 1 && (B >= 2 && B <= 6) && m1 == 0 && m2 == 0)
pDst[x] = 1;
}
}
img &= ~marker;
}
/**
* Function for thinning the given binary image
*
* Parameters:
* src The source image, binary with range = [0,1]
* dst The destination image
*/
void Thinning(const cv::Mat& src, cv::Mat& dst)
{
CV_Assert(src.channels() == 1);
CV_Assert(src.depth() != sizeof(uchar));
CV_Assert(src.rows > 3 && src.cols > 3);
dst = src.clone();
cv::Mat prev = cv::Mat::zeros(dst.size(), CV_8UC1);
cv::Mat diff;
do {
ThinningIteration(dst, 0);
ThinningIteration(dst, 1);
cv::absdiff(dst, prev, diff);
dst.copyTo(prev);
}
while (cv::countNonZero(diff) > 0);
}