Image processing help. Regarding pixel values and not needing to click on diagram to select pixel.

From what I understand, matlab mostly requires us to click on an image and then it will display the resulting pixel RGB value. Is there a way to let the code run through, finding ALL pixel's individual values without having to click? I do not want to select because I want to iterate through the whole picture.
How do I use code to get just R,G and B respectively of a pixel? Like when a pixel is selected, what code will give only R portion of RGB. I wish to compile RGB together meaning adding R, G and B to get my resulting pixel value.
Please help. Thanks.

 Risposta accettata

You can read in an image file with imread(). That will get you ALL the image pixel values without having to click on anything.
rgbImage = imread(fullFileName);
To split apart an RGB image into the individual color channels, you can do this:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
To recombine them into an RGB image again, do this:
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);

7 Commenti

Ok. So what if I want to get pixel value for say (123,123) of the image, how do i extract out the value?
rgbValue = impixel(123, 123);
Or to get the green channel only, and if it's an RGB image, use 2 as the third index
greenValue = rgbImage(123, 123, 2);
Be careful not to confuse x,y with row,column like most people do You want rgbImage(y,x,color), NOT rgbImage(x,y,color).
Thank you. This has helped me alot. One last question not really related. My array declared saturates at 255. How do I change it so that the cap of each number is not at 255 but more? eg. array = zeros([765,1,1,1]); Declared like this but how to change it? My array is size 765 by 1. Thats not the important part but I want each number to be able to hold value larger than 255.
You can cast the array to double or uint16.
array16 = zeros(rows, columns, 'uint16'); % Max is 65535
arrayDouble = zeros(rows, columns); % Max is realmax('double')
>> realmax('double')
ans =
1.79769313486232e+308
>> intmax('uint16')
ans =
65535
If that answers your question, can you mark it as "Accepted"?
Somehow when I printed out the values, array element is still capped at 255. RED, BLUE, and GREEN all prints out correct values but total_Value caps at 255. Why is this so? This is part of the code:
for y = 1:inputB
disp(['Image is now at [', num2str(x), ',', num2str(y),']']);
RED = I(y, x, 1) + 1;
GREEN = I(y, x, 2);
BLUE = I(y, x, 3);
total_Value = RED + GREEN + BLUE;
disp(['Ans is [', num2str(RED),',', num2str(GREEN),',',num2str(BLUE),',', num2str(total_Value), ']']);
array(total_Value) = array(total_Value) + 1;
end
They must be uint8. What does this reveal if you put it right before the second disp():
whos I
whos RED
whos GREEN
whos BLUE
whos total_value
whos array
Try casting to uint16 before summing.
total_Value = uint16(RED) + uint16(GREEN) + uint16(BLUE);
Oh it works now. The problem was that I did not cast it to uint16 before summing. Just adding the "total_Value = uint16(RED) + uint16(GREEN) + uint16(BLUE);" line and it works like I wanted. Thank you.

Accedi per commentare.

Più risposte (0)

Richiesto:

il 26 Dic 2014

Commentato:

il 29 Dic 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by