ImageMagick using alpha channel to control calculating min, max and mean image values












0














ImageMagick command identify prints to screen the min, max and mean values of all the pixels in an image - e.g. for an RGB TIF image we can see the mean thus:



identify -verbose -quiet image.tif | grep mean


which lists (for Red Green Blue and Gray):



mean: 122.974 (0.48225)
mean: 107.722 (0.422438)
mean: 84.1278 (0.329913)
mean: 104.941 (0.411534)


Q: if my image has a boolean alpha channel can I use it to limit the calculations to include only those pixels where alpha was set to 1?



I tried using the clip-mask option with either leading - or + but the means didn't change as would be expected.










share|improve this question
























  • I'm not at a computer, but you know how to count the on and off alpha pixels, yes? And if you multiply the RGB channels by the alpha channel all transparent pixels will go to black so you can find the max. And if you multiply by (255-alpha) you can find the min.
    – Mark Setchell
    Nov 19 '18 at 21:25






  • 1




    Good idea, Mark!
    – fmw42
    Nov 19 '18 at 23:35










  • thanks also to Mark!
    – GavinBrelstaff
    Nov 20 '18 at 17:16
















0














ImageMagick command identify prints to screen the min, max and mean values of all the pixels in an image - e.g. for an RGB TIF image we can see the mean thus:



identify -verbose -quiet image.tif | grep mean


which lists (for Red Green Blue and Gray):



mean: 122.974 (0.48225)
mean: 107.722 (0.422438)
mean: 84.1278 (0.329913)
mean: 104.941 (0.411534)


Q: if my image has a boolean alpha channel can I use it to limit the calculations to include only those pixels where alpha was set to 1?



I tried using the clip-mask option with either leading - or + but the means didn't change as would be expected.










share|improve this question
























  • I'm not at a computer, but you know how to count the on and off alpha pixels, yes? And if you multiply the RGB channels by the alpha channel all transparent pixels will go to black so you can find the max. And if you multiply by (255-alpha) you can find the min.
    – Mark Setchell
    Nov 19 '18 at 21:25






  • 1




    Good idea, Mark!
    – fmw42
    Nov 19 '18 at 23:35










  • thanks also to Mark!
    – GavinBrelstaff
    Nov 20 '18 at 17:16














0












0








0







ImageMagick command identify prints to screen the min, max and mean values of all the pixels in an image - e.g. for an RGB TIF image we can see the mean thus:



identify -verbose -quiet image.tif | grep mean


which lists (for Red Green Blue and Gray):



mean: 122.974 (0.48225)
mean: 107.722 (0.422438)
mean: 84.1278 (0.329913)
mean: 104.941 (0.411534)


Q: if my image has a boolean alpha channel can I use it to limit the calculations to include only those pixels where alpha was set to 1?



I tried using the clip-mask option with either leading - or + but the means didn't change as would be expected.










share|improve this question















ImageMagick command identify prints to screen the min, max and mean values of all the pixels in an image - e.g. for an RGB TIF image we can see the mean thus:



identify -verbose -quiet image.tif | grep mean


which lists (for Red Green Blue and Gray):



mean: 122.974 (0.48225)
mean: 107.722 (0.422438)
mean: 84.1278 (0.329913)
mean: 104.941 (0.411534)


Q: if my image has a boolean alpha channel can I use it to limit the calculations to include only those pixels where alpha was set to 1?



I tried using the clip-mask option with either leading - or + but the means didn't change as would be expected.







command-line imagemagick alpha






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:43







GavinBrelstaff

















asked Nov 19 '18 at 19:30









GavinBrelstaffGavinBrelstaff

1,79411131




1,79411131












  • I'm not at a computer, but you know how to count the on and off alpha pixels, yes? And if you multiply the RGB channels by the alpha channel all transparent pixels will go to black so you can find the max. And if you multiply by (255-alpha) you can find the min.
    – Mark Setchell
    Nov 19 '18 at 21:25






  • 1




    Good idea, Mark!
    – fmw42
    Nov 19 '18 at 23:35










  • thanks also to Mark!
    – GavinBrelstaff
    Nov 20 '18 at 17:16


















  • I'm not at a computer, but you know how to count the on and off alpha pixels, yes? And if you multiply the RGB channels by the alpha channel all transparent pixels will go to black so you can find the max. And if you multiply by (255-alpha) you can find the min.
    – Mark Setchell
    Nov 19 '18 at 21:25






  • 1




    Good idea, Mark!
    – fmw42
    Nov 19 '18 at 23:35










  • thanks also to Mark!
    – GavinBrelstaff
    Nov 20 '18 at 17:16
















I'm not at a computer, but you know how to count the on and off alpha pixels, yes? And if you multiply the RGB channels by the alpha channel all transparent pixels will go to black so you can find the max. And if you multiply by (255-alpha) you can find the min.
– Mark Setchell
Nov 19 '18 at 21:25




I'm not at a computer, but you know how to count the on and off alpha pixels, yes? And if you multiply the RGB channels by the alpha channel all transparent pixels will go to black so you can find the max. And if you multiply by (255-alpha) you can find the min.
– Mark Setchell
Nov 19 '18 at 21:25




1




1




Good idea, Mark!
– fmw42
Nov 19 '18 at 23:35




Good idea, Mark!
– fmw42
Nov 19 '18 at 23:35












thanks also to Mark!
– GavinBrelstaff
Nov 20 '18 at 17:16




thanks also to Mark!
– GavinBrelstaff
Nov 20 '18 at 17:16












1 Answer
1






active

oldest

votes


















2














In ImageMagick, the -scale 1x1! function can be used to compute the mean without including alpha so that you get the mean of only opaque values. So you could do the following:



Create test transparent image:



convert logo: -transparent white logot.png




Compute mean values:



convert logot.png -scale 1x1! -alpha off -format "%[fx:255*u.r], %[fx:255*u.g], %[fx:255*u.b]" info:

100.202, 81.4747, 98.6342





Alternately, you can use the alpha channel as a mask to get the means. You compute the mean of each channel without the alpha channel and the background under the alpha set to black. Then compute the mean of the alpha channel. Then divide each channel mean by the mean of the alpha channel.



convert logo: -transparent white logot.png
convert logot.png -alpha extract alpha.png

means_rgb=$(convert logot.png -background black -alpha background -alpha off -format "%[fx:mean.r],%[fx:mean.g],%[fx:mean.b]" info:)
mean_r=$(echo $means_rgb | cut -d, -f1)
mean_g=$(echo $means_rgb | cut -d, -f2)
mean_b=$(echo $means_rgb | cut -d, -f3)

mean_alpha=$(convert alpha.png -format "%[fx:mean]" info:)

mean_r=$(convert xc: -format "%[fx:255*$mean_r/$mean_alpha]" info:)
mean_g=$(convert xc: -format "%[fx:255*$mean_g/$mean_alpha]" info:)
mean_b=$(convert xc: -format "%[fx:255*$mean_b/$mean_alpha]" info:)

echo "$mean_r, $mean_g, $mean_b"

100.203, 81.4768, 98.6362





To get the min and max, taking the cue from Mark Setchell's idea:



convert logot.png -background black -alpha background -alpha off -format "%[fx:255*maxima.r], %[fx:255*maxima.g], %[fx:255*maxima.b]n" info:

255, 250, 244


convert logot.png -background white -alpha background -alpha off -format "%[fx:255*minima.r], %[fx:255*minima.g], %[fx:255*minima.b]n" info:

4, 0, 0








share|improve this answer























  • Yeah but unfortunately the -scale 1x1! trick doesn't work for minimum.
    – GavinBrelstaff
    Nov 19 '18 at 19:56










  • I have modified my answer to include computing the max and min values using a modification of the suggestion from Mark Setchell.
    – fmw42
    Nov 19 '18 at 23:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381412%2fimagemagick-using-alpha-channel-to-control-calculating-min-max-and-mean-image-v%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














In ImageMagick, the -scale 1x1! function can be used to compute the mean without including alpha so that you get the mean of only opaque values. So you could do the following:



Create test transparent image:



convert logo: -transparent white logot.png




Compute mean values:



convert logot.png -scale 1x1! -alpha off -format "%[fx:255*u.r], %[fx:255*u.g], %[fx:255*u.b]" info:

100.202, 81.4747, 98.6342





Alternately, you can use the alpha channel as a mask to get the means. You compute the mean of each channel without the alpha channel and the background under the alpha set to black. Then compute the mean of the alpha channel. Then divide each channel mean by the mean of the alpha channel.



convert logo: -transparent white logot.png
convert logot.png -alpha extract alpha.png

means_rgb=$(convert logot.png -background black -alpha background -alpha off -format "%[fx:mean.r],%[fx:mean.g],%[fx:mean.b]" info:)
mean_r=$(echo $means_rgb | cut -d, -f1)
mean_g=$(echo $means_rgb | cut -d, -f2)
mean_b=$(echo $means_rgb | cut -d, -f3)

mean_alpha=$(convert alpha.png -format "%[fx:mean]" info:)

mean_r=$(convert xc: -format "%[fx:255*$mean_r/$mean_alpha]" info:)
mean_g=$(convert xc: -format "%[fx:255*$mean_g/$mean_alpha]" info:)
mean_b=$(convert xc: -format "%[fx:255*$mean_b/$mean_alpha]" info:)

echo "$mean_r, $mean_g, $mean_b"

100.203, 81.4768, 98.6362





To get the min and max, taking the cue from Mark Setchell's idea:



convert logot.png -background black -alpha background -alpha off -format "%[fx:255*maxima.r], %[fx:255*maxima.g], %[fx:255*maxima.b]n" info:

255, 250, 244


convert logot.png -background white -alpha background -alpha off -format "%[fx:255*minima.r], %[fx:255*minima.g], %[fx:255*minima.b]n" info:

4, 0, 0








share|improve this answer























  • Yeah but unfortunately the -scale 1x1! trick doesn't work for minimum.
    – GavinBrelstaff
    Nov 19 '18 at 19:56










  • I have modified my answer to include computing the max and min values using a modification of the suggestion from Mark Setchell.
    – fmw42
    Nov 19 '18 at 23:44
















2














In ImageMagick, the -scale 1x1! function can be used to compute the mean without including alpha so that you get the mean of only opaque values. So you could do the following:



Create test transparent image:



convert logo: -transparent white logot.png




Compute mean values:



convert logot.png -scale 1x1! -alpha off -format "%[fx:255*u.r], %[fx:255*u.g], %[fx:255*u.b]" info:

100.202, 81.4747, 98.6342





Alternately, you can use the alpha channel as a mask to get the means. You compute the mean of each channel without the alpha channel and the background under the alpha set to black. Then compute the mean of the alpha channel. Then divide each channel mean by the mean of the alpha channel.



convert logo: -transparent white logot.png
convert logot.png -alpha extract alpha.png

means_rgb=$(convert logot.png -background black -alpha background -alpha off -format "%[fx:mean.r],%[fx:mean.g],%[fx:mean.b]" info:)
mean_r=$(echo $means_rgb | cut -d, -f1)
mean_g=$(echo $means_rgb | cut -d, -f2)
mean_b=$(echo $means_rgb | cut -d, -f3)

mean_alpha=$(convert alpha.png -format "%[fx:mean]" info:)

mean_r=$(convert xc: -format "%[fx:255*$mean_r/$mean_alpha]" info:)
mean_g=$(convert xc: -format "%[fx:255*$mean_g/$mean_alpha]" info:)
mean_b=$(convert xc: -format "%[fx:255*$mean_b/$mean_alpha]" info:)

echo "$mean_r, $mean_g, $mean_b"

100.203, 81.4768, 98.6362





To get the min and max, taking the cue from Mark Setchell's idea:



convert logot.png -background black -alpha background -alpha off -format "%[fx:255*maxima.r], %[fx:255*maxima.g], %[fx:255*maxima.b]n" info:

255, 250, 244


convert logot.png -background white -alpha background -alpha off -format "%[fx:255*minima.r], %[fx:255*minima.g], %[fx:255*minima.b]n" info:

4, 0, 0








share|improve this answer























  • Yeah but unfortunately the -scale 1x1! trick doesn't work for minimum.
    – GavinBrelstaff
    Nov 19 '18 at 19:56










  • I have modified my answer to include computing the max and min values using a modification of the suggestion from Mark Setchell.
    – fmw42
    Nov 19 '18 at 23:44














2












2








2






In ImageMagick, the -scale 1x1! function can be used to compute the mean without including alpha so that you get the mean of only opaque values. So you could do the following:



Create test transparent image:



convert logo: -transparent white logot.png




Compute mean values:



convert logot.png -scale 1x1! -alpha off -format "%[fx:255*u.r], %[fx:255*u.g], %[fx:255*u.b]" info:

100.202, 81.4747, 98.6342





Alternately, you can use the alpha channel as a mask to get the means. You compute the mean of each channel without the alpha channel and the background under the alpha set to black. Then compute the mean of the alpha channel. Then divide each channel mean by the mean of the alpha channel.



convert logo: -transparent white logot.png
convert logot.png -alpha extract alpha.png

means_rgb=$(convert logot.png -background black -alpha background -alpha off -format "%[fx:mean.r],%[fx:mean.g],%[fx:mean.b]" info:)
mean_r=$(echo $means_rgb | cut -d, -f1)
mean_g=$(echo $means_rgb | cut -d, -f2)
mean_b=$(echo $means_rgb | cut -d, -f3)

mean_alpha=$(convert alpha.png -format "%[fx:mean]" info:)

mean_r=$(convert xc: -format "%[fx:255*$mean_r/$mean_alpha]" info:)
mean_g=$(convert xc: -format "%[fx:255*$mean_g/$mean_alpha]" info:)
mean_b=$(convert xc: -format "%[fx:255*$mean_b/$mean_alpha]" info:)

echo "$mean_r, $mean_g, $mean_b"

100.203, 81.4768, 98.6362





To get the min and max, taking the cue from Mark Setchell's idea:



convert logot.png -background black -alpha background -alpha off -format "%[fx:255*maxima.r], %[fx:255*maxima.g], %[fx:255*maxima.b]n" info:

255, 250, 244


convert logot.png -background white -alpha background -alpha off -format "%[fx:255*minima.r], %[fx:255*minima.g], %[fx:255*minima.b]n" info:

4, 0, 0








share|improve this answer














In ImageMagick, the -scale 1x1! function can be used to compute the mean without including alpha so that you get the mean of only opaque values. So you could do the following:



Create test transparent image:



convert logo: -transparent white logot.png




Compute mean values:



convert logot.png -scale 1x1! -alpha off -format "%[fx:255*u.r], %[fx:255*u.g], %[fx:255*u.b]" info:

100.202, 81.4747, 98.6342





Alternately, you can use the alpha channel as a mask to get the means. You compute the mean of each channel without the alpha channel and the background under the alpha set to black. Then compute the mean of the alpha channel. Then divide each channel mean by the mean of the alpha channel.



convert logo: -transparent white logot.png
convert logot.png -alpha extract alpha.png

means_rgb=$(convert logot.png -background black -alpha background -alpha off -format "%[fx:mean.r],%[fx:mean.g],%[fx:mean.b]" info:)
mean_r=$(echo $means_rgb | cut -d, -f1)
mean_g=$(echo $means_rgb | cut -d, -f2)
mean_b=$(echo $means_rgb | cut -d, -f3)

mean_alpha=$(convert alpha.png -format "%[fx:mean]" info:)

mean_r=$(convert xc: -format "%[fx:255*$mean_r/$mean_alpha]" info:)
mean_g=$(convert xc: -format "%[fx:255*$mean_g/$mean_alpha]" info:)
mean_b=$(convert xc: -format "%[fx:255*$mean_b/$mean_alpha]" info:)

echo "$mean_r, $mean_g, $mean_b"

100.203, 81.4768, 98.6362





To get the min and max, taking the cue from Mark Setchell's idea:



convert logot.png -background black -alpha background -alpha off -format "%[fx:255*maxima.r], %[fx:255*maxima.g], %[fx:255*maxima.b]n" info:

255, 250, 244


convert logot.png -background white -alpha background -alpha off -format "%[fx:255*minima.r], %[fx:255*minima.g], %[fx:255*minima.b]n" info:

4, 0, 0









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 1:23

























answered Nov 19 '18 at 19:50









fmw42fmw42

7,47341427




7,47341427












  • Yeah but unfortunately the -scale 1x1! trick doesn't work for minimum.
    – GavinBrelstaff
    Nov 19 '18 at 19:56










  • I have modified my answer to include computing the max and min values using a modification of the suggestion from Mark Setchell.
    – fmw42
    Nov 19 '18 at 23:44


















  • Yeah but unfortunately the -scale 1x1! trick doesn't work for minimum.
    – GavinBrelstaff
    Nov 19 '18 at 19:56










  • I have modified my answer to include computing the max and min values using a modification of the suggestion from Mark Setchell.
    – fmw42
    Nov 19 '18 at 23:44
















Yeah but unfortunately the -scale 1x1! trick doesn't work for minimum.
– GavinBrelstaff
Nov 19 '18 at 19:56




Yeah but unfortunately the -scale 1x1! trick doesn't work for minimum.
– GavinBrelstaff
Nov 19 '18 at 19:56












I have modified my answer to include computing the max and min values using a modification of the suggestion from Mark Setchell.
– fmw42
Nov 19 '18 at 23:44




I have modified my answer to include computing the max and min values using a modification of the suggestion from Mark Setchell.
– fmw42
Nov 19 '18 at 23:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381412%2fimagemagick-using-alpha-channel-to-control-calculating-min-max-and-mean-image-v%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith