How to build 3d plot of grayscale image where x,y - pixel coordinates, z - pixel intensity?


I tried ListPlot3D[ImageData[image]], but ImageData gives just intensities. I suppose a possible solution would be transorming image to (x-coord, y-coord, intensity) and passing this as an agrument to ListPlot3D.

pic = Import @ "http://i.stack.imgur.com/RJs60.png"

ListPlot3D can work with an array of values too. The problem is that you have quadruplets there, RGB and alpha channel. Convert it:

  Reverse @ ImageData @ RemoveAlphaChannel @ ColorConvert[pic, "Grayscale"],
  AxesLabel -> {"x", "y", "z"}

enter image description here


Another nice 3D-ish plotting command is ReliefPlot. Modelling the preprocessing after Kuba's answer, this would be:

pic = Import@"http://i.stack.imgur.com/RJs60.png";
imdData = Reverse@ImageData@ RemoveAlphaChannel@ColorConvert[pic, "Grayscale"];

enter image description here


For smaller images I like the ListPlot3D with the option InterpolationOrder -> 0. Every pixel is then a nice flat tile which you can give a color with an appropriate ColorFunction.

t = Table[Exp[-(Sqrt[x^2 + y^2]/5)] Cos[Sqrt[x^2 + y^2]/2], {x, -10, 10, 
0.5}, {y, -10, 10, 0.5}];

i = Image[t] // ImageAdjust

lo = -0.2; hi = 1.5; ListPlot3D[i // ImageData, InterpolationOrder -> 0,
BoxRatios -> {1, 1, 1.2}, PlotRange -> {lo, hi}, 
ColorFunction -> (Hue[(#3 - lo)/(hi - lo)] &), 
ColorFunctionScaling -> False, AxesLabel -> {"x", "y", "z"}]

This results in:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.