From: Rise on
Hi, all,

For each pixel in each image of a sequence of 15 images, I need to store its RGB intensity values so that if I want, I can plot any pixel's R, G and/or B's intensity values across the sequence.

I've been reading about structs and cells and vectors, but I am not sure what is best to use.

Hope this makes sense. Much thanks. - Rise
From: David Young on
"Rise " <rise_riyo(a)hms.harvard.edu> wrote in message <hukvdl$ank$1(a)fred.mathworks.com>...
> Hi, all,
>
> For each pixel in each image of a sequence of 15 images, I need to store its RGB intensity values so that if I want, I can plot any pixel's R, G and/or B's intensity values across the sequence.
>
> I've been reading about structs and cells and vectors, but I am not sure what is best to use.
>
> Hope this makes sense. Much thanks. - Rise

I think you need to clarify the problem a little. Your question is posed as how to store the intensity values, but they must be already stored somehow, and what you really want to do is plot them. Presumably an example would be plot a graph of the R component for the pixel at row 29, column 726 against image number?

To do this, you would need to put the values to be plotted into a vector. The problem is how to get them there - and that, in turn, depends on how the images are stored. So can you describe that? For example, are they files on disk, or have they been read into main memory, or are you acquiring them from a camera as you go along? It would also help to know how big the images are - can they all be stored in main memory at once?
From: Rise on
Hi, David,

Sorry for the confusion. I have three datasets; each dataset consists of 15 images of a still object in a static scene. The size of each set's images is different; e.g., one set has an image size of 912x1247. The datasets are on my local hard drive.

% In my code, I read in one dataset:
thepath = 'M:\MATLAB\example_image-segmentation-using-kmeans-clustering\';
filelist = dir(fullfile(thepath,'new-*.tif'));
fileNames = {filelist.name}'

for k = 1:nImages
I = imread(fileNames{k});
[rows, cols] = size(I);

% Initially, I thought of using a struct
intensity(k).r = I(:,:,1); % red channel's pixel intensity
intensity(k).g = I(:,:,2); % green channel's pixel intensity
intensity(k).b = I(:,:,3); % blue channel's pixel intensity

% this is where I get stuck as it does not create a list of corresponding pixel's
% intensity values; it simply writes over the previous image's pixel's values
for r=1:rows
for c=1:cols
correspond_pixs_red(r,c) = intensity(k).r(r,c);
correspond_pixs_green(r,c) = intensity(k).g(r,c);
correspond_pixs_blue(r,c) = intensity(k).b(r,c);
end
end

% plot a pixel's R,G,B intensity values across the sequence
x=1:nImages;
figure,plot(x, intensity(k).r(r,c ), 'r:+', x, intensity(k).g(r,c ),'g-v', x, intensity(k).b(r, c),'b-o');
title('RGB Intensities per Pixel per Image');
xlabel('Number of Images in Sequence');
ylabel('Pixel RGB Intensity');
end

So, yes, I do have two issues:
1) one is trying to figure out a way of getting a pixel and its corresponding pixels' R,G, and B intensity values in the other images so that I have pixel1's RGB from image1, pixel1's RGB from image2, pixel1 from image3, ...,pixel1's RGB from image15, pixel2's RGB in image1, etc., and
2) plotting a pixel's intensity profile across the sequence.

I know my code does not work because 1) it just writes over a pixel's RGB values in the matrices and does not list the intensity values, respectively, for each pixel and its corresponding pixels, and 2) I haven't figured out a way to plot a "pixel's intensity profile" across the sequence.

I thought of an array of vectors would be better to use than a struct, but I think I am confusing myself even more with the syntax.

Thanks for your assistance. - Rise

"David Young" <d.s.young.notthisbit(a)sussex.ac.uk> wrote in message <hul3oc$s6i$1(a)fred.mathworks.com>...
> "Rise " <rise_riyo(a)hms.harvard.edu> wrote in message <hukvdl$ank$1(a)fred.mathworks.com>...
> > Hi, all,
> >
> > For each pixel in each image of a sequence of 15 images, I need to store its RGB intensity values so that if I want, I can plot any pixel's R, G and/or B's intensity values across the sequence.
> >
> > I've been reading about structs and cells and vectors, but I am not sure what is best to use.
> >
> > Hope this makes sense. Much thanks. - Rise
>
> I think you need to clarify the problem a little. Your question is posed as how to store the intensity values, but they must be already stored somehow, and what you really want to do is plot them. Presumably an example would be plot a graph of the R component for the pixel at row 29, column 726 against image number?
>
> To do this, you would need to put the values to be plotted into a vector. The problem is how to get them there - and that, in turn, depends on how the images are stored. So can you describe that? For example, are they files on disk, or have they been read into main memory, or are you acquiring them from a camera as you go along? It would also help to know how big the images are - can they all be stored in main memory at once?
From: ImageAnalyst on
You mean like in this demo where I extract the value of the image at
line 10, column 30 for each of 114 frames in the Rhino demo movie?
You'd just have to adapt it to extract the RGB values instead of
converting it to gray and getting the gray value - a very simple
modification.

% Demo macro to extract frames and get frame means from an avi movie
% and save individual frames to separate image files.
clc;
close all;

% Open the rhino.avi demo movie that ships with MATLAB.
movieFullFileName = 'C:\Program Files\MATLAB\R2010a\toolbox\images
\imdemos\rhinos.avi';
% Check to see that it exists.
if ~exist(movieFullFileName, 'file')
strErrorMessage = sprintf('File not found:\n%s\nYou can choose a new
one, or cancel', movieFullFileName);
response = questdlg(strErrorMessage, 'File not found', 'OK - choose a
new movie.', 'Cancel', 'OK - choose a new movie.');
if strcmpi(response, 'OK - choose a new movie.')
[baseFileName, folderName, FilterIndex] = uigetfile('*.avi');
if ~isequal(baseFileName, 0)
movieFullFileName = fullfile(folderName, baseFileName);
else
return;
end
else
return;
end
end
try
mov = aviread(movieFullFileName);
% movie(mov);
% Make a special output folder to hold all the separate movie frames
% as their own image files.
outputFolder = fullfile(cd, 'Rhino Movie');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
% Determine how many frames there are.
numberOfFrames = size(mov, 2);
numberOfFramesWritten = 0;
% Prepare a figure to show the images in the upper half of the
screen.
figure;
screenSize = get(0, 'ScreenSize');
newWindowPosition = [1 screenSize(4)/2 - 70 screenSize(3)
screenSize(4)/2];
set(gcf, 'Position', newWindowPosition); % Maximize figure.
% set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

% Loop through the movie, writing all frames out.
% Each frame will be in a separate file with unique name.
meanGrayLevels = zeros(numberOfFrames, 1);
for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = mov(frame).cdata;

% Display it
subplot(1,2,1);
image(thisFrame);
drawnow; % Force it to refresh the window.
axis square;

% Convert from RGB to gray.
grayImage = rgb2gray(thisFrame);

% % Calculate the mean gray level.
% meanGrayLevels(frame) = mean(grayImage(:));
% Calculate the gray level at line 10, column 30.
meanGrayLevels(frame) = grayImage(10,30);

% Plot the mean gray levels.
subplot(1,2,2);
plot(meanGrayLevels);
if frame == 1
title('Gray Levels at line 10, column 30');
xlabel('Frame Number');
yLabel('Gray Level');
end

% Construct an output image file name.
outputBaseFileName = sprintf('Frame %4.4d.png', frame);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Write the image array to the output file.
% imwrite(thisFrame, outputFullFileName, 'png');
% Update user with the progress. Display in the command window.
progressIndication = sprintf('Processed frame %4d of %d.', frame,
numberOfFrames);
disp(progressIndication);
% Increment frame count (should eventually = numberOfFrames
% unless an error happens).
numberOfFramesWritten = numberOfFramesWritten + 1;
end
catch ME
% Some error happened if you get here.
stError = lasterror;
strErrorMessage = sprintf('Error extracting movie frames from:\n\n%s\n
\nError: %s\n\n)', movieFullFileName, stError.message);
msgboxw(strErrorMessage);
end

% Alert user that we're done.
finishedMessage = sprintf('Done! It processed %d frames to folder
\n"%s"', numberOfFramesWritten, outputFolder);
disp(finishedMessage); % Write to command window.
msgbox(finishedMessage); % Also pop up a message box.

From: Rise on
Hi, ImageAnalyst,

Whoa. For a newbie like myself, I need to *slowly* digest what you just did in your code below. But yes, running your code -- it does exactly what I would like to do with my datasets; that is, to display the RGB intensity values that a pixel and its corresponding pixels have in the image sequence which then gives the pixel's measured intensity profile.

I will try to modify your program for my datasets. Thank you very much.

Sincerely, Rise :-)

ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <9c4c8198-2475-4c3b-ab5d-ce5d59c8ce7c(a)5g2000yqz.googlegroups.com>...
> You mean like in this demo where I extract the value of the image at
> line 10, column 30 for each of 114 frames in the Rhino demo movie?
> You'd just have to adapt it to extract the RGB values instead of
> converting it to gray and getting the gray value - a very simple
> modification.
>
> % Demo macro to extract frames and get frame means from an avi movie
> % and save individual frames to separate image files.
> clc;
> close all;
>
> % Open the rhino.avi demo movie that ships with MATLAB.
> movieFullFileName = 'C:\Program Files\MATLAB\R2010a\toolbox\images
> \imdemos\rhinos.avi';
> % Check to see that it exists.
> if ~exist(movieFullFileName, 'file')
> strErrorMessage = sprintf('File not found:\n%s\nYou can choose a new
> one, or cancel', movieFullFileName);
> response = questdlg(strErrorMessage, 'File not found', 'OK - choose a
> new movie.', 'Cancel', 'OK - choose a new movie.');
> if strcmpi(response, 'OK - choose a new movie.')
> [baseFileName, folderName, FilterIndex] = uigetfile('*.avi');
> if ~isequal(baseFileName, 0)
> movieFullFileName = fullfile(folderName, baseFileName);
> else
> return;
> end
> else
> return;
> end
> end
> try
> mov = aviread(movieFullFileName);
> % movie(mov);
> % Make a special output folder to hold all the separate movie frames
> % as their own image files.
> outputFolder = fullfile(cd, 'Rhino Movie');
> if ~exist(outputFolder, 'dir')
> mkdir(outputFolder);
> end
> % Determine how many frames there are.
> numberOfFrames = size(mov, 2);
> numberOfFramesWritten = 0;
> % Prepare a figure to show the images in the upper half of the
> screen.
> figure;
> screenSize = get(0, 'ScreenSize');
> newWindowPosition = [1 screenSize(4)/2 - 70 screenSize(3)
> screenSize(4)/2];
> set(gcf, 'Position', newWindowPosition); % Maximize figure.
> % set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
>
> % Loop through the movie, writing all frames out.
> % Each frame will be in a separate file with unique name.
> meanGrayLevels = zeros(numberOfFrames, 1);
> for frame = 1 : numberOfFrames
> % Extract the frame from the movie structure.
> thisFrame = mov(frame).cdata;
>
> % Display it
> subplot(1,2,1);
> image(thisFrame);
> drawnow; % Force it to refresh the window.
> axis square;
>
> % Convert from RGB to gray.
> grayImage = rgb2gray(thisFrame);
>
> % % Calculate the mean gray level.
> % meanGrayLevels(frame) = mean(grayImage(:));
> % Calculate the gray level at line 10, column 30.
> meanGrayLevels(frame) = grayImage(10,30);
>
> % Plot the mean gray levels.
> subplot(1,2,2);
> plot(meanGrayLevels);
> if frame == 1
> title('Gray Levels at line 10, column 30');
> xlabel('Frame Number');
> yLabel('Gray Level');
> end
>
> % Construct an output image file name.
> outputBaseFileName = sprintf('Frame %4.4d.png', frame);
> outputFullFileName = fullfile(outputFolder, outputBaseFileName);
> % Write the image array to the output file.
> % imwrite(thisFrame, outputFullFileName, 'png');
> % Update user with the progress. Display in the command window.
> progressIndication = sprintf('Processed frame %4d of %d.', frame,
> numberOfFrames);
> disp(progressIndication);
> % Increment frame count (should eventually = numberOfFrames
> % unless an error happens).
> numberOfFramesWritten = numberOfFramesWritten + 1;
> end
> catch ME
> % Some error happened if you get here.
> stError = lasterror;
> strErrorMessage = sprintf('Error extracting movie frames from:\n\n%s\n
> \nError: %s\n\n)', movieFullFileName, stError.message);
> msgboxw(strErrorMessage);
> end
>
> % Alert user that we're done.
> finishedMessage = sprintf('Done! It processed %d frames to folder
> \n"%s"', numberOfFramesWritten, outputFolder);
> disp(finishedMessage); % Write to command window.
> msgbox(finishedMessage); % Also pop up a message box.