-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageOperations.cpp
115 lines (90 loc) · 2.4 KB
/
ImageOperations.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
#include "ImageOperations.h"
using namespace std;
ImageOperations::ImageOperations()
{
}
ImageOperations::~ImageOperations()
{
}
char** ImageOperations::thresholdByValue(char** imageMatrix, int nRows, int nCols, int maxIntensityLevel, int thresh)
{
if (DEBUG)
{
//cout << "\nnRow: " << nRows << " " << "nCols: " << nCols << endl;
//cout << "thresh: " << thresh << endl;
}
char** threshImg = new char*[nRows];
for (int i = 0; i < nRows; i++)
{
threshImg[i] = new char[nCols];
for (int j = 0; j < nCols; j++)
{
unsigned char temp = imageMatrix[i][j];
if (temp < thresh)
threshImg[i][j] = 0;
else
threshImg[i][j] = temp;
/*if (DEBUG)
{
cout << "Orig: " << imageMatrix[i][j] << " ";
cout << "Temp: " << temp << " ";
cout << "Thresh: " << threshImg[i][j] << endl;
}*/
}
}
return threshImg;
}
int ImageOperations::getOtsuThreshold(char** imageMatrix, int nRows, int nCols, int maxIntensityLevel)
{
int otsuThreshold = 0;
int pixelCount = 0;
double maxBetween = 0.0;
int* frequencies = new int[maxIntensityLevel];
float* histogram = new float[maxIntensityLevel];
double* probability = new double[maxIntensityLevel];
double* mean = new double[maxIntensityLevel];
double* between = new double[maxIntensityLevel];
for (int i = 0; i < maxIntensityLevel; i++)
{
frequencies[i] = 0;
histogram[i] = 0;
}
for (int i = 0; i < nRows; i++)
for (int j = 0; j < nCols; j++)
{
int pVal = imageMatrix[i][j];
frequencies[pVal] = frequencies[pVal] + 1;
pixelCount++;
}
for (int i = 0; i <= maxIntensityLevel; i++)
histogram[i] = (float)frequencies[i] / (float)pixelCount;
for (int i = 0; i < maxIntensityLevel + 1; i++)
{
probability[i] = 0.0;
mean[i] = 0.0;
between[i] = 0.0;
}
probability[0] = histogram[0];
for (int i = 1; i < maxIntensityLevel + 1; i++)
{
probability[i] = probability[i - 1] + histogram[i];
mean[i] = mean[i - 1] + i * histogram[i];
}
for (int i = 0; i < maxIntensityLevel; i++)
{
if (probability[i] != 0.0 && probability[i] != 1.0)
between[i] = pow(mean[maxIntensityLevel] * probability[i] - mean[i], 2) / (probability[i] * (1.0 - probability[i]));
else
between[i] = 0.0;
if (between[i] > maxBetween)
{
maxBetween = between[i];
otsuThreshold = i;
}
}
cout << "\nOTSU THRESHOLD:\t" << otsuThreshold << endl;
return otsuThreshold;
}
void ImageOperations::getConnectedComponents(int method)
{
}