Create an image stack (3-D matrix)

조회 수: 122 (최근 30일)
Jack
Jack 2013년 6월 4일
편집: DGM 2023년 2월 13일
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
joseph robinson
joseph robinson 2017년 4월 19일
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!

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

채택된 답변

Kye Taylor
Kye Taylor 2013년 6월 4일
편집: Kye Taylor 2013년 6월 4일
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});

추가 답변 (2개)

hf fh
hf fh 2018년 5월 21일
편집: DGM 2023년 2월 12일
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
Walter Roberson
Walter Roberson 2021년 8월 16일
편집: Walter Roberson 2021년 8월 16일
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 2022년 2월 18일
Use Fiji or imagej it is free software. After spending many days I could not findout correct stacking solution in matlab.

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


Image Analyst
Image Analyst 2013년 6월 4일
편집: Image Analyst 2013년 6월 4일
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
Jay Kandukuri
Jay Kandukuri 2021년 7월 30일
I am trying to convert 80 dcm images into a 3d stack array can you tell me how i can do that.
Image Analyst
Image Analyst 2021년 7월 31일
@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

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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by