Page 1 of 1

[Solved] Average of random samples

Posted: 2016-08-17T16:38:26-07:00
by jmac698
Hi,
I'd like to sample random pixels with a mask (containing white for sample, black for not-sample), then average those pixels. This would be simple except there's a variable number of pixels to average.

Example:
picture
31 41
59 26
Mask:
255 0
255 0
Mask step:
31 0
59 0
Average, 4x4:
45

A straight average would be 23. There could be 1-4 pixels in each 4x4 area. I want to use large downsampling, even 16x16 areas.
One way to do it is create count of mask pixels for each nxn area, and a sum of sampled pixels (but without overflowing!), then divide them.

There's so many methods in IM, I'm looking for ideas and hints here.
Thanks.

Re: Average of random samples

Posted: 2016-08-17T16:42:21-07:00
by fmw42
If you want the global average of only white masked pixels in an image, you can get that by putting the black/white mask into the alpha channel and using -scale 1x1 -alpha off

Unix syntax:

Code: Select all

convert image mask -alpha off -compose copy_opacity -composite -scale 1x1! -alpha off -format "%[pixel:u.p{0,0}]" info:
This skips any transparent pixels when doing the average using -scale.

Please, always provide your IM version and platform when asking questions, since syntax may differ.

See viewtopic.php?f=1&t=9620

Re: Average of random samples

Posted: 2016-08-17T16:50:48-07:00
by jmac698
Hi,
Thanks for the quick response. The version in IM 7. This is for images, and not just global average. My example showed a 2x2 image, of course I will use much larger ones.
Maybe it helps to picture it this way. A mask with noise (white/black pixels), where white pixels sample the image (also can say transparent/opaque), but I want the average of only the pixel that shine through (not including the masked ones).

This has a real purpose btw, using blue noise to resize for no aliasing (it's used for 3d, not so much in images, so this is a new application).

Re: Average of random samples

Posted: 2016-08-17T17:40:19-07:00
by snibgo
As Fred says, but the final word of the command needs a colon.

Code: Select all

convert image mask -alpha off -compose copy_opacity -composite -scale 1x1! -alpha off -format "%[pixel:u.p{0,0}]" info:

Re: Average of random samples

Posted: 2016-08-17T17:47:20-07:00
by fmw42
snibgo wrote:final word of the command needs a colon.
Thanks for catching that. It was a cut and paste error. I have fixed my previous post.

Re: Average of random samples

Posted: 2016-08-17T21:45:41-07:00
by jmac698
You're right it works :) Thanks again.

Re: [Solved] Average of random samples

Posted: 2016-08-17T23:47:58-07:00
by jmac698
I'm getting this error; how can I work that?

Code: Select all

magick lighthouse_x4.jpg BlueNoise.gif -alpha off -compose copy_opacity -composite -scale %w/4x%h/4! -alpha off test.jpg
magick: invalid geometry `2048/4x3072/4!' @ error/geometry.c/ParseRegionGeometry/1542.

Re: [Solved] Average of random samples

Posted: 2016-08-18T00:29:26-07:00
by snibgo
Scaling by 25% seems the obvious method.

But if you want expressions for the two dimensions, they must be fx expressions, so you could have:

Code: Select all

%[fx:w/4]x%[fx:h/4]

Re: [Solved] Average of random samples

Posted: 2016-08-18T09:14:04-07:00
by jmac698
Yes, my bad, I knew that but was tired. However, every answer you give me I learn a lot!

Now I wanted to share some images of my new technique, sample by blue noise. Blue noise is a dense noise with no clustering or obvious patterns. It's great for stippling (dithering), and reducing aliasing in 3d renders. It should work well with pictures for the same reason.

resampled
Image
bluesized
Image

In the end it just looks noisier, so I'll call this a failed experiment.

However, the IM techniques I've learned can applied to something else...