-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHIstogram_Equalization_Transfer_Function_Method.cpp
More file actions
163 lines (132 loc) · 3.95 KB
/
HIstogram_Equalization_Transfer_Function_Method.cpp
File metadata and controls
163 lines (132 loc) · 3.95 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
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
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
// Define file pointer and variables
FILE *file;
int BytesPerPixel;
int Size1 = 390;
int Size2 = 300;
// Check for proper syntax
if (argc < 3){
cout << "Syntax Error - Incorrect Parameter Usage:" << endl;
cout << "program_name input_image.raw output_image.raw [BytesPerPixel = 1] [Size = 256]" << endl;
return 0;
}
// Check if image is grayscale or color
if (argc < 4){
BytesPerPixel = 1; // default is grey image
}
else {
BytesPerPixel = atoi(argv[3]);
// Check if size is specified
if (argc >= 5){
Size1 = atoi(argv[4]);
Size2 = atoi(argv[5]);
}
}
// Allocate image data array
unsigned char Imagedata[Size1][Size2][BytesPerPixel];
// Read image (filename specified by first argument) into image data matrix
if (!(file=fopen(argv[1],"rb"))) {
cout << "Cannot open file: " << argv[1] <<endl;
exit(1);
}
fread(Imagedata, sizeof(unsigned char), Size1*Size2*BytesPerPixel, file);
fclose(file);
///////////////////////// INSERT YOUR PROCESSING CODE HERE /////////////////////////
//unsigned char Input_Image[Size1][Size2][BytesPerPixel] = Imagedata[Size1][Size2][BytesPerPixel];
//Creating a Histogram
double histogram[256] ;
for (int i=0;i < 256; i++) {
histogram[i] = 0;
}
for (int k=0;k<400;k++) {
for (int m=0;m<400;m++) {
histogram[Imagedata[k][m][1]]++;
}
}
//Saving histogram to a .csv
/*
ofstream Histogram;
Histogram.open("Histogram_rose_dark.csv");
for (int i=0;i < 256; i++) {
Histogram << histogram[i] << endl;
}
ofstream Histogram;
Histogram.open("Histogram_rose_bright.csv");
for (int i=0;i < 256; i++) {
Histogram << histogram[i] << endl;
}
*/
ofstream Histogram;
Histogram.open("Histogram_rose_mix.csv");
for (int i=0;i < 256; i++) {
Histogram << histogram[i] << endl;
}
//Obtaning Probability
double prob_histogram[256];
int total_no_of_pixels = Size1*Size2;
for (int i = 0; i< 256; i++){
prob_histogram[i] = histogram[i]/total_no_of_pixels;
}
//CDF
double cdf_histogram[256];
for (int j = 0; j < 256; j++){
if (j == 0) {
cdf_histogram[j] = prob_histogram[j];
}
else {
cdf_histogram[j] = prob_histogram[j] + cdf_histogram[j-1];
}
}
//Converting to 0-255 Range
unsigned char transformed_pixel_values[256];
for (int t =0; t<256;t++) {
transformed_pixel_values[t] = cdf_histogram[t]*255;
}
unsigned char FinalImage[400][400][1];
for (int k = 0; k <400; k++) {
for(int m=0;m<400;m++) {
FinalImage[k][m][1] = transformed_pixel_values[Imagedata[k][m][1]];
}
}
//Saving Histogram after Equalization
double histogram1[256] ;
for (int i=0;i < 256; i++) {
histogram1[i] = 0;
}
for (int k=0;k<400;k++) {
for (int m=0;m<400;m++) {
histogram1[FinalImage[k][m][1]]++;
}
}
/*
ofstream Histogram1;
Histogram1.open("Histogram_rose_dark_after_equalization.csv");
for (int j =0; j < 256; j++){
Histogram1 << histogram1[j] << endl;
}
ofstream Histogram1;
Histogram1.open("Histogram_rose_bright_after_equalization.csv");
for (int j =0; j < 256; j++){
Histogram1 << histogram1[j] << endl;
}
*/
ofstream Histogram1;
Histogram1.open("Histogram_rose_mix_after_equalization.csv");
for (int j =0; j < 256; j++){
Histogram1 << histogram1[j] << endl;
}
// Write image data (filename specified by second argument) from image data matrix
if (!(file=fopen(argv[2],"wb"))) {
cout << "Cannot open file: " << argv[2] << endl;
exit(1);
}
fwrite(FinalImage, sizeof(unsigned char), Size1*Size2*BytesPerPixel, file);
fclose(file);
return 0;
}