如何从目录中读取 .tif 样式图像

How to read .tif style images from a directory

我正在尝试 运行 以下代码来分析几个不同目录中的多个 .tif 图像。在 运行 编码时,给出的数字是空白的,后面是第 49 行的错误“必须为填充腐蚀提供 M”。我首先意识到我在代码中将 .tif 拼写为 .tiff,我更改了它。然后我决定通过创建一个 Tiff 对象并将图像路径包装在 tiff 对象中来读取一个 .tif 图像。控制台报错,说文件无法打开。

代码

%% Import Spheroid Directory
Path = 'C:\Users606\OneDrive\Documents\Spheroids For Brandon\Spheroids For Brandon-8WT';
if ~isfolder(Path)
    errorMsg = sprintf('Error: The following folder does not exist:\n%s',Path);
    uiwait(warndlg(errorMsg));
    return;
end
%% Iterate over each subfolder with .tiff images
Content = fullfile(Path, '*.tif'); % Read Experiment directory with .tiff images
subFold = dir(Content);
imageFiles = [];                    % Create empty list array 
for k = 1:length(subFold)
    F = fullfile(Path, subFold(k).name); % For k subfolders, output the full file name and open each subfolder
    fileID = fopen(F);
    imageFiles{end+1} = fopen(fileID,'%s\n'); % Append fileID to imageFiles list
    fclose(fileID);
end
disp(imageFiles(k))                           % Display the image files for analysis

%% Image preprocessing

%Divide image "obj" into its respective RGB intensities
red = imageFiles(:,:,1);
green = imageFiles(:,:,1);
blue = imageFiles(:,:,1);
figure(1)
subplot(2,2,1); imshow(imageFiles); title('Original Image')
subplot(2,2,2); imshow(imageFiles); title('Red Plane')
subplot(2,2,3); imshow(imageFiles); title('Green Plane');
subplot(2,2,4); imshow(imageFiles); title('Blue Plane');

%Threshold the blue plane
figure(2)
level = 0.37;
bw2 = imbinarize(blue, level);
subplot(2,2,1); imshow(bw2); title('Blue plane threshold');

%% Image preprocessing continued

fill = imfill(bw2,'holes');
subplot(2,2,2); imshow(fill); title('Holes filled')

%Remove any blobs on border of image
clear = imclearborder(fill);
subplot(2,2,3); imshow(clear); title('Blobs removed');

%Remove blobs smaller than 7 pixels across
se = strel('disk',7);
open = imopen(fill, se);
subplot(2,2,4); imshow(open); title('Removed small blobs');

%% Measure the number of pixels in Image
%numberOfPixels = numel(inputFile);
%[pixelCounts, grayLevels] = imhist(inputFile);
%numberofPixels = sum(pixelCounts);

%% Measure the distance of migration in micromolars

diameter = regionprops(open, 'MajorAxisLength');

%Show result
figure(3)
imshow(imageFiles)
d = imdistline; %Includes a line to physically measure the object

错误消息

>> RawData
Error using images.internal.morphop>ParseInputs
M must be provided for packed erosion.

Error in images.internal.morphop (line 23)
 unpacked_M,mex_method] = ParseInputs(varargin{:});

Error in imerode (line 87)
    B = images.internal.morphop(A,se,'erode',mfilename,varargin{:});

Error in imopen (line 73)
    outputImage = imdilate(imerode(inputImage,se,'ispacked',M),se,'ispacked',M);

Error in RawData (line 49)
open = imopen(fill, se);
 

我有一种预感,文件路径读取错误,但程序正在读取一些东西,否则它会抛出代码中指定的错误。我很感激我能在这件事上得到的任何帮助。

您的代码中有很多地方没有意义。

    fileID = fopen(F);
    imageFiles{end+1} = fopen(fileID,'%s\n'); % Append fileID to imageFiles list

这将打开文件,然后将其文件名放入 imageFiles{end+1}。这与 imageFiles{end+1} = F 相同,但更令人困惑。

稍后,您使用 imageFiles 就好像它是图像一样:

red = imageFiles(:,:,1);

但这不是图像,它是一个元胞数组,其中包含一组文件的名称(带路径)!

imshow(imageFiles) 将加载该数组中的图像,并将它们显示给您。但除此之外,您永远不会加载图像,因此您所做的所有计算都是在文件名称而不是像素数据上进行的。

您想使用imread加载像素数据。不要 fopen 文件,只 imread 它们。