forked from Dwayne-Phillips/CIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhist.c
executable file
·188 lines (152 loc) · 4.64 KB
/
hist.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
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
/**************************************************
*
* file hist.c
*
* Functions: This file contains
* calculate_histogram
* perform_histogram_equalization
* zero_histogram
* smooth_histogram
*
* Purpose: These functions calculate
* the histogram of an input image array.
* They also modify an image by equalizing
* its histogram.
*
* Modifications:
* July 86 - ported to IBM-PC
* August 1990 - modified for use in the
* C Image Processing System
* March 1992 - removed the hardwired values
* of 100 and replaced them with ROWS
* and COLS. There are still some
* hardwired numbers in this file, but
* they deal with displaying a histogram.
* October 4, 1992 - added the smooth histogram
* function.
* April 22, 1998 - modified routines to work
* with an entire image in one array.
*
**************************************************/
#include "cips.h"
#define PRINT_WIDTH 80
#define FORMFEED '\014'
/*****************************************
*
* zero_histogram(...
*
* This function clears or zeros a
* histogram array.
*
******************************************/
int zero_histogram(histogram, gray_levels)
int gray_levels;
unsigned long histogram[];
{
int i;
for(i=0; i<gray_levels; i++)
histogram[i] = 0;
return(1);
} /* ends zero_histogram */
/*****************************************
*
* calculate_histogram(...
*
* This function calculates the histogram
* for an input image array.
*
******************************************/
int calculate_histogram(image, histogram, length, width)
int length, width;
short **image;
unsigned long histogram[];
{
long i,j;
short k;
for(i=0; i<length; i++){
for(j=0; j<width; j++){
k = image[i][j];
histogram[k] = histogram[k] + 1;
}
}
return(1);
} /* ends calculate_histogram */
/********************************************
*
* smooth_histogram(...
*
* This function smoothes the input histogram
* and returns it. It uses a simple averaging
* scheme where each point in the histogram
* is replaced by the average of itself and
* the two points on either side of it.
*
*********************************************/
int smooth_histogram(histogram, gray_levels)
int gray_levels;
unsigned long histogram[];
{
int i;
unsigned long new_hist[gray_levels];
zero_histogram(new_hist, gray_levels);
new_hist[0] = (histogram[0] + histogram[1])/2;
new_hist[gray_levels] =
(histogram[gray_levels] +
histogram[gray_levels-1])/2;
for(i=1; i<gray_levels-1; i++){
new_hist[i] = (histogram[i-1] +
histogram[i] +
histogram[i+1])/3;
}
for(i=0; i<gray_levels; i++)
histogram[i] = new_hist[i];
return(1);
} /* ends smooth_histogram */
/*****************************************
*
* perform_histogram_equalization(...
*
* This function performs histogram
* equalization on the input image array.
*
******************************************/
int perform_histogram_equalization(image,
histogram,
gray_levels,
new_grays,
length,
width)
int gray_levels, new_grays;
long length, width;
short **image;
unsigned long histogram[];
{
int i,
j,
k;
unsigned long sum,
sum_of_h[gray_levels];
double constant;
sum = 0;
for(i=0; i<gray_levels; i++){
sum = sum + histogram[i];
sum_of_h[i] = sum;
}
/* constant = new # of gray levels div by area */
constant = (float)(new_grays)/(float)(length*width);
for(i=0; i<length; i++){
for(j=0; j<width; j++){
k = image[i][j];
image[i][j] = sum_of_h[k] * constant;
}
}
return(1);
} /* ends perform_histogram_equalization */
int hist_long_clear_buffer(string)
char string[];
{
int i;
for(i=0; i<300; i++)
string[i] = ' ';
return(1);
}