How to convert 2D to 3D?

Hi, I have some pictures, some of them are 2D and some are 3D. I need to convert 2D to 3D if I choose to open 2D picture( because I use histeq and imhist) I don´t know how to do write a command for converting to 3D if it is needed and not if is choosen 3D picture.
Thanks a lot
jane
p.s: sorry for my english

1 Comment

histeq() (a pretty much useless function by the way) and imhist() require 2D (grayscale) images, not 3D (color) images, so why do you want to convert to 3D/color if you say you're going to use histeq() and imhist()???

Sign in to comment.

 Accepted Answer

if ndim(TheArray) == 2
TheArray = TheArray(:,:,[1 1 1]);
end

13 Comments

janepear
janepear on 4 Apr 2013
Edited: janepear on 4 Apr 2013
I´m not sure it is correct now, but it doesn´t work so I do something wrong. I have
[file path]=uigetfile({'*.jpg';'*.bmp';'*.jpeg';'*.png'}, 'Načti
obrázek');
image=[path file];
handles.file=image;
if ndim(image) == 2
image = image(:,:,[1 1 1]);
end
handles.imga=im2double(rgb2gray(imread(image)));
and error is : Undefined function 'ndim' for input arguments of type 'char'.
Error in analyza2>pushbuttonload_Callback (line 167) if ndim(image) == 2
I should have suggested ndims() instead of ndim().
However... why are you needing to convert the 2D images to 3D? You would then just use rgb2gray() to convert back down to 2D.
Do not name a variable "image": doing that interferes with using the important image() function.
You should use fullfile() instead of concatenating the path and file.
filename = fullfile(path, file);
handles.file = filename;
imga = imread(filename);
if ndmins(imga) == 3
imga = rgb2gray(imga);
end
imga = im2double(imga);
handles.imga = imga;
Thanks a lot...it works great!. Please may I have a one more question? Why I should not use: [file path]=uigetfile({'*.jpg';'*.bmp';'*.jpeg';'*.png'}, 'Načti obrázek'); image=[path file]; ?
The "path" returned by uigetfile() might not include a directory delimiter at the end of it.
Now can you answer our question about why you want 3D when imhist and histeq cannot accept 3D images?
Ok :-) when I have a all pictures in 3D, I can use rgb2gray and it works for histeq and imhist, but I use colormaps too and they don´t work on 2D (I think). So Ï thought that it will be easier to have all in 3D in one variable and if i need to use histeq or imhist I will transform it.
Sure im beginner and It might by wrong way...
colormaps only work on 2D.
Ok so this is weird because it always wrote error
Error using rgb2gray>parse_inputs (line 81) MAP must be a m x 3 array.
If you have a colormapped image that you want to convert to grayscale, use http://www.mathworks.com/help/images/ref/ind2gray.html
Image Analyst
Image Analyst on 4 Apr 2013
Edited: Image Analyst on 4 Apr 2013
You are confused, or I am. If you have grayscale (2D) images, you do not need to make them color (3D) and then use rgb2gray() or some other way to convert them back to grayscale (2D), which is how they started. You're just doing a round trip grayscale->color->grayscale for no reason. You've given us no compelling need why you would need to do that. There's no reason why you can use imhist() and (the useless) histeq() directly on your original grayscale images.
Tell me, in a larger sense or context, what you want to do, and we'll show you how to do it. For example you want to read in all the images in a folder, no matter whether color or grayscale, display their histograms, increase their contrast, and write out the contrast-increased images to a new folder, or something like that. Please give me the big picture (no pun intended) of what this project is supposed to accomplish.
janepear
janepear on 4 Apr 2013
Edited: janepear on 4 Apr 2013
Ok,I´m sure that I´m the confused here :-). So i want to load picture ( 2 or 3D array) and then you can do negativ,contrast,brightness, gamma , equalization or choose in popmenu colormap.
This is not the larger picture. What does this code do? Why are you trying to somehow do something with a bone colormap? What do you want as a final image, handles.cm? A color image, an indexed image, or a grayscale image? It looks like a grayscale image so you only need to do that if ndims(handles.imga) == 3. But even if you do it only for color images, I'm not sure why a bone colormap is involved when you end up as grayscale.
janepear
janepear on 4 Apr 2013
Edited: janepear on 4 Apr 2013
1.)I want to show what you can do with utrasound picture...I use colormaps to show that sometimes is better to use copper or summer or another coloration of image

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!