[Solved] Average of random samples

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

[Solved] Average of random samples

Post 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.
Last edited by jmac698 on 2016-08-17T21:46:01-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Average of random samples

Post 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
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: Average of random samples

Post 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).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Average of random samples

Post 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:
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Average of random samples

Post 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.
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: Average of random samples

Post by jmac698 »

You're right it works :) Thanks again.
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: [Solved] Average of random samples

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [Solved] Average of random samples

Post 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]
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: [Solved] Average of random samples

Post 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...
Post Reply