New: mean shift noise removal and color reduction
Posted: 2014-04-29T16:29:48-07:00
As of IM 6.8.9.1, Imagemagick will include the mean shift technique for noise removal and color reduction/segmentation
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:
where WxH is the window size and D is the color distance measured in the range 0 to 1 or 0 to 100%.
Examples of noise reduction for small window sizes:
Source image:
add gaussian noise
Window size 7x7, RGB:
Window size 7x7, YIQ:
Window size 9x9, YIQ:
Window size 11x11, YIQ:
Example of color reduction:
Window size 51x51, YIQ
Reduce depth and select 16 colors
Reduce depth and select 16 colors from original image without the mean shift
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