-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhistogram.cpp
More file actions
124 lines (94 loc) · 3.4 KB
/
histogram.cpp
File metadata and controls
124 lines (94 loc) · 3.4 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
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
#include "histogram.h"
#include "QImageWriter"
histogram::histogram()
{
}
void histogram::calculateHistogram(QImage image){
originalPixels = image.bits();
int imageSize = image.width() * image.height();
for (int pixel = 0; pixel < imageSize; pixel++) {
int value =(int)originalPixels[pixel];
histogramArray[value]++;
}
}
void histogram::calculateAccumulatedFrequency(){
accumulatedFrequency[0] = histogramArray[0];
for (int frequency = 1; frequency < 256; frequency++) {
accumulatedFrequency[frequency] = histogramArray[frequency] + accumulatedFrequency[frequency-1];
}
}
void histogram::equalizeHistogram(QImage image){
qEqualizedImage = QImage(image.width(),image.height(),QImage::Format_Grayscale8);
uchar* equalizedPixels = qEqualizedImage.bits();
calculateHistogram(image);
calculateAccumulatedFrequency();
float scale = 255.0f/accumulatedFrequency[255];
for (int value = 0; value < 256; ++value) {
newValues[value] = (scale * accumulatedFrequency[value]);
}
int imageSize = image.width() * image.height();
for (int pixel = 0; pixel < imageSize; pixel++) {
int value = (int)newValues[originalPixels[pixel]];
equalizedPixels[pixel]=value;
equalizedHistogram[value]++;
}
}
int histogram::calculateThreshold(ThresMethod thres, QImage image){
int threshold = 0;
if(thres == ISODATA){
calculateHistogram(image);
calculateAccumulatedFrequency();
int oldT = 0, newT = 128;
while(oldT != newT){
oldT = newT;
float meanF = 0.0f;
float meanB = 0.0f;
for (int f = 0; f < newT; f++) {
meanF += f * histogramArray[f];
}
meanF = meanF/accumulatedFrequency[newT-1];
for (int b = newT; b < 256; b++) {
meanB += b * histogramArray[b];
}
meanB = meanB / (accumulatedFrequency[255] - accumulatedFrequency[newT-1]);
newT = (meanF + meanB) / 2;
}
return newT;
}
else if(thres==OTSU){
calculateHistogram(image);
calculateAccumulatedFrequency();
int imageSize = image.width() * image.height();
int sum = 0;
int q1 = 0; // class 1
int q2 = 0; // class 2
float meanF = 0.0;
float max = 0; // maximun -- threshold
for (int i = 0; i < 256; i++){
sum += i * ((int)histogramArray[i]);
}
for (int i = 0 ; i <256 ; i++) {
q1 += histogramArray[i];
q2 = imageSize - q1;
meanF += (float) (i * ((int)histogramArray[i]));
float m1 = meanF / q1;
float m2 = (sum - meanF) / q2;
float temp = (float) q1 * (float) q2 * (m1 - m2) * (m1 - m2);
// Update the threshold if necessary
if (temp > max) {
max = temp;
threshold = i;
}
}
return threshold;
}
}
void histogram::thresholding(int threshold,QImage image){
qThresholdedImage = QImage(image.width(), image.height(), image.format());
uchar* originalPixels = image.bits();
uchar* thresholdedPixels = qThresholdedImage.bits();
int imageSize = image.width() * image.height();
for (int pixel = 0; pixel < imageSize; pixel++) {
thresholdedPixels[pixel]=originalPixels[pixel]<threshold ? 0:255;
}
}