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

2 views (last 30 days)
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
Lee
Lee on 15 Mar 2014
Hello Image Analyst,
I have hundreds of image files. On each image file, there is one oval shaped brighter (high gray value) part. What I want to know is how much the brighter part of high gray value is shirincked or expanded as index of the image files vary.
To do that, firstly, I want to define two rectangles with respect to longitudinal and transverse axis of the oval. After then, I want to obtain gray values on each rectangle and add up the values into one-dimensional vector such that I could have same effect as I do a thick line scan with respect to longitudinal and transverse axis of the oval. The main purpose of this is to decrease signal-to-noise ratio which may bring about in case that I do just one line scan.
I do not want to define the two rectangles one by one, manually clicking mouse button, which is definitely time and effort consuming and may cause some precision problems related.
I do agree with the comments of Benjamin and you to make a cell array to load or save image files and use the loop. But, I have no idea after then…
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!