2
$\begingroup$

I am a Mathematica newbie. I need to sample random points from an image which I have stored in a variable img as:

image=Import["ExampleData/ocelot.jpg"]    
img = ImageData[image]

Can you guys suggest how I can use a BernoulliDistribution to sample points from the image using Flatten, Map, RandomVariate, Partition and Image functions?

I know a similar problem was discussed here but the solutions there are very convoluted and I am not able to follow them. Thanks a lot for the help and very sorry if this seems like a duplicate post but I have spent the whole evening banging my head over the link to the post above but am not able to reproduce the same with the above functions. Thanks again!

$\endgroup$
4
  • $\begingroup$ Can you upload image.jpg somewhere? $\endgroup$ – J. M.'s ennui Oct 2 '18 at 5:25
  • $\begingroup$ Hi, I have updated the question. Thanks a lot for the help! $\endgroup$ – mathematicaNewbie Oct 2 '18 at 5:49
  • $\begingroup$ Can you please clarify if you actually have a grayscale image, or a binary image? The new example you gave is grayscale, so sampling over that is not as clear-cut as the binary image case, which is already dealt with in the thread you linked to. $\endgroup$ – J. M.'s ennui Oct 2 '18 at 6:44
  • $\begingroup$ The image I need to work with is grayscale. Thanks! $\endgroup$ – mathematicaNewbie Oct 2 '18 at 7:07
2
$\begingroup$

I'm assuming you want to vary the Bernoulli parameter p depending on the pixel grayscale value. Darker areas of the image are more likely to generate a black pixel and lighter areas more likely to be white.

img = Import["ExampleData/ocelot.jpg"];
dat = ImageData[img];
result = Image[
  Map[RandomVariate[BernoulliDistribution[#]] &, dat, {2}]]

random stippling effect

The positions where a 1 appears are given by:

PixelValuePositions[result, 1]

The sum of many images averaged over time converges to the grayscale image in the limit.

img = Import["ExampleData/ocelot.jpg"];
dat = ImageData[img];
ListAnimate[
 ImageAdjust[Image[#]] & /@ 
  Accumulate[
   ParallelTable[
    Map[RandomVariate[BernoulliDistribution[#]] &, dat, {2}], {30}]]
 ]

enter image description here

$\endgroup$
1
  • $\begingroup$ This is quite nice! $\endgroup$ – J. M.'s ennui May 17 '20 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.