DICOM Image Resize Error
显示 更早的评论
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!
采纳的回答
更多回答(3 个)
Image Analyst
2019-2-17
编辑:Image Analyst
2019-2-17
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);
Image Analyst
2019-2-16
编辑:Image Analyst
2019-2-16
1 个投票
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 个评论
ssk
2019-2-16
ssk
2019-2-16
Walter Roberson
2019-2-16
You cannot resize an imageDatastore -- I already told you that, and discussed readall() and cellfun() with you.
Anjali Acharya
2019-3-2
0 个投票
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 个评论
Image Analyst
2019-3-2
You probably have an old version of MATLAB. Use isdir() instead.
Anjali Acharya
2019-3-3
@Image Analyst
Thank you for your quick feedback.
I am using matlab 2017. isdir() works fine.
类别
在 帮助中心 和 File Exchange 中查找有关 Deep Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!