필터 지우기
필터 지우기

DICOM Image Resize Error

조회 수: 6 (최근 30일)
ssk
ssk 2019년 2월 16일
댓글: Image Analyst 2019년 9월 28일
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!

채택된 답변

Walter Roberson
Walter Roberson 2019년 2월 16일
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
geetha reddy
geetha reddy 2019년 9월 28일
can you please tell how you solved that resizing problem
Image Analyst
Image Analyst 2019년 9월 28일
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.

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

추가 답변 (3개)

Image Analyst
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);
  댓글 수: 1
ssk
ssk 2019년 2월 17일
편집: ssk 2019년 2월 17일
Thanks for your reply and sharing such a nice code! I already upvoted for you!

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


Image Analyst
Image Analyst 2019년 2월 16일
편집: Image Analyst 2019년 2월 16일
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
ssk 2019년 2월 16일
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 2019년 2월 16일
You cannot resize an imageDatastore -- I already told you that, and discussed readall() and cellfun() with you.

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


Anjali Acharya
Anjali Acharya 2019년 3월 2일
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
Image Analyst 2019년 3월 2일
You probably have an old version of MATLAB. Use isdir() instead.
Anjali Acharya
Anjali Acharya 2019년 3월 3일
@Image Analyst
Thank you for your quick feedback.
I am using matlab 2017. isdir() works fine.

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

카테고리

Help CenterFile Exchange에서 Segmentation and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by