Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace/repaint all pixels with one color in image with interpolated from neighboring pixels

Tags:

imagemagick

I have a GIF image generated by a program where each output value is represented by its color via attached color palette. I need to replace one value, i.e. color from image with interpolated from neighboring pixels. Since I don't have possibility to alter programs output, I need to modify the output image. The resulting image will be saved in the PNG or GIF format.

I can easily extract (mask) all pixels that need repainting, since they have fixed color, but I was unable to find solution on how to replace a color of one/all pixels in imagemagick with interpolated color from neighboring pixels. Is there a way to do this in imagemagick?

The raw values of the pixels are proportional to the physical value, so it would be great if the interpolation could be done on raw values that are then later transformed to the color via supplied color palette.

Attached image shows the original (left) and processed manually in GIMP (right).

Original and processed image

like image 344
Vinko Surija Avatar asked Oct 27 '25 06:10

Vinko Surija


1 Answers

One technique is to replace the offending color with the background, and then use a combination of erode & dilate morphology to remove the paths.

Given...

input.png

convert input.png \
        -fill white -fuzz 10% -opaque black \
        -morphology Erode Diamond \
        -morphology Dilate Diamond \
        output.png

output.png

It's not a true interpolate from nearest neighbors, but close. Also note the rounding errors across edges.

edge rounding errors


Updated

Or as Fred pointed out in the comments, just use -morphology Smooth Diamond instead of Erode + Dilate

convert input.png \
        -fill white -fuzz 10% -opaque black \
        -morphology Smooth Diamond \
        output.png
like image 105
emcconville Avatar answered Oct 29 '25 00:10

emcconville