Images stored in a cell array are getting cut off

1 view (last 30 days)
Bhumi Davda
Bhumi Davda on 27 Nov 2021
Commented: DGM on 28 Nov 2021
HI!
Im runing this for loop and trying to save all my images in a cell array. But the size of the image getting stored in the cell array is becoming 1x27 from 1024x1344. My code is using the function processImage, which outside this FOR loop is giving same resolution image. Can someone tell me where am I making a mistake?
Thank you!
BFPimages = {};
RFPimages = {};
YFPimages = {};
for i=1:4
iTime = num2str(i);
BFPimages{i}=processImage(['/MATLAB Drive/BFP_Time' iTime '.TIF']);
RFPimages{i}=processImage(['/MATLAB Drive/RFP_Time' iTime '.TIF']);
YFPimages{i}=processImage(['/MATLAB Drive/YFP_Time' iTime '.TIF']);
end

Answers (1)

DGM
DGM on 27 Nov 2021
Edited: DGM on 27 Nov 2021
Whatever your processImage function is returning, it's not 1024x1344 raster data. I doubt it's getting cut off either. It's a 1x27 vector -- probably the same 1x27 character vector that is the filename.
The problem is in the processImage function. We can't really go any further unless you attach it. If the behavior is changing depending on the number of output arguments, there may be an issue with how varargout is being used.
  3 Comments
DGM
DGM on 28 Nov 2021
At no point is the file being read. You're operating on the filename+path. The reason that the second example produces a longer vector is because it's concatenating all the results together.
The basic part of the fix is to simply use imread at the start of the function. Now you have image data. That said, the function implements basic normalization. Depending on your images, you may simply be able to use existing normalizing tools to do this instead.
% for I images, this is identical to simple normalization
% but for RGB images, this operates channelwise as opposed to globally
fname = 'cameraman.tif';
A = processImage(fname);
B = mat2gray(imread(fname));
imshow(A)
immse(A,B)
ans = 9.7862e-33
function [Processed_Yeast_Image] = processImage(myImagePath)
doubleImage = im2double(imread(myImagePath));
minImage = min(min(doubleImage));
subtracted = doubleImage - minImage;
maxImage = max(max(subtracted));
divided = subtracted ./ maxImage;
Processed_Yeast_Image = divided;
end
This works fine and matches the behavior of mat2gray() for single-channel images. Because the calculations of minImage and maxImage assume that the image is 2D, they will be nonscalar (1x1x3) when the image is RGB. This means that if the image is RGB, each channel will be normalized independently. This isn't necessarily wrong, but it differs from the behavior of mat2gray().
If you need to process both I/RGB images, you should decide whether you want to normalize the whole image or each channel independently. If you want to normalize globally regardless of image type, then mat2gray() should suffice.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!