GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — learn-opencv-cpp/opencvtest1.cpp at master · awidardi/learn-opencv-cpp

Expand file tree

/

opencvtest1.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
440 lines (333 loc) · 24.5 KB
/

opencvtest1.cpp

Copy path

File metadata and controls

440 lines (333 loc) · 24.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// opencvtest1.cpp : Defines the entry point for the console application.
//
#include"stdafx.h"//header for Visual Studio
#include <opencv2\opencv.hpp>
#include <opencv2/core/core.hpp>//header for OpenCV core
#include <opencv2/highgui/highgui.hpp>//header for OpenCV UI
#include <iostream>//header for c++ IO
#include <fstream>
//set opencv and c++ namespaces
using namespace cv;
using namespace std;
String sample_pic_dir = "C:/Users/GP62MVR-7RFX/source/repos/opencvtest1/opencvtest1/sample_pic/";
//String sample_pic_dir = "C:/Users/ARDI/source/repos/opencvtest1/opencvtest1/sample_pic/";
String output_pic_dir = "C:/Users/GP62MVR-7RFX/source/repos/opencvtest1/opencvtest1/tmp/";
//String output_pic_dir = "C:/Users/ARDI/source/repos/opencvtest1/opencvtest1/tmp/";
int cnt[3005];
// vector of pair [column](starting row , length)
vector<pair<int, int> > startWhitePixel[3005];
vector<pair<int, int> > startWhitePixelAfter[3005];
// vector of pair [length](starting row , column)
vector<pair<int, int> > startCoordinateWhitePixel[3005];
long long dpSigmaXSquare[3005];
long long dpSigmaX[3005];
long long dpSigmaY[3005];
long long dpSigmaXY[3005];
int jedaBarisFrek[3005];
Mat changeVerticalWhiteIntoBlack(Mat image, int he, int wi, int len) {
for (int i = 1;i <= len;i++) {
image.at<uchar>(he + i - 1, wi) = 0;
}
return image;
}
Mat changeVerticalBlackIntoWhite(Mat image, int he, int wi, int len) {
for (int i = 1;i <= len;i++) {
image.at<uchar>(he + i - 1, wi) = 255;
}
return image;
}
void printMaxIMaxJMaxLen(int maxi, int maxj, int maxlen) {
cout << "i: " << maxi << endl;
cout << "j: " << maxj << endl;
cout << "len: " << maxlen << endl;
}
Mat inputGrayscaleImage(string input, int &width, int &height) {
Mat im_gray;
Mat im_rgb = imread(sample_pic_dir + "buku/" + input + ".jpg");
cvtColor(im_rgb, im_gray, CV_RGB2GRAY);
height = im_gray.rows;
width = im_gray.cols;
return im_gray;
}
Mat clahe(Mat input, int limit) {
Ptr<CLAHE> clahe = createCLAHE();
clahe->setClipLimit(limit);
Mat output;
clahe->apply(input, output);
return output;
}
Mat otsu(Mat imageAfterClahe, int width, int height) {
String dir;
Mat img_bw = imageAfterClahe;
Mat resultAfterPartialOtsu = imageAfterClahe;
Size smallsize(width / 16, height / 16);
for (int i = 0; i + smallsize.height < height; i += smallsize.height) {
for (int j = 0; j + smallsize.width < width; j += smallsize.width) {
Mat outin = img_bw(Rect(j, i, width / 16, height / 16));
Mat out;
threshold(outin, out, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
out.copyTo(resultAfterPartialOtsu(Rect(j, i, out.cols, out.rows)));
if (i + 2 * smallsize.height >= height || j + 2 * smallsize.width >= width) {
int tmpposi = i + smallsize.height;
int tmpposj = j + smallsize.width;
outin = img_bw(Rect(tmpposj, tmpposi, width - tmpposj, height - tmpposi));
threshold(outin, out, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
out.copyTo(resultAfterPartialOtsu(Rect(tmpposj, tmpposi, out.cols, out.rows)));
}
}
}
return resultAfterPartialOtsu;
//dir = output_pic_dir + "buku/"+ input + "_partial_otsu16x16_edited9-13px.jpg";
//dir = output_pic_dir + "buku/"+ input + "_partial+clahe2_otsu16x16_edited10-19px.jpg";
//imwrite(dir, resultAfterPartialOtsu);
}
void prefsum() {
dpSigmaXSquare[1] = 1;
dpSigmaX[1] = 1;
dpSigmaY[1] = cnt[1];
dpSigmaXY[1] = cnt[1];
for (int i = 2;i < 3005;i++) {
dpSigmaXSquare[i] = dpSigmaXSquare[i - 1] + (i*i);
dpSigmaX[i] = dpSigmaX[i - 1] + i;
dpSigmaY[i] = dpSigmaY[i - 1] + cnt[i];
dpSigmaXY[i] = dpSigmaXY[i - 1] + (i * cnt[i]);
}
}
double solveGradient(int start, int window) {
long long sigmaXSquare = dpSigmaXSquare[start + window - 1] - dpSigmaXSquare[start - 1];
long long sigmaY = dpSigmaY[start + window - 1] - dpSigmaY[start - 1];
long long sigmaX = dpSigmaX[start + window - 1] - dpSigmaX[start - 1];
long long sigmaXY = dpSigmaXY[start + window - 1] - dpSigmaXY[start - 1];
return (double)(window * sigmaXY - sigmaX * sigmaY) / (double)(window * sigmaXSquare - sigmaX * sigmaX);
}
void printData(int threshold, int window, string filename, string mode) {
ofstream data(filename);
if (data.is_open()) {
if (mode == "GRADIENT") {
for (int i = 1;i < 3005;i++) {
if (cnt[i] > threshold && cnt[i + window - 1] > threshold) {
data << i << ": " << cnt[i] << endl;
data << "gradient: " << (double)solveGradient(i, window) << endl;
}
}
}
else if (mode == "DATA ONLY") {
for (int i = 1;i < 3005;i++) {
if (cnt[i] > threshold && cnt[i + window - 1] > threshold) {
data << i << ": " << cnt[i] << " ";
}
}
}
data.close();
}
else {
cout << "Unable to find file\n";
}
}
long long findStartMaxGradient(int threshold, int window) {
int indexMax = 1;
long long valueMax = -1;
for (int i = 1;i < 3005;i++) {
if (cnt[i] > threshold && cnt[i + window - 1] > threshold) {
long long tmp = solveGradient(i, window);
if (tmp > valueMax) {
indexMax = i;
valueMax = tmp;
}
}
}
return indexMax;
}
int findNormalPoint(int start , int window) {
long long sum = 0;
long long divisor = 0;
for (int i = start;i <= start + window - 1;i++) {
sum += (i * cnt[i]);
divisor += cnt[i];
}
return (int)(sum / divisor);
}
Mat findAndReverseWhiteVerticalConsecutivePixels(Mat image, int rangeStart, int rangeFinish) {
for (int i = rangeStart;i <= rangeFinish;i++) {
for (int j = 0;j < startCoordinateWhitePixel[i].size();j++) {
image = changeVerticalWhiteIntoBlack(image, startCoordinateWhitePixel[i][j].first, startCoordinateWhitePixel[i][j].second , i);
}
}
return image;
}
void countJedaBarisAtPartition(int &width, int &height, Mat &resultAfterPartialOtsu, int rangeStart, int rangeFinish) {
Size smallsize(width / 16, height / 16);
for (int j = 0;j < width;j+=smallsize.width) {
}
}
Mat fixVectorImage(int width, int height, int normalPoint ,int startThreshold, int windowThreshold) {
for (int j = 0;j < width;j++) {
int add = 0;
int sz = startWhitePixel[j].size();
for (int i = 0;i < sz;i++) {
int fi = startWhitePixel[j][i].first;
int se = startWhitePixel[j][i].second;
int len;
if (i!=0) {
if ((se >= startThreshold && se <= startThreshold + windowThreshold - 1)) {
add = add + (normalPoint - se);
len = min(height - (fi + add) , normalPoint);
}
else {
len = min(height - (fi + add), se);
}
startWhitePixelAfter[j].push_back(make_pair(fi + add, len));
}
}
}
Mat newImage(height, width, CV_8UC1, Scalar(0, 0, 0));
for (int j = 0;j < width;j++) {
for (int i = 0;i < startWhitePixelAfter[j].size();i++) {
newImage = changeVerticalBlackIntoWhite(newImage, startWhitePixelAfter[j][i].first, j, startWhitePixelAfter[j][i].second);
}
}
return newImage;
}
void solverWhitePixel(Mat resultAfterPartialOtsu, int width, int height) {
int tmplen;
int tmpi = -1;
int tmpj = -1;
for (int j = 0;j < width;j++) {
for (int i = 0;i < height;i++) {
if (i == 0) {
if (tmpi != -1 && tmpj != -1) {
cnt[tmplen]++;
startWhitePixel[tmpj].push_back( make_pair(tmpi , tmplen) );
startCoordinateWhitePixel[tmplen].push_back(make_pair(tmpi, tmpj));
jedaBarisFrek[tmpj]++;
tmplen = 0;
}
if (resultAfterPartialOtsu.at<uchar>(i, j) != 0) {
tmpj = j;
tmpi = i;
tmplen = 1;
}
}
else {
// before black current white
if (resultAfterPartialOtsu.at<uchar>(i, j) != 0 && resultAfterPartialOtsu.at<uchar>(i - 1, j) == 0) {
tmpj = j;
tmpi = i;
tmplen = 1;
}
// before white current white
else if (resultAfterPartialOtsu.at<uchar>(i, j) != 0 && resultAfterPartialOtsu.at<uchar>(i - 1, j) != 0) {
tmplen++;
}
// before white current black
else if (resultAfterPartialOtsu.at<uchar>(i, j) == 0 && resultAfterPartialOtsu.at<uchar>(i - 1, j) != 0) {
cnt[tmplen]++;
startWhitePixel[tmpj].push_back( make_pair(tmpi , tmplen) );
startCoordinateWhitePixel[tmplen].push_back(make_pair(tmpi, tmpj));
jedaBarisFrek[tmpj]++;
tmpi = -1;
tmpj = -1;
tmplen = 0;
}
}
}
}
}
void imadjust(const Mat1b& src, Mat1b& dst, int tol = 1, Vec2i in = Vec2i(0, 255), Vec2i out = Vec2i(0, 255))
{
// src : input CV_8UC1 image
// dst : output CV_8UC1 imge
// tol : tolerance, from 0 to 100.
// in : src image bounds
// out : dst image buonds
dst = src.clone();
tol = max(0, min(100, tol));
if (tol > 0)
{
// Compute in and out limits
// Histogram
vector<int> hist(256, 0);
for (int r = 0; r < src.rows; ++r) {
for (int c = 0; c < src.cols; ++c) {
hist[src(r, c)]++;
}
}
// Cumulative histogram
vector<int> cum = hist;
for (int i = 1; i < hist.size(); ++i) {
cum[i] = cum[i - 1] + hist[i];
}
// Compute bounds
int total = src.rows * src.cols;
int low_bound = total * tol /100;
int upp_bound = total * (100 - tol) / 100;
in[0] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), low_bound));
in[1] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), upp_bound));
}
// Stretching
float scale = float(out[1] - out[0]) / float(in[1] - in[0]);
for (int r = 0; r < dst.rows; ++r)
{
for (int c = 0; c < dst.cols; ++c)
{
int vs = max(src(r, c) - in[0], 0);
int vd = min(int(vs * scale + 0.5f) + out[0], out[1]);
dst(r, c) = saturate_cast<uchar>(vd);
}
}
}
int main()
{
double startAll = clock(), start, stop;
int height, width;
String filename="g1";
String dir;
// TODO : INPUT IMAGE AND CONVERT TO GRAYSCALE
start = clock();
Mat imageInput = inputGrayscaleImage(filename, width, height);
cout << "INPUT Time Execution : " << (double) (clock() - start) / CLOCKS_PER_SEC << endl;
// TODO : CONTRAST ENHANCEMENT WITH CLAHE
start = clock();
Mat imageAfterClahe = clahe(imageInput, 2);
cout << "CONTRAST ENHANCEMENT Time Execution : " << (double)(clock() - start) / CLOCKS_PER_SEC << endl;
// TODO : BINARIZATION WITH OTSU METHOD
start = clock();
Mat imageAfterOtsu = otsu(imageAfterClahe, width, height);
dir = output_pic_dir + "buku/otsu/" + filename + ".jpg";
imwrite(dir , imageAfterOtsu);
cout << "OTSU Time Execution : " << (double)(clock() - start) / CLOCKS_PER_SEC << endl;
// TODO : PROCESSING WHITE PIXEL SOLVER
start = clock();
solverWhitePixel(imageAfterOtsu, width, height);
cout << "MENCARI JEDA BARIS Time Execution : " << (double)(clock() - start) / CLOCKS_PER_SEC << endl;
// TODO : PRE - COMPUTE PREFSUM DP
start = clock();
prefsum();
cout << "PENYIMPANAN DATA 1 Time Execution : " << (double)(clock() - start) / CLOCKS_PER_SEC << endl;
// TODO : PRINT GRADIENT OR SAVE THE DATA AT FILE
// dir = output_pic_dir + "buku/data/(Window 10) Data + Gradient " + filename + ".txt";
// printData(1, 5, dir , "GRADIENT");
// TODO : SOLVE REGRESSION AND FIND MAX GRAD
// THRESHOLD = 1 , WINDOW = 10
start = clock();
int pointStartMaxGrad = findStartMaxGradient(1, 10);
cout << "MENCARI MAX GRADIENT DENGAN REGRESI Time Execution : " << (double)(clock() - start) / CLOCKS_PER_SEC << endl;
// TODO : FIND NORMAL POINT
start = clock();
int normalPoint = findNormalPoint(pointStartMaxGrad, 10);
cout << "MENCARI JEDA BARIS DEFAULT Time Execution : " << (double)(clock() - start) / CLOCKS_PER_SEC << endl;
// TODO : REVERSE WHITE PIXEL INTO BLACK AND OUTPUT
// Mat imageValidation = findAndReverseWhiteVerticalConsecutivePixels(imageAfterOtsu, pointStartMaxGrad, pointStartMaxGrad + 9);
// dir = output_pic_dir + "buku/hasil/" + filename + ".jpg";
// imwrite(dir, imageValidation);
// TODO : FIX IMAGE
start = clock();
Mat imageResult = fixVectorImage(width, height, normalPoint, pointStartMaxGrad, 10);
dir = output_pic_dir + "buku/result/" + filename + ".jpg";
imwrite(dir, imageResult);
cout << "PENYISIPAN DAN PEMBETULAN GAMBAR Time Execution : " << (double)(clock() - start) / CLOCKS_PER_SEC << endl;
cout << "TOTAL Time Execution : " << (double)(clock() - startAll) / CLOCKS_PER_SEC << endl;
getchar();
waitKey(0);
destroyAllWindows();
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.