Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want source code to view the image histogram ,click a button to download the image and then click another button to display the histogram of this image
Posted

Gray-scale histogram is nearly trivial compared to color histogram (which is not trivial at all due to the fact that what to take for Luminosity is somewhat philosophical question, :-) for those who do not believe: see this: http://en.wikipedia.org/wiki/HSL_color_space[^]).

However, a even a gray-scale histogram is a bit less trivial then appears in the Answer by Albin. To start with, 255 is not a word constant like a speed of light. Usually, real bits per pixel vary from 8 to 16, but in practice a raw picture from a standard CMOS or CCD it's somewhere between 8 (8-bit mode) and 12 (16-bit mode). If remaining four bits are used in the output file really depends on in-camera processing: the dynamic range can be modified; the ultimate transformation use to use full range is called normalization.

As a result, the number of color bins can reach 212=4096 or 216=65536 which is bit to much to show directly. Number of bins should be arbitrarily selected based on the presentation and re-calculated into required number of bins grouping close colors together; the array should be of floating-point type and re-normalized to maintain consistent vertical scale.

Zinssame, please sort out your buttons and loading yourself and next time think of some better UI design (gosh, why everyone is asking about these buttons?). Access to the separate pixels fully depends on the library you want to use: System.Drawing or WPF (or anything else); you could have asked about the particular library you want to use. It is done in totally different ways. The techniques are well documented and sample codes are provided in MSDN, so it should not be a problem. You can ask a separate question if you face any problem.

—SA
 
Share this answer
 
Comments
Nuri Ismail 12-Mar-11 11:37am    
Very good explanation. My 5
Sergey Alexandrovich Kryukov 12-Mar-11 15:01pm    
Thank you.
--SA
Have an array indexed 0 to 255. Loop through the pixels of the image then increment the values in the array index. For example if the pixel value is 50 then FreqArray[50]= freqArray[50]+1;. Once you read all the pixels then plot the frequencies against the color bins (0 to 255). Thats the histogram. Is it really tough to code?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Mar-11 23:33pm    
This is just a bit more complex then that, please see what I answered...
--SA
Albin Abel 12-Mar-11 2:14am    
Agreed, The MAX depends on whether it is 8bit or 24 bit or 32 bits. But again we are going to plot frequencies of each bins. The bins can be any desired level and actual value can be scaled to the desired range for a histogram. As you said there will be grouping the range is very narrow i.e 0 to 255 for a 24 bits. More over Histogram is used to see a pattern, no need to worry much about actual values. So more importance is for the viewability and any operations can be applied to the actual values by simple transformations
Sergey Alexandrovich Kryukov 12-Mar-11 14:59pm    
That's correct. I only say the picture just a bit more complex.
--SA
Albin Abel 13-Mar-11 9:28am    
I was impressed by your answer. In fact after reading your answer I am trying to convert a image into HSV, then rescale V so I ll get some free bits. In that free bits I am adding V from another gray scale images. Before adding I juggle the V in pattern, so mostly a noise pattern, but can be reorganized. Just a try to hide one image in to another. Thanks for notifying about the bits can be use for storing specific information

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900