How can I get the RGB values of a photo taken with a webcam?
4 views (last 30 days)
Show older comments
caner kaya
on 11 Oct 2017
Commented: Walter Roberson
on 12 Oct 2017
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
0 Comments
Accepted Answer
Walter Roberson
on 11 Oct 2017
You index the arrays like we discussed in https://www.mathworks.com/matlabcentral/answers/360646-how-can-i-get-a-rgb-values-from-a-picture
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.
2 Comments
Walter Roberson
on 12 Oct 2017
As discussed before:
Once you have done the snapshot(), then just index into the resulting array to get the RGB values. For example,
img(183, 17, :)
would extract the R, G, and B components (in that order) of row 183 column 17 of the image stored in the variable img
More specifically,
row = 183; %change this to the row you want to extract from
column = 17; %change this to the column you want to extract from
R = img(row, column, 1);
G = img(row, column, 2);
B = img(row, column, 3);
fprintf('The Red at (%d,%d) was value %d\n', row, column, R);
fprintf('The Green at (%d,%d) was value %d\n', row, column, G);
fprintf('The Blue at (%d,%d) was value %d\n', row, column, B);
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!