-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistogram_Equalization_Cumulative_method.cpp
More file actions
130 lines (110 loc) · 3.43 KB
/
Histogram_Equalization_Cumulative_method.cpp
File metadata and controls
130 lines (110 loc) · 3.43 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
#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];
/*
for (int l=0;l<256;l++){
cout << (int)histogram[l] ;
}
*/
//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]]++;
}
}
int total_no_of_pixels = Size1*Size2;
int no_elements = total_no_of_pixels/256;
//int bucket[256];
int counter = histogram[0]; //To Keep the count of number of pixels with a particular intensity value
unsigned char index = 0; //Variable for storing no of intensisty values i.e 255
int bucket = 0; //Intensity values for the new histogram
cout << histogram[0]<< endl;
unsigned char FinalImage[400][400][1];
for(int i=0;i<400;i++){
for(int j=0;j<400;j++){
FinalImage[i][j][0] = 0;
}
}
//Bucket Equalization Logic
for(int index =0; index < 256; index++) {
for(int k = 0;k < 400; k++) {
for(int m=0;m<400;m++){
if (Imagedata[k][m][0] == index) {
if(counter < 625){
counter = counter + 1;
FinalImage[k][m][0] = bucket;
}
else {
bucket = bucket + 1;
counter = 0;
}
}
}
}
//index = index + 1;
}
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][0]]++;
}
}
ofstream Histogram1;
Histogram1.open("Histogram_rose_mix_after_Cumulative_equalization.csv");
for(int i = 0; i < 256; i++){
Histogram1 << histogram1[i] << 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;
}