I met such an image in a paper:

enter image description here

It represents an in-plane distribution of a light intensity, J=J(x,y). It is assumed that the white color intensity shows the light intensity. I would like to get this image as a list {..., {x,y,J},...} and/or as the interpolation function, so that I can play with this intensity one step further.

Any idea of how to transform it?

Apologies that I give no own code: I have no idea of how to approach this task.

  • 1
    $\begingroup$ Does applying ImageData help? This would give you the J values. x and y values have to be computed from pixel positions... $\endgroup$ May 8, 2018 at 9:34
  • $\begingroup$ Concerning interpolation, you can use ImageValue[image,pos] where pos is {x_Real,y_Real} (or a list of such pairs). It interpolates between pixels. Several interpolation methods are available. $\endgroup$
    – andre314
    May 8, 2018 at 10:17

2 Answers 2

img = Image[Import["a.jpg"]];
J = Flatten[ImageData[img][[All, All, 1]], 1];
pts = Flatten[Outer[List,Evaluate[Sequence @@ Range /@ Reverse@ImageDimensions[img]]], 1];
data = Join[pts, Partition[J, 1], 2];

f = Interpolation[data, InterpolationOrder -> 1];
Plot3D[f[x, y], {x, f["Domain"][[1, 1]], f["Domain"][[1, 2]]}, {y, 
  f["Domain"][[2, 1]], f["Domain"][[2, 2]]},
 PlotPoints -> ImageDimensions[img] + 1]

enter image description here

enter image description here

There are some artefacts caused by the coordinate axes in the original image, so some image-based preprocess might be in order. A Gaussian filter can also get you rid of much of the noise within the image. For example,

J = Flatten[ImageData[GaussianFilter[img, 8]][[All, All, 1]], 1];
f = Interpolation[Join[pts, Partition[J, 1], 2], InterpolationOrder -> 3];

would give you this:

enter image description here

This is actually a rather strong smoothing. You can easily see that the plateau in the original image gets quite much eroded. So there is some price you have to pay for the smoothing.

  • $\begingroup$ Thank you, this solves my question. I would be also grateful if you could kindly show, how do you propose to apply the filter to smooth the data. $\endgroup$ May 9, 2018 at 7:37
  • $\begingroup$ You're welcome. I added some sentences about that. However, there are probably better ways to get rid of the artefacts caused by the coordinates axes... $\endgroup$ May 9, 2018 at 7:55
img = Import["https://i.stack.imgur.com/94FiP.jpg"];

ImageMeasurements[img, "ColorSpace"]
(* RGB *)

data = ImageData@ColorConvert[img, "Grayscale"];

ListDensityPlot[Reverse@data, AspectRatio -> 1/2]

enter image description here

We do Reverse@data because there are different coordinate systems: http://reference.wolfram.com/language/tutorial/ImageProcessing.html#1708646374

  • 1
    $\begingroup$ +1 for ColorConvert. $\endgroup$ May 8, 2018 at 9:58
  • $\begingroup$ @Alexey Golyshev Thank you, Alexey. Strictly speaking, it is not the result I am after, but still may be useful later. $\endgroup$ May 9, 2018 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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