# Histogram - IIT Bombay

advertisement
Two-week ISTE workshop on
Effective teaching/learning of computer
programming
Dr Deepak B Phatak
Subrao Nilekani Chair Professor
Department of CSE, Kanwal Rekhi Building
IIT Bombay
Lectures 11, Matrices -examples
Saturday 3 July 2010
[Several slides courtesy Anirudha Jathar and Wikipedia]
Overview
 A quiz
 Handling images
• Representation of digital images
• Histogram
• Contrast enhancement
 Example program to calculate histogram of a small digital
image
A quiz
Q. Currently, what activity do you propose to carry out within two
weeks of the workshop conclusion, for the mandatory
submission
A. Design questions and answers for examinations
B. Design quiz questions and answers
C. Add lecture slides with examples
D. Something different
Digital images
 Digital images are a collection of pixel values
 These are arranged in an array (W x H)
 Each pixel value can be represented by
• 1 bit (m : mono colour, e.g. black and white)
• 8 bits (g : gray scale 0 – black to 255 – white)
• 24 bits (c: Red, Blue, Green, each one byte)
 One can have 16 million colours!
• Capacity of a human eye is limited to a
small range from 200 to 2000 colours
Digital images ...
 While storing information about an image in a file, we mainly
need values of Width, Height, the type of colours present,
and values for each pixel
 Images such as black and white fingerprints have small size
(500 x 300)
 For large images, compression is mandatory to keep the file
size within limits
• 12 M pixel camera can produce 36 M bytes image
 Compression can be either lossy or lossless
 Several file formats have evolved
raw, png, bmp, tiff, giff, jpeg, xmp
 Refer to wikipedia (Image_file_formats)
Images and histograms
 Pixel values of digital images can be read in a matrix for
further processing
 Each picture point (pixel) has an associated “tonal” value
• For grayscale images, the value range is 0-255
• 0: Black, 255: White
 Thus each element of such an image matrix would contain a
value which can be of the type/size short int or char (1 byte)
 Histogram indicates how many pixels in an image have the
same value
A sample image 8 pixel x 8 pixel
Pixel values in the sample image
Histogram values
Val
52
55
58
59
60
61
62
63
n
1
3
2
3
1
4
1
2
Val
64
65
66
67
68
69
70
71
n Val
2 72
3 73
2 75
1 76
5 77
3 78
4 79
2 83
n
1
2
1
1
1
1
2
1
Val
85
87
88
90
94
104
106
109
n
2
1
1
1
1
2
1
1
Val
113
122
126
144
154
n
1
1
1
1
1
Cumulative Distribution Function (cdf)
V
52
55
58
59
60
61
62
63
c
1
4
6
9
10
14
15
17
V
64
65
66
67
68
69
70
71
c
19
22
24
25
30
33
37
39
V
72
73
75
76
77
78
79
83
c
40
42
43
44
45
46
48
49
V c
85 51
87 52
88 53
90 54
94 55
104 57
106 58
109 59
V
113
122
126
144
154
c
60
61
62
63
64
Histogram equalization
 The histogram equalization formula
 “Equalization” formula for example image
Histogram equalization …
 For example, the cdf of 78 is 46
Pixel values after histogram equalization
Enhancement of Contrast
Original Picture for comparison
Another grayscale picture
Histogram and cdf
“Equalized” histogram and cdf
Picture with enhanced contrast
Original picture for comparison
Program to calculate histogram
#include<iostream>
using namespace std;
int main(){
int i, j, npix;
int image[500][500], histogram[256];
/* we assume that the image data is contained in a text file
"image.txt“ in a suitable format
Also, we will use command line redirection to read data from
this file
*/
Histogram …
cin >> npix; // number of pixels
//read image pixel values in the matrix
for(i=0; i< npix; i++){
for(j=0; j < npix; j++){
cin >> image[i][j];
cout << image[i][j] << " ";
}
cout << "\n";
}
Histogram …
// set histogram counts to zero
//
for(i=0 ; i< 256; i++) histogram[i] = 0;
// Calculate histogram values
for(i=0; i< npix; i++){
for (j = 0; j < npix; j++){
//
based on the value of the pixel
//
increment the corresponding count
//
in the histogram array
histogram[image[i][j]]++;
}
}
Histogram …
// print the histogram at non zero values
cout << "Histogram at non zero values is:”;
cout << "\n";
for (i=0; i<256; i++){
if (histogram[i] !=0) {
cout << i << " " << histogram[i] << "\n";
}
}
Histogram …
// find the maximum value in the histogram
int imax, max = 0;
for (i=0; i<256; i++){
if (histogram[i] > max) max = histogram[i];
}
cout << "Maximum histogram value " << max
cout << “ occurs at:” << "\n";
// Print the grey levels at which max value occurs
for(i=0; i<256; i++){
if (histogram[i]== max) cout << i << " ";
}
return 0;
}
Image of a finger print
THANK YOU
Question, From GEC Thrisur
• Can we return structure from the function
?
Question, From PSG Coimbatore
•
Does C support function overloading ?
Lecture 11 Examples of matrix
manipulation
Question, From PSG Coimbatore
• Why is C, C++ generally use to design and develop
an operating system ?
Lecture 11 Examples of matrix
manipulation
Question, From NIT, Kurukshetra
•
What is the application of bit wise operators ?
Lecture 11 Examples of matrix
manipulation
Question, From GEC Thrisur
• What is the maximum dimension possible for a multi
dimension array ?
Lecture 11 Examples of matrix
manipulation
Question, From GEC Thrisur
• Earlier Pascal was use to discuss programming
methodology, later why its turn to C ?
Lecture 11 Examples of matrix
manipulation
Question, From PSG Coimbatore
• If C is not supporting function overloading mean how it is
possible to use different number of scanf with different
argument ?
Lecture 11 Examples of matrix
manipulation
Question, From NIT Kurukshetra
• When we declare float instruct and why scanning
value, It will display floating point abnormal
termination error ?
Lecture 11 Examples of matrix
manipulation
Question, From NIT Kurukshetra
• Is cin and cout return any value?
Lecture 11 Examples of matrix
manipulation
Question, From NIT Jalandhar
• What are macros and why they are taking lesser time during
compilation?
Lecture 11 Examples of matrix
manipulation

– Cards

– Cards

– Cards

– Cards

– Cards