-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptive_threshold.cpp
More file actions
89 lines (72 loc) · 2.03 KB
/
adaptive_threshold.cpp
File metadata and controls
89 lines (72 loc) · 2.03 KB
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
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <numeric>
#include <chrono>
#include <vector>
#include <iostream>
using namespace cv;
void adaptiveThresholdingSegmentation(Mat & image, const int kernelSize)
{
Mat tempImage;
if (image.channels() != 1)
{
cvtColor(image, tempImage, COLOR_BGR2GRAY);
tempImage.copyTo(image);
}
else
{
image.copyTo(tempImage);
}
int totalKernelElements = kernelSize * kernelSize;
std::vector<long> kernel(totalKernelElements, 1.0 / totalKernelElements);
std::vector<long> values;
int halfSize{ kernelSize / 2 };
for (int i{ halfSize }; i < tempImage.rows - halfSize; i++)
{
for (int j{ halfSize }; j < tempImage.cols - halfSize; j++)
{
values.clear();
for (int x = { -halfSize }; x <= halfSize; x++)
{
for (int y = { -halfSize }; y <= halfSize; y++)
{
unsigned char * pixelValuePtr = tempImage.ptr(i + x) + (j + y);
values.push_back(*pixelValuePtr);
}
}
long averageValue = inner_product(begin(values), end(values), begin(kernel), 0.0);
unsigned char * pixelValuePtr = image.ptr(i) + j;
*pixelValuePtr = *pixelValuePtr > averageValue ? 0 : 255;
}
}
}
int main(int argc, char** argv )
{
using std::chrono::duration_cast;
using std::chrono::nanoseconds;
typedef std::chrono::high_resolution_clock clock;
if ( argc != 3 )
{
printf("usage: DisplayImage.out <Image_Path> <threshold>\n");
return -1;
}
Mat image;
auto start = clock::now();
image = imread( argv[1], 1 );
auto end = clock::now();
std::cout << duration_cast<nanoseconds>(end-start).count() << "ns\n";
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
// imshow("Display Image", image);
start = clock::now();
adaptiveThresholdingSegmentation(image, atoi(argv[2]));
end = clock::now();
std::cout << duration_cast<nanoseconds>(end-start).count() << "ns\n";
imshow("Display Image", image);
waitKey(0);
return 0;
}