I am trying to count the number of distinct colours in a $5\times5$ box, (a radius 2 filter) at all points over a quantized image. I cannot seem to get anything out of the following code except for a black square:
img = ColorQuantize[ExampleData[{"TestImage", "Peppers"}], 8, Dithering -> False];
dis = ImageFilter[CountDistinct[Flatten[#, 1]] &, img, 2];
dis // ImageAdjust
I expect each pixel in the image to be replaced with a single non-negative integer telling me how many unique colours are in the radius 2 vicinity of that pixel. It's plain to see you can choose $5\times5$ boxes in the peppers image which have more than one colour so the output should look more interesting.
I'd also like to know why this related code for 1D produces all 1's instead of the number of unique elements in a centered window as it slews across the list, and how to correct it:
MovingMap[CountDistinct, {1, 2, 3, 3, 3, 4, 5, 6}, {1, Center}, "Reflected"]
For 1D, I want to achieve with MovingMap
the same behaviour you can get with Partition
like this:
CountDistinct /@ Partition[{1, 2, 3, 3, 3, 4, 5, 6}, 3, 1, 2, {}]
MovingMap
you are asking for a window of size 1, so the inputs toCountDistinct
are lists with only one element, so the output is always 1 (you can see what happens by replacingCountDistinct
with an undefinedf
). You would get closer with something likeMovingMap[CountDistinct, yourList, Quantity[3, "Events"]]
, but I am not convinced that you can reproduce the partitioning you get withPartition
usingMovingMap
. $\endgroup$MovingMap
case for me. Any idea how to get the distinct colourImageFilter
to work? $\endgroup$