The mean shift algorithm is iterative and thus slower the larger the window size. For each pixel, it gets all the pixels in the window centered at the pixel and excludes those that are outside the squared radius=(width-1)(height-1)/4 surrounding the pixel. From those pixels, it finds which of them are within the specified squared color distance from the current mean. It then computes a new x,y centroid from those coordinates and a new mean. This new x,y centroid is used as the center for a new window. This process is iterated until it converges and the final mean is then used to replace the original pixel value. It repeats this process for the next pixel, etc, until it processes all pixels in the image. Results are better when using other colorspaces rather than RGB. Recommend YIQ, YUV or YCbCr, which seem to give equivalent results.
The syntax is as follows:
Code: Select all
convert image -mean-shift WxH+D[%] result
Examples of noise reduction for small window sizes:
Source image:
add gaussian noise
Code: Select all
convert peppers.png -seed 100 -attenuate 1 +noise gaussian peppers_gnoise1.png
Window size 7x7, RGB:
Code: Select all
convert peppers_gnoise1.png -mean-shift 7x7+10% peppers_gnoise1_im_ms_7x10_rgb.png
Window size 7x7, YIQ:
Code: Select all
convert peppers_gnoise1.png -colorspace YIQ -mean-shift 7x7+10% -set colorspace YIQ -colorspace sRGB peppers_gnoise1_im_ms_7x10_yiq.png
Window size 9x9, YIQ:
Code: Select all
convert peppers_gnoise1.png -colorspace YIQ -mean-shift 9x9+10% -set colorspace YIQ -colorspace sRGB peppers_gnoise1_im_ms_9x10_yiq.png
Window size 11x11, YIQ:
Code: Select all
convert peppers_gnoise1.png -colorspace YIQ -mean-shift 11x11+10% -set colorspace YIQ -colorspace sRGB peppers_gnoise1_im_ms_9x11_yiq.png
Example of color reduction:
Window size 51x51, YIQ
Code: Select all
convert peppers.png -colorspace YIQ -monitor -mean-shift 51x51+10% +monitor -set colorspace YIQ -colorspace sRGB peppers_im_ms_51x10_yiq.png
Reduce depth and select 16 colors
Code: Select all
convert peppers_im_ms_51x10_yiq.png -depth 4 +dither -colors 16 peppers_im_ms_51x10_yiq_d4_c16.png
Reduce depth and select 16 colors from original image without the mean shift
Code: Select all
convert peppers.png -depth 4 +dither -colors 16 peppers_d4_c16.png