1
votes

I'm trying to stack 2D images to get 3D just like How can I plot several 2D image in a stack style in Matlab?

my original code had some errors and someone suggested to go with the below code

M = zeros(25, 50, 8);
for k = 1:8
    img = imread(sprintf('%d-0000.jpg', k + 30));
    img = imresize(img, [25 50]);
    img = im2double(rgb2gray(img));  % Convert to double format
    M(:, :, k) = img;
end
hf2 = figure ;
hs = slice(M,[],[],1:8) ;
shading interp
set(hs,'FaceAlpha',0.8);

this is the expected result How can I plot several 2D image in a stack style in Matlab?

this is the error I get

Error using rgb2gray>parse_inputs (line 81)
MAP must be a m x 3 array.

Error in rgb2gray (line 35)
X = parse_inputs(varargin{:});

Error in stack2 (line 9)
img = im2double(rgb2gray(img)); % Convert to double format

enter image description here

1
Please don’t post the same question twice. Instead, edit your first question to properly explain your problem. A well-poses question will get answered eventually. - Cris Luengo
I said the above because it’s exactly the same screen shot you had on the other question. Please copy-paste the error message, don’t use images of text, they’re not searchable and not everyone can read from an image. Also see minimal reproducible example. I can’t reproduce your problem because I don’t have your data. That means I can only guess at what the problem might be, which is not conductive to writing an answer. This is why you only had comments to your other question. - Cris Luengo
To your other question you had gotten some really good suggestions from gnovice: “The images you load from file have to be 3-D RGB images for your code to work. Based on the screenshot you added, your third image 33-0000.jpg is likely an indexed color image, meaning you have to use ind2gray to convert it.” and “Because you have to load the image and map, and pass both to ind2gray.“ (which came with proper formatting and links to relevant examples, I’m too lazy to copy these, go look at those links!!!) - Cris Luengo
It is literally the first example in the documentation for ind2gray. It shows you how to get two values out of imread and put them into ind2gray. mathworks.com/help/images/ref/ind2gray.html#d117e180249 - Cris Luengo

1 Answers

0
votes

Your code as it's written is designed to work with 3-D RGB images. However, the screenshot of your workspace suggests not all of your images fit that criteria. When k is 3, img is a 2-D matrix, meaning that the image in file "33-0000.jpg" is either already a grayscale image or an indexed image for which you didn't load the associated map.

To solve this, you will need to add some additional logic to your loop when loading your image so that you can identify what image type it is and how to properly convert it. Specifically, you need to check the number of dimensions of the image data and whether or not imread returns an associated colormap. Then you can apply rgb2gray or ind2gray as needed. For example:

for k = 1:8
  [img, cmap] = imread(sprintf('%d-0000.jpg', k + 30));
  if ~isempty(cmap)         % There is a colormap, so it's indexed
    img = ind2gray(img, cmap);
  elseif (ndims(img) == 3)  % 3 dimensions, so it's RGB
    img = rgb2gray(img);
  end
  M(:, :, k) = imresize(im2double(img), [25 50]);  % Convert to double and resize
end