Mean and SD of opaque pixels
Posted: 2014-02-06T16:53:36-07:00
As part of general photographic processing with masks, I wanted to find the mean and standard deviation of the opaque pixels in an image. Here are two methods.
The first extracts the opaque pixels into a new image, then uses simple IM facilities on that new image. The new image has dimensions (n)x1 where (n) is the number of opaque pixels.
An alternative method is to use a single (but complex) convert command. This is inspired by fmw42's http://www.fmwconcepts.com/imagemagick/ ... shape_mean . It implements the formula: SD = sqrt (average ((u-umean)^2)). Windows syntax.
We can compare the methods. Sample image, a circle with grayscale gradient, against a transparent background:
Extracting the opaque pixels into another image:
The single convert command above:
The difference in SD is slightly over 1 part in 10,000, which is close enough for me.
(Fred: feel free to add this to your tidbits page, if you want.)
EDIT: removed a debugging "-write mean.png" from the single-command convert.
EDIT2: if the input image is fully opaque for the single-command convert, the "CopyOpacity" is wrong so the "SD of opaque" is wrong. If "convert %INFILE% -format "%[opaque]" info:" returns "true", don't use this to find the SD of opaque pixels.
The first extracts the opaque pixels into a new image, then uses simple IM facilities on that new image. The new image has dimensions (n)x1 where (n) is the number of opaque pixels.
An alternative method is to use a single (but complex) convert command. This is inspired by fmw42's http://www.fmwconcepts.com/imagemagick/ ... shape_mean . It implements the formula: SD = sqrt (average ((u-umean)^2)). Windows syntax.
Code: Select all
convert ^
%INFILE% ^
-write mpr:TRANS ^
-format "raw mean: %%[fx:mean]\nraw SD: %%[fx:standard_deviation]\n" -write info: ^
( +clone ^
-scale "1x1^!" ^
-alpha off ^
-format "mean of opaque: %%[fx:u]\n" ^
-write info: ^
-write mpr:MEAN ^
+delete ^
) ^
( +clone ^
-tile mpr:MEAN -draw "color 0,0 reset" ^
) ^
-compose Difference -composite ^
-evaluate Pow 2 ^
mpr:TRANS ^
-compose CopyOpacity -composite ^
-scale "1x1^!" ^
-alpha off ^
-format "SD of opaque: %%[fx:sqrt(u)]\n" info:
Code: Select all
convert ^
-size 200x300 gradient: -rotate 90 ^
( +clone -fill Black -colorize 100 ^
+antialias ^
-fill White -draw "circle 100,100 50,100" ) ^
-alpha off ^
-compose CopyOpacity -composite ^
g.png
Code: Select all
Geometry: 8021x1+0+0
Units: Undefined
Type: Grayscale
Base type: Grayscale
Endianess: Undefined
Colorspace: Gray
Depth: 16-bit
Channel depth:
gray: 16-bit
Channel statistics:
Gray:
min: 10959 (0.167224)
max: 32877 (0.501671)
mean: 21918.1 (0.334448)
standard deviation: 5537.44 (0.0844959)
Code: Select all
raw mean: 0.5
raw SD: 0.289639
mean of opaque: 0.334447
SD of opaque: 0.0845058
(Fred: feel free to add this to your tidbits page, if you want.)
EDIT: removed a debugging "-write mean.png" from the single-command convert.
EDIT2: if the input image is fully opaque for the single-command convert, the "CopyOpacity" is wrong so the "SD of opaque" is wrong. If "convert %INFILE% -format "%[opaque]" info:" returns "true", don't use this to find the SD of opaque pixels.