MATLAB: How can i extract numerical intensity values from an image

digital image processingimage analysisImage Processing Toolbox

Consider an image sample.jpg Now I want to extract numerical value of pixel intensity for every pixel and the pixel location on the X and y axis such that I will have three columns X location y location and their corresponding pixel intensity. Please can someone help me with a code or a direction on how I can accomplish this?

Best Answer

You can make use of the fact that an image is already a matrix. Instead of ndgrid (as used in this code), you could also consider using fullfact (although that requires the statistics toolbox).
%generate a random image to make this code run
IM=uint8(randi(255,10,5));
%note that the convention is to have the y-axis flipped in images
[Y,X]=ndgrid(1:size(IM,1),size(IM,2):-1:1);
%convert the intensity to double to make sure the entire table is a double,
%which allows x and y to be greater than 255
data=[X(:),Y(:),double(IM(:))];