Page 1 of 1

Confused about median

Posted: 2018-10-21T12:59:14-07:00
by chani
Hi there,

using Linux and ImageMagick 6.9.10-8 Q16 x86_64 20180723

I'm somewhat confused about the results of -statistic median. Please correct me if I'm wrong: Assuming that we do have a 2x2 grayscale grid with two pixel having a value of 134 (gray) and two having a value of 255 (pure white). Calculating the median of that would be: (134, 134, 255, 255) - pick the middle values (134, 255) sum them and divide by 2 = ~194. Correct?

The documentation states that -statistic will "replace each pixel with corresponding statistic from the neighborhood." so if I apply -statistic median 2x2 to a picture, all 4 pixels should be replaced by the result of the statistic, shouldn't they (probably due to my english skills I understood it wrongly)?

I created a 6x4 graphic in which I do have different pixels and issued the following command:

Code: Select all

convert scale-test.png -statistic median 2x2 scale1.png
And I do not understand the result. The above example grid:

134 255
255 134

turns into

255 255
255 255

None of the tests I did has shown the result for a mathematical median operation (is my calculation wrong?)

I started playing around with that when I was looking for a median-resize and found this post: https://www.imagemagick.org/discourse-s ... hp?t=31624 though instead of going with a 5x5 grid I'd like to use the median result of just 4px (2x2).

Okay, so what do I do wrong? :)

Jean

Re: Confused about median

Posted: 2018-10-21T13:08:32-07:00
by fmw42
With even dimensions, I do not thing there will be any averaging done for the middle two. IM probably takes either the first or the second of the two middle values. Looks like from your tests, it takes the second.

Try with odd number of values and see what happens.

Only the middle value is replaced by the median. With odd dimensions there is clear middle value. But with even dimensions, there is no clear middle and it looks like ImageMagick takes the larger in both x and y.

I generally only use odd values for -statistic, otherwise, the images shifts a half pixel.

Re: Confused about median

Posted: 2018-10-21T13:22:34-07:00
by chani
Hi Fred,

thanks for answering that fast - You've got really nice scripts on your page, just by the way. Back to topic: I tried with a 9x9 test-picture and a 3x3 grid. The command is:

Code: Select all

convert scale-test.png -statistic median 3x3 scale1.png
This seems to do more but the result is still confusing me. That is because I would have thought if you apply -statistic that _all_ px in the given grid are replaced by the result of -statistic. That does not seem to be the case:

000 255 255
255 000 255
255 255 000

(so a black line from left top to bottom right) turns into:

000 255 255
255 255 000
255 000 000

Here's a zoomed example of the input on the left and the output on the right:

Image

That doesn't look like a median. Does it?

Jean

Re: Confused about median

Posted: 2018-10-21T13:49:29-07:00
by fmw42
It replaces only the center pixel in each 3x3 neighborhood with its median. So when the command finishes, every pixel will have been replace by the median in its 3x3 region surrounding that pixel. This is the way most regional statistics operators work. For the mean, each pixel will be replaced by the average of its 3x3 neighborhood from the original image. Likewise for the min and max results within a 3x3 neighborhood.

I would refer you to any textbook on image processing or Google it for a description.

Re: Confused about median

Posted: 2018-10-21T13:54:30-07:00
by chani
Hi Fred,

oh. I see. Thanks.

Jean

Re: Confused about median

Posted: 2018-10-21T13:57:53-07:00
by snibgo
Something else to remember: "-statistic" is affected by the "-virtual-pixel" setting.

Re: Confused about median

Posted: 2018-10-21T17:15:17-07:00
by fmw42
The virtual pixel setting is used to extend the image at the borders, so that the edge pixels have values next to them so that each edge pixel can be in the center of a neighborhood.

See, for example, https://www.cs.auckland.ac.nz/courses/c ... ng_2up.pdf for a description