# Transform a black-and-white image into a list or a function

I met such an image in a paper: 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.

• Does applying ImageData help? This would give you the J values. x and y values have to be computed from pixel positions... – Henrik Schumacher May 8 '18 at 9:34
• 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. – andre314 May 8 '18 at 10:17

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];
ListPlot3D[data]

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]  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: 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.

• 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. – Alexei Boulbitch May 9 '18 at 7:37
• 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... – Henrik Schumacher May 9 '18 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] We do Reverse@data because there are different coordinate systems: http://reference.wolfram.com/language/tutorial/ImageProcessing.html#1708646374

• +1 for ColorConvert. – Henrik Schumacher May 8 '18 at 9:58
• @Alexey Golyshev Thank you, Alexey. Strictly speaking, it is not the result I am after, but still may be useful later. – Alexei Boulbitch May 9 '18 at 7:39