# Calculating mean difference using ArcGIS focal statistics?

For each pixel of my raster, I would like to calculate the mean difference with the neighbour pixels on a given radius.

The tool "focal statistic" looks like exactly what I need, however, there is no way to calculate the mean difference with it, only the sum, the mean.

Does anyone have an idea about how to solve this by using an approach equivalent to the focal statistic but that allows me to calculate the mean difference? It could be in several steps, like first calculating the sum or the differences from which I could, then calculate the mean... or whatever that works to reach my goal.

• Is it the mean difference between the values of the 8 surrounding against the centre cell? I don't think you can do that with focal statistics. You can average (mean) the surrounding values and take a difference from that value with a kernel resources.arcgis.com/en/help/main/10.2/index.html#//…. You might need to read the raster as a numpy array and work with that, is arcpy an option for you? – Michael Stimson Aug 9 '17 at 21:07
• Hi, thanks you. Yes, I think it is: calculating the difference btw the center cell and each of the surrounding cells, and then calculates the mean of these diff for each pixel (I am not a native English speaker so I prefer to explain with my own words to avoid confusion). I'm not sure the kernel will really help to perform this "mean difference", but I never used it and I may misunderstand. I first need to calculate the difference and then average these differences. Do you think using the kernel is the right option? I have never used arcpy but I can try if it seems to be the best option! – user3297731 Aug 11 '17 at 7:30
• You could create a difference raster then average with a kernel as per radouxju answer. I would use NbrIrregular with a kernel 3 3\n 1 1 1\n 1 0 1\n 1 1 1\n where \n is a new line, this would define the kernel for the focal statistics mean of the 8 surrounding cells but ignoring the centre.. when it gets to an edge the focal mean should only average the cells that exist but that will not affect a mean overly, a sum on the other hand would be adversely affected at the edges and adjacent to NoData cells. – Michael Stimson Aug 13 '17 at 21:19
• Thanks for this idea. I still understand that the kerne will help me to define the cells I will use for the calculation and don't consider the center cell, which is great. Good to know also about how to deal With NoData and the edges, which would be 1 of my next question. However, my main question for now is how to create this "difference raster". I really don't figure out how to make it calculating the difference between the center cell and the neighbour cells... it looks clear in your mind but for me honestly this is my main issue. What do I miss in my understanding? – user3297731 Aug 15 '17 at 8:10
• Just to give an idea of how I did 1st to calculate the "mean difference raster": I used the "shift tool" by shifting my raster by 1 cell towards N, 1 cell towards NE, etc. in the 8 aspects, then calculated the difference btw the non-shifted raster and the shifted ones, and averaged the 8 rasters of difference to get the "mean diff". Now I need to do it for a radius of 50 neighbour cells. I thought focal stat. could help if it was handling "diff". I see how helpful the kernels could be, but I still don't get how to use them to calculate the "mean diff". What do I miss in my understanding? – user3297731 Aug 15 '17 at 8:33

this is a quick answer, I will expand if needed.

You can use a custom text file with your own weights (see help here) in order to compute the mean difference. Here is an example for a 3 by 3 kernel:

3 3

-0.125 -0.125 -0.125

-0.125 1 -0.125

-0.125 -0.125 -0.125

save this in a text file and use it as a weight kernel with the "sum" in focal stats. This weighted sum will be equal to a weighted difference.

• I did not realize the weight kernel accepted negative values... well, I've learned something today. Great answer! – Michael Stimson Aug 9 '17 at 21:44

Thanks a lot for the suggestion.

But here again I don't really understand how to use the kernel. I get that it defines the shape of the nwighborhood with 0 and non-0 values, but I don't really understand the role of non-0 values in the calculation.

Sorry I may look very stupid (but I have excuses: I am blond and > 8 months pregnant and it is well known that the IQ decreases along pregancy ;-)).

So, if I give a weight of -0.125, for example and that I use the "sum" in focal stat, does it mean that it makes the sum of the cell values + (-0.125)? If ye, then I don't really understand how to use this weighted kernel to make it doing the sum or other calculation between the centre pixel and surrounding pixels.

I obvioulsy miss something in my understanding ... I would be very happy to have some clarification if possible. Thank you very much for the support!