This code gives you the histogram of a grayscale image without using the built-in functions of MATLAB. After calculating the histogram the code also performs image equalization and presents both results.
Image histograms are present on many modern digital cameras. Photographers can use them as an aid to show the distribution of tones captured, and whether image detail has been lost to blown-out highlights or blacked-out shadows. This is less useful when using a raw image format, as the dynamic range of the displayed image may only be an approximation to that in the raw file.
The horizontal axis of the graph represents the tonal variations, while the vertical axis represents the total number of pixels in that particular tone.The left side of the horizontal axis represents the dark areas, the middle represents mid-tone values and the right hand side represents light areas. The vertical axis represents the size of the area (total number of pixels) that is captured in each one of these zones. Thus, the histogram for a very dark image will have most of its data points on the left side and center of the graph.
Conversely, the histogram for a very bright image with few dark areas and/or shadows will have most of its data points on the right side and center of the graph.
The histogram of a digital image with intensity levels in the range [0, L-1] is a discrete function h(rk) = nk, where rk is the kth intensity value and nk is the number of pixels in the image with intensity rk. It is common practice to normalize a histogram by dividing each of its components by the total number of pixels in the image, denoted by the product MN, where, as usual, M and N are the rows and columns dimensions of the image. Thus, a normalized histogram is given by p(rk) = rk / MN, for k=0,1,2….,k-1. Loosely speaking, p(rk) is an estimate of the probability of occurrence of intensity level rk in an image. The sum of all components of a normalized histogram is equal to 1.
Image Manipulation and Histograms
Image editors typically create a histogram of the image being edited. The histogram plots the number of pixels in the image (vertical axis) with a particular brightness or tonal value (horizontal axis). Algorithms in the digital editor allow the user to visually adjust the brightness value of each pixel and to dynamically display the results as adjustments are made. Histogram equalization is a popular example of these algorithms. Improvements in picture brightness and contrast can thus be obtained.
In the field of computer vision, image histograms can be useful tools for thresholding. Because the information contained in the graph is a representation of pixel distribution as a function of tonal variation, image histograms can be analyzed for peaks and/or valleys. This threshold value can then be used for edge detection, image segmentation, and co-occurrence matrices.
- Ed Sutton. “Histograms and the Zone System”. Illustrated Photography. Archived from the original on 2015-02-23. Retrieved 2015-08-31.
- Michael Freeman (2005). The Digital SLR Handbook. Ilex. ISBN 1-904705-36-7.
- Martin Evening (2007). Adobe Photoshop CS3 for Photographers: A Professional Image Editor’s Guide… Focal Press. ISBN 978-0-240-52028-5.
- CAMERA HISTOGRAMS: TONES & CONTRAST at cambridgeincolour.com