MATLAB: How to get the RGB values of a photo taken with a webcam

color anaysis

For example, I took the photo of a pure red using webcam, and I should get these values R=255, G=0, B=0. The code should give these values. Also, these values should not be affected by the environment I mean, I have to get the same values of pure red for each different environment

Best Answer

Cameras are always affected by noise, so even if your object was at constant illumination, was larger than the field of view, was fixed distance, the lens parameters were not changed, and so on -- even in all of those conditions, not all of the pixel values will come out the same. The classic test for this is to put the lens cap on a camera and take a picture: you will seldom get out "all black" for all pixels.
There is essentially no such thing as "pure red" in physical objects. You can get hydrogen red as an emission from burning some things, or from lasers, but short of that you are dealing with absorption spectra based upon light with a distribution of photon energies. The light is being sensed by three photo receptors tuned to different but overlapping ranges. The photo receptors accumulate energy for a period of time. They do not recognize specific frequencies, they just accumulate energy -- which means that if you shone a different frequency that was only received half as efficiently but was twice as bright, then the receptors would not be able to tell the difference in energy. The conversion of relative energies over the period of time, into RGB values, is done under the model that a particular kind of light is being used; if a different kind of light was what was really used, then the conversion will be wrong.
It is the old effect of standing under a sodium vapor streetlight and looking terrible: your "real" color has not changed, but the illumination has. If your requirement held, that the colors were not to be affected by the environment, then it would follow that a picture of you would have to look the same regardless of whether you were standing in bright sunlight or if you were standing under one of those streetlights. You can (generally) make those kinds of color corrections if you use enough sensor types and measure the right things, but you really need special equipment.
Anyhow... Image Analyst has posted on how to do illumination correction.
Related Question