Tiff Stack is only reading one image.

3 views (last 30 days)
Luke
Luke on 16 Sep 2014
Commented: Ashish Uthama on 16 Sep 2014
Hello,
I am trying to write a script that measures the contrast of each individual tiff in a stack of tiffs, and then proceeds to graph them on a plot. I am running into this issue where only the first image of my stack is being recognized when I run it through the length function, and this makes my for loop run from image 1 to image 1. Here is my script below:
clc; % Clear command window.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
filename = input('Enter image file name: ', 's');
%
infoI=imfinfo(filename);
mImage=InfoImage(1).Width;
nImage=infoI(1).Height;
NumberImages=length(infoI);
FinalImage=zeros(nImage,mImage,NumberImages,'uint16');
TifLink = Tiff(filename, 'r');
for k=1:NumberImages
TifLink.setDirectory(k);
imd(:,:,k)=TifLink.read();
%mean of intensity of each pixel
mean_pixels=mean(imd(:));
%measures rms contrast of images.
contrast = sqrt((1/(1600*1200))*sum((imd(:)-mean_pixels).^2));%rms
h= plot(NumberImages, contrast, '.');
set(h,'Markersize',10)
end
I have read other forums that had solutions for them, but none of the modifications work. If I could get someone to give me some input on why I am only measuring the contrast of the first image that would be great.
  2 Comments
Geoff Hayes
Geoff Hayes on 16 Sep 2014
Luke - have you stepped through the code to verify that only one image is being read? I tried the above with an example multi-image tiff file from imwrite and the code failed at the line
mImage=InfoImage(1).Width;
Should InfoImage be replaced with infoI? I changed that and did verify that each of the two images were read on each iteration of the loop.
I do wonder about your calculation of the contrast though. On the first iteration, when there is only the one image in imd, you will compute the mean_pixels using just that image. But on the second iteration, when the two images are stored in imd, mean_pixels will be calculated using both images. Is this intentional?
Also, because you don't use the hold on command, your second plot will overwrite the first. But then you use NumberImages for both plots rather than k. What is the intent here?
Ashish Uthama
Ashish Uthama on 16 Sep 2014
Have you ensured NumberImages is greater than 1?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!