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.
[Solved] Average of random samples
[Solved] Average of random samples
Last edited by jmac698 on 2016-08-17T21:46:01-07:00, edited 1 time in total.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Average of random samples
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:
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
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:
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
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).
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).
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Average of random samples
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Average of random samples
Thanks for catching that. It was a cut and paste error. I have fixed my previous post.snibgo wrote:final word of the command needs a colon.
Re: Average of random samples
You're right it works
Thanks again.
![Smile :)](./images/smilies/icon_smile.gif)
Re: [Solved] Average of random samples
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: [Solved] Average of random samples
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:
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
Re: [Solved] Average of random samples
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](https://s4.postimg.org/6mi9lau59/test.jpg)
bluesized
![Image](https://s4.postimg.org/r40hn4mt9/test1.jpg)
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...
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](https://s4.postimg.org/6mi9lau59/test.jpg)
bluesized
![Image](https://s4.postimg.org/r40hn4mt9/test1.jpg)
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...