Images stored in a cell array are getting cut off

조회 수: 2 (최근 30일)
Bhumi Davda
Bhumi Davda 2021년 11월 27일
댓글: DGM 2021년 11월 28일
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

답변 (1개)

DGM
DGM 2021년 11월 27일
편집: DGM 2021년 11월 27일
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
Bhumi Davda
Bhumi Davda 2021년 11월 28일
Also, I tried running the code in a different way, but the images are getting cut off again. Now every image is getting stored in the cell array as 1x176 double array, and those 176 values are first 176 values of the 1024x1344 array I'm getting after processing the image outside the loop like previous problem
filenames = dir("/MATLAB Drive/images");
imagenames = {filenames(3:end).name};
processedImages = [];
for i=1:12
processedImages{i} = processImage(['/MATLAB Drive/images' imagenames{1:12}]);
end
DGM
DGM 2021년 11월 28일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by