-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhisto_plot_Benny.cpp
More file actions
101 lines (81 loc) · 2.13 KB
/
histo_plot_Benny.cpp
File metadata and controls
101 lines (81 loc) · 2.13 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
// ========================================================
// histo_plot.cpp - Histogram program.
//
// Written by: Christyn Vasquez
// ========================================================
#include "IP.h"
using namespace std;
void histo_plot(imageP, imageP, int);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// main:
//
// Main routine for quantization of image.
//
int
main(int argc, char** argv)
{
int flg;
imageP inImage, outImage;
// Checks image version
if (argc != 4) {
cerr << "Usage: histo_plot imageP imageP bool\n";
exit(1);
}
// Read input image and reserve space for output
inImage = IP_readImage(argv[1]);
outImage = NEWIMAGE;
//Read flag
flg = atoi(argv[3]);
// Quantization image and save result in file
histo_plot(inImage, outImage, flg);
IP_saveImage(outImage, argv[2]);
// Free up image structures/memory
IP_freeImage(inImage);
IP_freeImage(outImage);
return 1;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// histo_plot:
//
// Evaluates the histogram of input image and plots it into an
// output image. Black background, white foreground.
// X - axis : intensity Y- axis : frequency
//
void
histo_plot(imageP inImage, imageP outImage, int flg)
{
int i, total;
int x, y, v;
uchar *in, *out, H[MXGRAY];
// Init number of pixels in image
total = inImage->width * inImage->height;
// Init out image dimensions
outImage->width = 256;
outImage->height = 256;
outImage->image = (uchar *) malloc(total);
if(outImage->image == NULL) {
cerr << "qntz: Insufficient memory\n";
exit(1);
}
in = inImage->image; // Input image buffer
out = outImage->image; // Output image buffer
// Create an empty histogram array
for(i = 0; i < MXGRAY; i++) H[i] = 0;
// Fill array with values in image
for(i = 0; i < total; i++) {
H[in[i]]++;
}
if (flg == 0){
int scale = 128 / (inImage->width * inImage->height / MXGRAY);
for (i = 0; i <256; i++){
H[i] = H[i]*scale;
if(H[i] > 255) H[i] = 0;
}}
// Plot histogram in out file
for(x = 0; x < (256); x++) {
for(y=1; y <= H[x]; y++) {
v = (256)*(256-y)+x;
out[v] = MaxGray;
}
}
}