MATLAB Answers

DICOM Image Resize Error

57 views (last 30 days)
ssk
ssk on 16 Feb 2019
Commented: Image Analyst on 28 Sep 2019
Hi,there. I'm new to matlab and programming.
Since resizing is needed for Alexnet and Googlenet, I`m now working on resizing Dicom File from 256*256*1 to 227*227*3.
Here is my directory:
main
-- a
-- image.dcm(10 dicom file)
 -- b
  -- image.dcm(10 dicom file)
 -- c
  -- image.dcm(10 dicom file)
-- d
  -- image.dcm(10 dicom file).
Here is my code:
%path = current directory
currentdirectory = pwd;
%% Create an ImageDatastore to help you manage the data.currentdirectory = pwd;
categories = {'a', 'b', 'c','d'};
%Because ImageDatastore operates on image file locations,
imds = imageDatastore(fullfile(currentdirectory, categories),'IncludeSubfolders',true,'FileExtensions','.dcm','LabelSource', 'foldernames','ReadFcn',@dicomread);
%resize
imdsResized = imresize3(imds, [227 227 3]);
However, it throws following error:
Error using imresize3
Expected input number 1, V, to be one of these types:
single, double, int8, int16, int32, uint8, uint16, uint32
Instead its type was matlab.io.datastore.ImageDatastore.
Anyone have answer about this situation, pls let me know.
Thanks in advance!

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Feb 2019
It is not possible to resize an entire imageDatastore. You would need to loop doing readimage() and resize them one by one, or else use readall() to get back a cell array that you could cellfun() a resize operation on.

  26 Comments

ssk
ssk on 17 Feb 2019
Sorry to reply again. I solved this problem and finally it worked well!
geetha reddy
geetha reddy on 28 Sep 2019
can you please tell how you solved that resizing problem
Image Analyst
Image Analyst on 28 Sep 2019
Walter said he had to do a loop after getting the datastore. This is exactly what I did in my code below so I imagine he used that or something very similar of his own making. Anyway geetha, it should work for you. It's fairly robust. Since you're asking, I'm assuming you tried the code and it didn't work for you after your modifications. So post your code and I'll fix it.

Sign in to comment.

More Answers (3)

Image Analyst
Image Analyst on 17 Feb 2019
Edited: Image Analyst on 17 Feb 2019
Try this utility I wrote to create 227x227 images for input to an AlexNet deep learning network from a folder of aribtrary RGB, gray scale, or binary (logical) images. If that's what you want/need, please accept or vote for my Answer. For dicom images use dicomread() instead of imread().
% Utility to convert arbitrary RGB or gray scale images into RGB images for AlexNet deep learning network.
% Input images are in the current folder AND subfolders within that.
% All the 227x227 output images are created in a subfolder "For AlexNet" of the current folder (not subfolders of the subfolders where the images live).
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
% Specify the folder where the files live.
myFolder = pwd; % Current folder. Or change to whatever you want.
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder AND subfolders with the desired file name pattern.
imds = imageDatastore(pwd, 'FileExtensions','.png')
% Get a list of all filenames.
fileNamesImds = imds.Files;
% Now make an output folder. Create it if it does not exist already.
outputFolder = fullfile(myFolder, 'For AlexNet')
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
numberOfImages = length(fileNamesImds)
for k = 1 : numberOfImages
% Get the input file name.
inputFullFileName = fileNamesImds{k};
fprintf(1, 'Now reading input file "%s" (#%d of %d)\n', inputFullFileName, k, numberOfImages);
% Create the output file name.
[~, baseFileNameNoExt, ext] = fileparts(inputFullFileName);
outputBaseFileName = sprintf('AlexNet %s.png', baseFileNameNoExt);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
% Read in the input image.
inputImage = imread(inputFullFileName);
imshow(inputImage, []); % Display image.
title(baseFileNameNoExt, 'FontSize', fontSize, 'Interpreter', 'none');
drawnow; % Force display to update immediately.
% If it's logical (binary), convert to uint8, or else imwrite() will throw an error.
if isa(inputImage, 'logical')
inputImage = im2uint8(inputImage);
end
% Create a 227 x 227 RGB true color image.
[rows, columns, numberOfColorChannels] = size(inputImage);
fprintf(' Creating 227 by 227 output file "%s" from the %d by %d input image...\n', outputBaseFileName, rows, columns);
% Ref: https://www.learnopencv.com/number-of-parameters-and-tensor-sizes-in-convolutional-neural-network/
% "Color images of size 227x227x3. The AlexNet paper mentions the input size of 224×224 but that is a typo in the paper."
if numberOfColorChannels == 1
% It's gray scale. Convert to RGB by concatenating gray scale images along the third dimension.
inputImage = cat(3, inputImage, inputImage, inputImage);
end
outputImage = imresize(inputImage, [227, 227]);
% Save that array to the PNG output file
imwrite(outputImage, outputFullFileName);
end
message = sprintf('\nDone creating %d images in folder:\n%s.', numberOfImages, outputFolder);
fprintf('%s\n', message);
uiwait(helpdlg(message));
% Open the folder (Windows ONLY). Comment out for Mac.
winopen(outputFolder);

  1 Comment

ssk
ssk on 17 Feb 2019
Thanks for your reply and sharing such a nice code! I already upvoted for you!

Sign in to comment.


Image Analyst
Image Analyst on 16 Feb 2019
Edited: Image Analyst on 16 Feb 2019
Don't resize the third dimension. Don't try to do it all in one shot with imresize. Use imresize() to resize the lateral dimensions first to get a 227x227 gray scale image. Then use cat(3, grayscaleImage, grayscaleImage, grayscaleImage) to convert that image to RGB for AlexNet. At least that's how I'd do it.

  3 Comments

ssk
ssk on 16 Feb 2019
Dear, Image Analyst
Thanks for your advice.
Since I'm very new to programming, would you give me more hint to imprement this method or show the exact source code?
Thanks for you in advance!
ssk
ssk on 16 Feb 2019
I wrrote my code as follows, however it still throws error..
Although I changed imds into img, it doesn't work well..
Error using imresize
Expected input number 1, A, to be one of these types:
single, double, int8, int16, int32, uint8, uint16, uint32, logical
Instead its type was matlab.io.datastore.ImageDatastore.
mdsResized = imresize(imds, [227 227]);
Walter Roberson
Walter Roberson on 16 Feb 2019
You cannot resize an imageDatastore -- I already told you that, and discussed readall() and cellfun() with you.

Sign in to comment.


Anjali Acharya
Anjali Acharya on 2 Mar 2019
Hello @Image Analyst
I have set my current directory as follows:
myFolder = 'D:\matlab\alexnet\myImages\'; % Current folder.
I have RGB images with different size which i wasn to resize to 227 for alexnet.
When I run the code I get
Undefined function or variable 'isfolder'. (line 16)
Isnt myFolder supposed to be myImages in my case?
Your response would be great help.

  2 Comments

Image Analyst
Image Analyst on 2 Mar 2019
You probably have an old version of MATLAB. Use isdir() instead.
Anjali Acharya
Anjali Acharya on 3 Mar 2019
@Image Analyst
Thank you for your quick feedback.
I am using matlab 2017. isdir() works fine.

Sign in to comment.

Categories


Translated by