-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.cpp
executable file
·192 lines (179 loc) · 5.13 KB
/
histogram.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
#include "histogram.h"
histogram createHist(int size){
histogram h;
h.aray=new int[size];
for(int i=0;i <size;i++){
h.aray[i]=0;
}
return h;
}
void fillHistogram(histogram *h,Mat img){
h->jumlahint=0;
h->jumlahpx=0;
h->maks=(int)img.at<uchar>(0,0);
h->mins=(int)img.at<uchar>(0,0);
int intens;
for(int y = 0; y < img.rows; y++){
for(int x = 0; x < img.cols; x++){
//Inisialisasi intensitas
intens = (int)img.at<uchar>(y,x);
//parameter rata-rata
h->jumlahint += intens;
h->jumlahpx++;
//Mengganti maks dan min
if(intens > h->maks){
h->maks = intens;
}
if(intens < h->mins){
h->mins = intens;
}
//Menambah nilai pada histogram
h->aray[intens]++;
}
}
//menghitung rata-rata
h->avg =(double) h->jumlahint/h->jumlahpx;
}
int getModus(int data[]){
int maks=data[0];
for(int i=0;i <256;i++){
if (data[i] > maks) maks=data[i];
}
return maks;
}
int getModusIndeks(int data[]){
int maks=0;
for(int i=0;i <256;i++){
if (data[i] >data[maks]) maks=i;
}
return maks;
}
double countStandarDeviasi(int data[],int lowerThreshold,int upperThreshold){
int mean=countMean(data,lowerThreshold,upperThreshold);
// std::cout << "mean = "<<mean << std::endl;
double sum=0;
int n=0;
for(int i= lowerThreshold;i<upperThreshold;i++){
for(int j=0;j<data[i];j++){
sum=sum+(pow((i - mean),2));
n++;
}
// std::cout << "pow = " <<(pow((data[i]-mean),2))<< std::endl;
}
// std::cout << "sum = "<<sum << std::endl;
// std::cout << "end-start = "<<end-start << std::endl;
// std::cout << "varaaa = "<<sum/(end - start) << std::endl;
// std::cout << "n = "<<n << std::endl;
if (n!=0)
return (double) sqrt(sum/n);
else
return 0;
}
double countMean(int data[],int start,int end){//end = index terakhir+1
int sum=0;
int count=0;
for(int i= start;i<end;i++){
count=count+i*data[i];
sum=sum+data[i];
}
if (sum!=0)
return (double) count/sum;
else
return 0;
}
double countVariance(int data[],int start,int end){
int mean=countMean(data,start,end);
// std::cout << "mean = "<<mean << std::endl;
double sum=0;
int n=0;
for(int i= start;i<end;i++){
for(int j=0;j<data[i];j++){
sum=sum+(pow((i - mean),2));
n++;
}
// std::cout << "pow = " <<(pow((data[i]-mean),2))<< std::endl;
}
// std::cout << "sum = "<<sum << std::endl;
// std::cout << "end-start = "<<end-start << std::endl;
// std::cout << "varaaa = "<<sum/(end - start) << std::endl;
// std::cout << "n = "<<n << std::endl;
if (n!=0)
return (double) sum/n;
else
return 0;
}
double countSegment(int data[],int start,int end){
double sum=0;
for(int i= start;i<end;i++){
sum+=data[i];
}
return sum;
}
void drawHistogram(histogram h,string name){
int hist_w = 512;
int hist_h = 100;
double bin_w = cvRound((double) hist_w/256);
float bin_h = (float) hist_h/getModus(h.aray);
//cout<<hist_h<<" "<<modus<<" "<<bin_h;
Mat histImage(hist_h, hist_w, CV_8UC1, Scalar(255, 255, 255));
for (int i=0;i<256;i++){
//cout <<cvRound((double)bin_h*histogram[i])<<endl;
line(histImage, //img
Point(bin_w*(i), hist_h), //point a
Point(bin_w*(i), hist_h - cvRound((double) h.aray[i]*bin_h)),//point b
Scalar(0,0,0),//color black
bin_w,//thickness
0,//linetype
0);//Number of fractional bits in the point coordinates.
std::cout << "tes = "<< h.aray[i] << '\n';
}
showImage(histImage,name);
}
void statistikHistogram(histogram h){
cout<<"\nSTATISTIK GAMBAR"<<endl;
cout<<"\nIntensitas Maksimal\t: "<<h.maks<<endl;
cout<<"Intensitas Minimal\t: "<<h.mins<<endl;
cout<<"Total Intensitas\t: "<<h.jumlahint<<endl;
cout<<"Jumlah Pixel\t\t: "<<h.jumlahpx<<endl;
cout<<"Intenstitas Rata - rata\t: "<<h.avg<<endl;
cout<<"Standar Deviasi\t\t: "<<countStandarDeviasi(h.aray,0,256)<<endl;
}
// Unknown Number versi Billy
int countNumberB(int data[],int n){
int ST=0;
int SDKiri= cvRound(countStandarDeviasi(data,0,n));
int SDKanan=cvRound(countStandarDeviasi(data,n,255));
int SDTotal=cvRound(countStandarDeviasi(data,0,255));
if (SDKiri==0||SDKanan==0){
if(SDKiri>SDKanan){
if (SDTotal>SDKiri)
ST=cvRound(SDTotal-SDKiri);
else
ST=cvRound(SDKiri-SDTotal);
}
else {
if (SDTotal>SDKanan)
ST=cvRound(SDTotal-SDKanan);
else
ST=cvRound(SDKanan-SDTotal);
}
}else{
if(SDKiri>SDKanan) ST=cvRound(SDKanan);
else ST=cvRound(SDKiri);
}
return ST;
}
// Versi Kadek
int countNumberK(int data[],int n){
int ST=0;
int SDKiri= cvRound(countStandarDeviasi(data,0,n));
int SDKanan=cvRound(countStandarDeviasi(data,n,255));
int SDTotal=cvRound(countStandarDeviasi(data,0,255));
if (SDKiri==0||SDKanan==0){
ST=cvRound(sqrt(SDKiri+SDKanan));
}else{
if(SDKiri>SDKanan) ST=cvRound(SDKanan);
else ST=cvRound(SDKiri);
}
return ST;
}