Home > Mobile >  How to read .tif style images from a directory
How to read .tif style images from a directory

Time:05-20

I am trying to run the following code to analyze multiple .tif images in several different directories. Upon running the code, the figures that are given are blank, followed by the error on line 49 "M must be provided for packed erosion". I first realized I spelt .tif as .tiff in the code, which I changed. Then I decided that I would just read off one .tif image by creating a Tiff object and wrapping the image path in the tiff object. The console threw an error, saying that the file was unable to be opened.

Code

%% Import Spheroid Directory
Path = 'C:\Users\18606\OneDrive\Documents\Spheroids For Brandon\Spheroids For Brandon\1-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

Error Msg

>> 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);
 

I have a hunch that the file path is being read incorrectly, but the program is reading something, otherwise it would have thrown the error specified in the code. I appreciate any help I can get on this matter.

CodePudding user response:

There are many things in your code that don't make sense.

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

This opens the file, then puts its file name in imageFiles{end 1}. This is the same as just doing imageFiles{end 1} = F, but more confusing.

Later, you use imageFiles as if it was an image:

red = imageFiles(:,:,1);

But it's not an image, it is a cell array with the names (with path) of a set of files!

imshow(imageFiles) will load the images in that array, and show them to you. But other than that, you never load the images, so all computations you do, you are doing on the names of the files rather than the pixel data.

You want to use imread to load the pixel data. Don't fopen the files, just imread them.

  • Related