MATLAB Answers

getting pixel value of the pre-specifed region of many image files

1 view (last 30 days)
Lee
Lee on 14 Mar 2014
Commented: Image Analyst on 15 Mar 2014
Hello all,
I’d like to obtain pixel values from a fixed region of hundreds of gray scale images. The location, shape (either rectangular or circle), and size of the region should be able to specified before running my m-file, without mouse button click. So, the pixel values, corresponding pixel locations, and file index at every single image are automatically saved in the workspace by just typing first and last index of the images in my m-file.
How can I do this?
Thanks in advance.

Answers (2)

Image Analyst
Image Analyst on 14 Mar 2014
See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F for code examples. Though it shows you how to loop through image files it doesn't save all the hundreds of images. I really question why you need to save all the masked images. Why is that needed? Why can't you just call a function, say AnalyzeSingleImage(), to analyze the image right there in the loop? What's the point of saving them all, just to run another loop again to analyze them all? Why not do it all in one loop?
  2 Comments
Image Analyst
Image Analyst on 15 Mar 2014
Lee, okay, just as I though. No reason at all to save the images in a cell array. Simply call a function to process the image as it gets read in, and then you can save the results but throw away the image (actually reuse the variable by reading the next image into it on the next iteration).
Let's say you return 4 measurements in an array called results. Then
allResults = zeros9numberOfImages, 4);
for k = 1 : numberOfImages
% Read in image using imread or whatever...
% Now process the image.
thisResults = AnalyzeSingleImage(fullFileName);
allResults(k,:) = thisResults;
end
% Now allResults has the results from all the images.
% Each image's results are in one row of allResults.
You write a function called AnalyzeSingleImage() that takes an image array or a filename string and analyzed it and sends the results out in an array called thisResults or something like that. Does that make sense? Because you really don't want to mess with cell arrays when you don't have to, and you don't have to.

Sign in to comment.


Benjamin Avants
Benjamin Avants on 14 Mar 2014
I think the best way is to make a logical mask first. You'll want a mask of the same dimension as your images where each element that is in your region of interest is true and all the rest are false. Then just run a loop that loads the images (or references them if they're already in memory) and use a command like:
mask = zeros(imgX,imgY,'uint8');
mask = logical(mask);
% Make the desired pixels 'true'
for i = 1:numOfImages
newImg{i} = oldImg{i}(mask);
end
This command assumes you're new and old images are in cell arrays because they're easy to work with without having to change variable names.

Community Treasure Hunt

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

Start Hunting!

Translated by