how to crop multiple files

hello guys , worked on triangular mask cropping of a DICOM image , here is the code -
clear all
I=dicomread('four.dcm');
I=uint16(I);
g=imshow(I,[0 600]);
axis on
[x,y]=ginput(3);
h = impoly(gca, [x,y]);
pos = wait(h);
BW = createMask(h,g);
BW=uint16(BW);
J=I.*BW;
figure,imshow(J,[0 600]);
I just want to perform this task on multiple DICOM images . used for loop but createMask is showing error about cell type data variables . what will be the approach ? please suggest me . Thank you

 Accepted Answer

for i = 1:length(fileList)
I=dicomread(fileList{i}); %fileList contains names of all dicom images
I=uint16(I);
g=imshow(I,[0 600]);
axis on
[x,y]=ginput(3);
h = impoly(gca, [x,y]);
pos = wait(h);
BW = createMask(h,g);
BW=uint16(BW);
J=I.*BW;
figure,imshow(J,[0 600]);
end

6 Comments

Thanks for the suggestion .
@Stalin Samuel: Note that you should not use i or j as loop variable names, as these are the names of the inbuilt imaginary unit.
Also note that consistent indentation makes code easier to follow. Please consider using the MATLAB Editor's default code indentation and formatting in order to make your cod more readable.
Hi ,
I am running the same code with a little change. But the output that Matlab shows, is completely black. I think my code is not correct. I am trying to create a fixed size rectangle shape and apply this for Dicom image since I want the ROI to be same size for all images. I will be thankful if you help me.
Question:
1) How can I crop a stack of Dicom images with the same size and position of mask (aim: I do not want to process the whole Dicom image for a specific task)?
2) What is the purpose of these two lines of code?
g=imshow(I,[0 600]);
and,
pos = wait(h);
Thank you in advance,
for p=1:k
filename{p} = strcat('C:\directory\',srcFiles(p).name); %filename contains names of all dicom images
I=dicomread(filename{p});
g=imshow(I,[0 600]);
axis on
h = imrect(gca, [300 300 500 500]);
[x,y]=size(h);
h = impoly(gca, [x,y]);
% pos = wait(h);
BW = createMask(h,g);
BW=uint16(BW);
J=I.*BW;
figure,imshow(J,[1 600]);
X(:,:,p) = J;
end
The purpose of
g=imshow(I,[0 600]);
is to display the image contained in I, with all values below 0 to be mapped to the first color in the colormap, and all values above 600 to be mapped to the last color in the colormap, and the values in-between to be mapped into the colormap linearly. For example if the colormap were 128 colors, then the intensity value 150 would map to (150/600)*128 = color #32.
The problem with using that line blindly is that not all DICOM images are in the range 0 to 600. CT images, for example, tend to range from -1024 to +3071 (Hounsfield units) . Some DICOM images have non-negative data up to about 6000. DICOM permits signed and unsigned values and does not restrict to "small numbers".
You do not need to display an image to crop it. You can just directly use
J = imcrop(I, [300 300 500 500]);
skipping most of the steps that you have in that code.
Sara Salimi
Sara Salimi on 29 Sep 2016
Edited: Sara Salimi on 29 Sep 2016
Dear Walter, thank you very much.
Question: Does cropping affect the processing and and the performance of process/calculation? I mean smoothing or derivatives?
Thanks again
Generally speaking, operating on smaller matrices is faster, but for reasonably sized arrays operations like smoothing a sobel filters are pretty quick. Cropping is usually used to remove irrelevant information -- if you know that something is not part of the interesting part of the image, it is confusing to region find on it (for example might be text labels.)

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!