Home > Blockchain >  ImageMagick: Divide AE distortion by total pixels in fx output info format
ImageMagick: Divide AE distortion by total pixels in fx output info format

Time:02-16

I am trying to use ImageMagick 7 to detect if a specific channel in an image is largely pure black and pure white (plus a little antialiasing, and there's a chance the image could be pure black). This is to distinguish from another kind of image that shares a naming convention but has photographic-like image data in the r/g/b channels.

(Basically both image types are specular maps from different engines. The one I'm trying to differentiate here is more modern and has the metallic map in the blue channel; the other is much older and just has the specular colour in the RGB channels and the gloss map in the alpha.)

Currently I'm comparing the channel to a clone of itself that has had a 50% threshold applied, using the AE metric to see if it's largely the same apart from a small amount of antialiasing, and a fuzz of 1% to account for occasional aberration from pure black/white. This command works, but of course at the moment it only returns the number of distorted pixels:

magick  ( "file.png" -channel b -separate ) ^
        (  clone -channel b -separate -threshold 50% ) ^
        -fuzz 1% -metric AE -compare ^
        -format "%[distortion]" info:

Because the input image sizes will vary, I want to divide the distortion by the total number of pixels in the image to get the relative amount of the image that's not pure black/white -- under 10% has seemed good so far in my manual testing -- but I can't get the format syntax right. Everything I've tried -- for example "%[fx:%[distortion]/w*h]" -- has given the magick: undefined variable `[distortion]' @ error/fx.c/FxGetSymbol/1169 error.

What syntax should I use? (And if there's a better way to do what I'm doing, I always appreciate it!)

CodePudding user response:

I believe the following is what you want in Imagemagick. Basically you save the distortion in -set option: argument and then use it in -fx later.

However, clone gives you just the b channel, so there should be no need for -channel b -separate in your second line.

magick  ( "file.png" -channel b -separate ) ^
        (  clone -threshold 50% ) ^
        -fuzz 1% -metric AE -compare ^
        -set option:distort "%[distortion]" ^
        -format "%[fx:distort/(w*h)]" info:
  • Related