-
Notifications
You must be signed in to change notification settings - Fork 0
/
histo.c
121 lines (99 loc) · 3.76 KB
/
histo.c
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
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to perform histogram equalisation on an image
// Function takes total rows, columns, input file name and output
// file name as parameters
void histogramEqualisation(int cols, int rows,
char* input_file_name, char* output_file_name)
{
// creating image pointer
unsigned char* image;
// Declaring 2 arrays for storing histogram values (frequencies) and
// new gray level values (newly mapped pixel values as per algorithm)
int hist[256] = { 0 };
int new_gray_level[256] = { 0 };
// Declaring other important variables
int input_file, output_file, col, row, total, curr, i;
// allocating image array the size equivalent to number of columns
// of the image to read one row of an image at a time
image = (unsigned char*)calloc(cols, sizeof(unsigned char));
// opening input file in Read Only Mode
input_file = open("img1.bmp", O_RDONLY);
if (input_file < 0) {
printf("Error opening input file\n");
exit(1);
}
// creating output file that has write and read access
output_file = creat("hisimage.bmp", 0666);
if (output_file < 0) {
printf("Error creating output file\n");
exit(1);
}
// Calculating frequency of occurrence for all pixel values
for (row = 0; row < rows; row++) {
// reading a row of image
read(input_file, &image[0], cols * sizeof(unsigned char));
// logic for calculating histogram
for (col = 0; col < cols; col++)
hist[(int)image[col]]++;
}
// calulating total number of pixels
total = cols * rows;
curr = 0;
// calculating cumulative frequency and new gray levels
for (i = 0; i < 256; i++) {
// cumulative frequency
curr += hist[i];
// calculating new gray level after multiplying by
// maximum gray count which is 255 and dividing by
// total number of pixels
new_gray_level[i] = ((((float)curr) * 255) / total);
}
//round
// closing file
close(input_file);
// FILE * f1 = fopen(str, "r");
// reopening file in Read Only Mode
//printf("\n Insert the Image name you want to Modify/Enhance \n\n");
// char str[20];
// input_file = open(str, O_RDONLY);
input_file = open("img1.bmp", O_RDONLY);
// performing histogram equalisation by mapping new gray levels
for (row = 0; row < rows; row++) {
// reading a row of image
read(input_file, &image[0], cols * sizeof(unsigned char));
// mapping to new gray level values
for (col = 0; col < cols; col++)
image[col] = (unsigned char)new_gray_level[image[col]];
// reading new gray level mapped row of image
write(output_file, &image[0], cols * sizeof(unsigned char));
}
// freeing dynamically allocated memory
free(image);
// closing input and output files
close(input_file);
close(output_file);
}
// driver code
int main()
{
// declaring variables
char* input_file_name;
char* output_file_name;
int cols, rows;
// defining number of rows and columns in an image
// here, image size is 512*512
cols = 512;
rows = 512;
// defining input file name (input image name)
// this boat_512_512 is a raw grayscale image
input_file_name = "boat_512_512";
// defining output file name (output image name)
output_file_name = "boat_512_512_histogram_equalised";
// calling function to do histogram equalisation
histogramEqualisation(cols, rows, input_file_name, output_file_name);
return 0;
}