-
Notifications
You must be signed in to change notification settings - Fork 1
/
Histogram.cpp
72 lines (65 loc) · 1.39 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
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <stack>
#include <string>
#include "Image.h"
#include "Histogram.h"
using namespace std;
Histogram::Histogram(){
}
Histogram::~Histogram(){
}
Histogram::Histogram(Image& img){
int height = img.get_height();
int width = img.get_width();
vector < vector <int> > brightness(height,vector <int> (width));
for(int i = 0; i<height; i++)
{
for(int j = 0; j<width; j++)
{
brightness[i][j] = (img.get_Color_x_y(i,j).get_red() + img.get_Color_x_y(i,j).get_blue() + img.get_Color_x_y(i,j).get_green())/3;
}
}
for(int i=0;i<256;i++){
histogram.push_back(0);
}
for(int i = 0; i<height; i++)
{
for(int j = 0; j<width; j++)
{
histogram[brightness[i][j]]++;
}
}
for(int i = 5; i<251; i++)
{
int flag = 0;
for(int j = 1; j<5; j++)
{
if(histogram[i] < histogram[i-j] && histogram[i] <= histogram[i+j])
{
continue;
}
else
{
flag = 1;
}
}
if(flag == 0)
{
threshold.push_back(i);
}
}
}
float Histogram::medianThreshold(){
int size = threshold.size();
sort(threshold.begin() , threshold.end());
if(size == 0){
return 122;
}
else if(size%2 == 0)
return (threshold[size/2] + threshold[size/2 -1])/2;
else
return threshold[size/2];
}