Create an image stack (3-D matrix)

Hello. I'm importing a series of images from my working directory using the following code:
for i=139:141
% {} = cell array
images{i} = imread(sprintf('IMG_%04d.JPG',i));
I{i} = rgb2gray(images{i});
end
I have a large number of images, but here I'm importing just a sample. I'm converting each color image to grayscale. I must also create an image stack, a 3D matrix of grayscale images. I've been browsing the web for an answer, but I'm having difficulty understanding the process. size(I) yields '1 141', and I'm wondering how I can create a 3-D matrix from this. Your help would be greatly appreciated. Thank you!

1 commentaire

Adding to the feedback of others, your code would be more efficient without looping, e.g.,
idx = 139:141;
# image file paths
ipaths =cellfun(@(x) sprintf('IMG_%04d.JPG',x),idx,'uni',false);
# read images into cell array
I = cellfun(@imread, ipaths,'uni',false);
# Convert to gray-scale
Igray = cellfun(@rgb2gray,I,'uni',false);
# Concatenate 3 gray-scale images into single 3D matrix
myImage = cat(3,Igray{:});
Hope this helps!

Connectez-vous pour commenter.

 Réponse acceptée

Kye Taylor
Kye Taylor le 4 Juin 2013
Modifié(e) : Kye Taylor le 4 Juin 2013

0 votes

Execute this command after your code above to get the three gray-scale images you create into one 3D matrix of grayscale images;
myImage = cat(3,I{139:141});

Plus de réponses (2)

hf fh
hf fh le 21 Mai 2018
Modifié(e) : DGM le 12 Fév 2023
Please help me .. I searched a lot for convert 2Dimages into 3D but I still have the same problem !!! ''I can not get the images in volume in three dimensions" I have a search in 20 image dimensions [773x896]pixel, I want to convert it to 3D-three dimensions in volume form to study the depth, I following steps this:
  1. Must be converted to a size of [300 * 300] pixels
  2. Converted to stack
  3. Converted to format==> 3D
like :
folder = '/Users/mac/Desktop/fiel2/data/';
for i=1:20
images{i} = imread( fullfile(folder, sprintf('%d.tif',i) ));
Q{i}=imresize(images{i} ,[300 , 300]);
end
I = cat(3, Q{i});
imshow(Q{i});
view(3);
Thank you for help me ...

17 commentaires

hf fh
hf fh le 21 Mai 2018
Modifié(e) : DGM le 13 Fév 2023
Hi
I have a question
I used the same method but I want to extract it in size format volume like figure 3D
how it would be???
folder = '/Users/mac/Desktop/fiel2/data';
D = '/Users/mac/Desktop/fiel2/data';
for i=1:8
% {} = cell array
images{i} = imread(sprintf('%d.tif',i));
I{i} = rgb2gray(images{i});
I = cat(3, I, images{i}); % Tack on slice i into a new cell.
end
Walter Roberson
Walter Roberson le 21 Mai 2018
Déplacé(e) : DGM le 12 Fév 2023
Sigh, we wish you would have taken the time to read about formatting code while you were posting in the tutorial about formatting code...
Walter Roberson
Walter Roberson le 21 Mai 2018
Déplacé(e) : DGM le 12 Fév 2023
folder = '/Users/mac/Desktop/fiel2/data';
for i=1:8
images{i} = imread( fullfile(folder, sprintf('%d.tif',i) ));
I{i} = rgb2gray(images{i});
end
I = cat(3, I{:});
Image Analyst
Image Analyst le 21 Mai 2018
Why do you want the images or Q cell arrays anyway? Chances are you don't. I never do that. I never create a cell array of a bunch of images and I think you most likely don't need to either. Why do you think you need to???
hf fh
hf fh le 21 Mai 2018
Déplacé(e) : DGM le 12 Fév 2023
Thank you for answering me... I'm a beginner in using MATLAB and I just want to convert 20 images from 2D-two to 3D-three dimensions only. The aim is to convert from 20 images to an only volume 3D Look for of a method to apply the images.
I following steps like this:
First: Must be converted to a size of [300 * 300] pixels
Second: Converted to stack==>2D[300*300*20]
Third: Converted to format==> 3D volume
please help me What do I think about is true ??? and if you have a proposal please help me ..
PRAKASH JOSHI
PRAKASH JOSHI le 23 Mar 2019
Déplacé(e) : DGM le 12 Fév 2023
Error using rgb2gray>parse_inputs (line 80)
MAP must be a m x 3 array.
Error in rgb2gray (line 52)
isRGB = parse_inputs(X);
it is showing this error
Please help me out
Walter Roberson
Walter Roberson le 23 Mar 2019
Déplacé(e) : DGM le 12 Fév 2023
for i = 1 : 8
images{i} = imread( fullfile(folder, sprintf('%d.tif', i) ) );
if ndims(images{i}) > 2
I{i} = rgb2gray(images{i});
else
I{i} = images{i};
end
end
Adithya Vasudevan
Adithya Vasudevan le 14 Avr 2020
Déplacé(e) : DGM le 12 Fév 2023
folder = 'C:\Users\Suraj\Desktop\images\Results';
for i = 109 : 433
try
images{i} = imread( fullfile(folder, sprintf('000%d.jpg', i) ) );
catch
continue;
end
if ndims(images{i}) > 2
I{i} = rgb2gray(images{i});
else
I{i} = images{i};
end
end
I = cat(3, I{:});
imshow(I{i});
view(3);
Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in test_3D (line 11)
I{i} = images{i};
Image Analyst
Image Analyst le 15 Avr 2020
Déplacé(e) : DGM le 12 Fév 2023
This works for me:
folder = pwd; % 'C:\Users\Suraj\Desktop\images\Results';
% for k = 109 : 433
% fullFileName = fullfile(folder, sprintf('000%d.jpg', k));
for k = 1 : 10
fullFileName = fullfile(folder, sprintf('%d.jpg', k));
if ~isfile(fullFileName)
fprintf('File #%d not found : %s\n', k, fullFileName);
continue;
end
fprintf('Storing image file #%d : %s\n', k, fullFileName);
% Store the original image, regardless of whether it's RGB or gray scale.
originalImages{k} = imread(fullFileName);
% Store the gray scale version of the image. Convert (if necessary) from RGB to gray scale.
if ndims(originalImages{k}) > 2
grayScaleImages{k} = rgb2gray(originalImages{k});
else
grayScaleImages{k} = originalImages{k};
end
end
Change the comments to have it use your images in your folder, and in the range 109 to 433. Better yet, see if you can just use dir() to get a list of only those images that definitely exist.
jaydip sawant
jaydip sawant le 15 Août 2021
hello i have 12 images in 12 direction like 30,60,... 360
i want to make a 3D volume using these images can anyone please help me in this.
@jaydip sawant use cat()
image3d = cat(3, image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, image11, image12);
jaydip sawant
jaydip sawant le 16 Août 2021
@Image Analyst hello thank you for help, when i tried cat function then this error is showing: -
"Error using images.internal.imageDisplayValidateParams>validateCData (line 118)
Multi-plane image inputs must be RGB images of size MxNx3.
Error in images.internal.imageDisplayValidateParams (line 30)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 79)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 253)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in Untitle (line 12)"
It sounds to me as if you have 12 color images. If so then get https://www.mathworks.com/matlabcentral/fileexchange/22940-vol3d-v2 and then
image3d = cat(4, image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, image11, image12);
vol3d('CData', image3d)
ketan kyung hee university
ketan kyung hee university le 16 Août 2021
I have gray scale 12 images
jaydip sawant
jaydip sawant le 16 Août 2021
@Walter Roberson Thank you for reply , I take X-ray images from diffrent angle like 30,60,....,360. I want place these images in it's angle posission like 30 degree image will place at 30 degree and 60 will place at 60 and so on. Then I can make a 3D volume of these 12 images.
I tried cat function but it does not work well it showing error.
my image pixel is 256 height and 248 width. I want make 256X248X12. last 12 is a number of image.
can you please help me in this problem.
Walter Roberson
Walter Roberson le 16 Août 2021
Modifié(e) : Walter Roberson le 16 Août 2021
For your situation, the cat(3) that Image Analyst showed is correct.
Your difficulty is that you cannot use imshow() to display volumes. To display volumes, you need to use one of the following:
  • montage()
  • implay()
  • volumeViewer()
  • vol3d from the FIle Exchange
  • videofig from the File Exchange
Tilkesh
Tilkesh le 18 Fév 2022
Use Fiji or imagej it is free software. After spending many days I could not findout correct stacking solution in matlab.

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 4 Juin 2013
Modifié(e) : Image Analyst le 4 Juin 2013

0 votes

The (badly-named) variable I is a one dimensional array of cells. Inside each cell it a 2D array. So, in effect, this is a 3D array though you can't reference it like I(row, column, slice). If that is what you need to do then you need to do
I = []; % Before the loop
Then in the loop:
I = cat(3, I, images{i}); % Tack on slice i into a new cell.
In the meantime, please read the FAQ on cells to get a better idea of how they work: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

6 commentaires

Just do something like
folder = '/Users/mac/Desktop/fiel2/data/';
image3d = zeros(300, 300, 20, 'uint8');
for k = 1 : 20
% Read one image.
thisImage = imread( fullfile(folder, sprintf('%d.tif', k) ));
% Store this image in the k'th slice of the 3-D image array.
image3d(:, :, k) =imresize(thisImage, [300, 300]);
end
hf fh
hf fh le 22 Mai 2018
The same method was used and the result was?
Unable to perform assignment because the size of the left side is 300-by-300 and the size of the right side is 300-by-300-by-3.
Error in d (line 7) image3d(:, :, k) =imresize(thisImage, [300, 300]);
image4d(:, :, :, k) = imresize(thisImage, [300, 300])
Image Analyst
Image Analyst le 22 Mai 2018
I'd assumed that you had gray scale images. They seem to be color and so creating a 4-D image like Walter showed should work. Or else convert the images to gray scale before using cat()
Jay Kandukuri
Jay Kandukuri le 30 Juil 2021
I am trying to convert 80 dcm images into a 3d stack array can you tell me how i can do that.
@Jay Kandukuri, allocate an array of 80 slices, then loop and read in the files and load them in to the array you just allocated.
dcm3d = zeros(rows, columns, 80, 'uint8');
% Then in a loop, load them in
for slice = 1 : 80
thisImage = dcmread(filename); % filename must change on every iteration obviously.
dcm3d(:, :, slice) = thisImage;
end

Connectez-vous pour commenter.

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by