# Extracting intensity from an image

Suppose I have an image

sampleOriginal = Import["...mySample.png"]


I then convert it to Grayscale colorspace

sample = ColorConvert[sampleOriginal, "Grayscale"]


Now

sampleDate := ImageData[sample]


gives me a matrix of image data points. And my question is how can I create a function I[x,y] that will return the intensity of the gray-scale image at the point {x, y}. Is there a suitable way to create this function which will later allow me to use ND and calculate the derivatives up to the second order at a given point ?

• (I am very sorry if there already is an answer to this which I did not find, or maybe I found it but did not realize that it was the answer) Jan 12, 2015 at 21:09
• You should not need a special function for the first part of your question, since ImageData[sample][[x,y]] should give you the value you are looking for. Take a look at Interpolation for the 2nd part of your question. Jan 12, 2015 at 22:09

Using ND is not necessary, as Mathematica makes it easy to construct InterpolatingFunction objects, which can then be formally differentiated using Derivative. First, a sample image:

data = Import[
"http://41.media.tumblr.com/bece7ed81870c8e9b904290700451eb2/tumblr_\
mv8m2sE2FW1slvzano1_400.jpg"]


Convert to grayscale, and then convert the resulting matrix into an InterpolatingFunction object:

imData = ImageData[ColorConvert[data, "Grayscale"]];
f = ListInterpolation[imData];


Plot the InterpolatingFunction using DensityPlot:

DensityPlot[f[-x, y], {y, 1, 400}, {x, -1, -398}, PlotPoints -> 300,
ColorFunction -> GrayLevel]


Plot the first partial derivative in the $x$-direction:

g = Derivative[1, 0][f];
DensityPlot[g[-x, y], {y, 1, 400}, {x, -1, -398}, PlotPoints -> 300,
PlotRange -> All, ColorFunction -> GrayLevel]


Plot the second partial derivative in the $x$-direction:

h = Derivative[2, 0][f];
DensityPlot[h[-x, y], {y, 1, 400}, {x, -1, -398}, PlotPoints -> 300,
PlotRange -> All, ColorFunction -> GrayLevel]


Of course, you can get better results with even less work by using bill s's answer.

• Thanks. This exactly what I was looking for. It answers the question. Jan 13, 2015 at 19:01

You can get the values of the pixel intensities directly using the built-in functions PixelValue and/or ImageValue. For example, using DumpsterDoofus's image, the RGB pixel values at location {100,100} are easy to find, and their mean is (roughly) the intensity.

data = Import["http://41.media.tumblr.com/bece7ed81870c8e9b904290700451eb2/tumblr_mv8m2sE2FW1slvzano1_400.jpg"];
PixelValue[data, {100, 100}]
Mean[PixelValue[data, {100, 100}]]


Depending on what you want to do with the image, there may be no need to do the interpolation at all. There are many functions that deal directly with images, including GradientFilter (that does more or less what you ask for in a single command). If your goal in taking the derivatives is to detect edges, then there is Edgedetect. GradientOreintationFilter is another option.

GradientFilter[data, {2, 0.1}]


You can take the second derivative

GradientFilter[GradientFilter[data, {1, 0.1}], {1, 0.1}]


and there are numerous options to control the process.

• Yeah, taking advantage of the built-in image processing tools will definitely give better results than my example of manually doing everything (I mainly wanted to show the OP how list interpolation works). Jan 13, 2015 at 1:26