MATLAB Answers


mesh, intensity image, 3D

Asked by Jean
on 29 Jun 2013
Hello, I am doing some image processing in which I have imported a video clip and extracted relevant frames. I want to take one of the images (which is in intensity values) and use mesh() or a similar function to create a 3D plot where x and y are the location of the pixels and the z axis is the intensity or pixel value. When I use the mesh() function, I just get a blank 3D plot and 'Warning: Matrix dimensions must agree, not rendering mesh.' What am I doing wrong? any help? Does the intensity spectrum need to be adjusted? How do I make the matrix dimensions 'agree'? Thanks very much for any help.


Please show your code.
%Create working directory and name workingDir=tempname; mkdir(workingDir); mkdir(workingDir, 'images'); vids1=VideoReader(''); %select frames, extract, convert to intensity for ii=1000:1005 img1=read(vids1,ii); hcsc = vision.ColorSpaceConverter; hcsc.Conversion = 'RGB to intensity'; I_I1 = step(hcsc, img1); I_I2=imcomplement(I_I1); %adjust intensity J1 = imadjust(I_I2,[],[],100);
imwrite(J1,fullfile(workingDir,'images',sprintf('img%d.jpg',ii))); end %sort into proper order imageNames = dir(fullfile(workingDir,'images','*.jpg')); imageNames = {}'; imageStrings = regexp([imageNames{:}],'(\d*)','match'); imageNumbers = str2double(imageStrings);[~,sortedIndices] = sort(imageNumbers); sortedImageNames = imageNames(sortedIndices); disp(sortedImageNames) %write video file outputVideo = VideoWriter('vidout.avi'); outputVideo.FrameRate = vids1.FrameRate; open(outputVideo); for iii = 1:length(sortedImageNames) img = imread(fullfile(workingDir,'images',sortedImageNames{iii})); writeVideo(outputVideo,J1); end
THis is the original script (above) that I use to generate the images. Then, I do something like this to try to get the spacial/intensity 3D plot with one of the image frames:
img = imread(fullfile(workingDir,'images',sortedImageNames{1})); imshow(img) mesh(double(img) which is a bit stringy and unhelpful if coming from an intensity grayscale image (I want the result in colormap jet) I can do surf(double(img) which yields a surface plot all in black, or meshgrid(img) which runs forever and freezes my computer. Any advice? Many thanks.

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 29 Jun 2013
 Accepted Answer

Are you constructing your x and y coordinates using meshgrid() ? If you are, use ndgrid() instead.
[x, y] = ndgrid(.....);
mesh(x, y, z)

  1 Comment

Hey guys, thank you, I did eventually get it; had to use mesh(double(...)). I will try this as well to evaluate options. One issue I am still having is the 3D spacial and intensity plot only shows up in colormap jet if the image is already mapped that way. Otherwise, I get a similar plot, but only black for all z levels (so a big black blob). I will post my code in answer to Walter. Thanks again to all!

Sign in to comment.