New: mean shift noise removal and color reduction

Announcements pertaining to ImageMagick, or ImageMagick related software. This list is moderated. No discussions here, instead post to the users group instead.
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

New: mean shift noise removal and color reduction

Post by fmw42 »

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:

Code: Select all

convert image -mean-shift WxH+D[%] result
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:

Image

add gaussian noise

Code: Select all

convert peppers.png -seed 100 -attenuate 1 +noise gaussian peppers_gnoise1.png
Image

Window size 7x7, RGB:

Code: Select all

convert peppers_gnoise1.png -mean-shift 7x7+10% peppers_gnoise1_im_ms_7x10_rgb.png
Image


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
Image


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
Image


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
Image


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
Image

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
Image

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
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: New: mean shift noise removal and color reduction

Post by snibgo »

Gosh! Canny, Hough and now mean-shift. IM goes from strength to strength.

I don't quite understand:
fmw42 wrote:From those pixels, it then computes the new mean color (for each channel) and the new center x,y from those pixels that are within the specified color distance from the (center) pixel. This process is iterated ...
Perhaps this means (correct me if I'm wrong):

From those pixels, it then computes the new mean color (for each channel). It finds which of those pixels are within the specified color distance from this mean, and computes the center (centroid?) of those. This new x,y is used as the center of a new window. This process is iterated ...
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: New: mean shift noise removal and color reduction

Post by fmw42 »

Thanks. I will try to make it clearer.
Post Reply