MATLAB: Trying to understand pixel value.

digital image processing

I am trying to understand pixel value, and trying to learn to alter it. But things act weird in matlab. Correct me if I am wrong but, pixel value is in a way amound of "light" a colour has. 0 means no light= black, 255 full light. Everything in between is something like, light blue dark blue and etc. So I googled "blue 300×300" and used this code:
clc;
clear all;
f = imread('C:\Users\*****\Desktop\images\sfaafsfas.jpg');
for i=0:255
f(f==i)=12;
end
imshow(f)
If I delete for loop, and look at the f value from workshot all the values of the pixel value (at least I think they are pixel values) all of them are 12. Then I add for loop, and check the values, still they are all 12, yet imshow shows me a grey image. It kinda makes sence, yet it does not too. Nothing chages but something changes. What am I missing here?
sfaafsfas.jpg
original
untitlefasd.jpg
What I get.

Best Answer

"What am I missing here?"
You have ignored the fact that RGB images have three color channels, which in order to generate your "light-blue" square have quite different values. This is explained in the link that I gave you:
You also made a basic beginners mistake, which is to not actually look at your data thoroughly:
>> f = imread('sfaafsfas.jpg');
>> R = f(:,:,1);
>> G = f(:,:,2);
>> B = f(:,:,3);
>> unique(R(:))
ans = 12
>> unique(G(:))
ans = 186
>> unique(B(:))
ans = 255
The code I gave you earlier:
would have made it clear that your assumption that "all of them are 12" is incorrect: all of the blue channel values are 12, but none of the red or green channel values are. But in your loop you set all elements of the entire 3D array (i.e. all three color channels) to the same value (12), which for RGB by definition must be a gray color (if all channels have the same value then a pixel is gray).
"But things act weird in matlab. "
So far everything seems to be acting exactly as expected.
Related Question