5
$\begingroup$

I want to display an image that each pixel appears randomly, the final result is an image without noise. I have tried this, but the result is not what I expected, How should I fix it?

img=ExampleData[{"TestImage","Lena"}];
data=ImageData[img];
{w,h}=ImageDimensions[img];
constArr=ConstantArray[N@{1,1,1},{h,w}];

Manipulate[constArr[[1;;i,1;;j]]=RandomSample@data[[1;;i,1;;j]];Image[constArr],{i,1,h,1},{j,1,w,1}]

enter image description here

$\endgroup$
2
  • $\begingroup$ You can do many pixels at once with img = ExampleData[{"TestImage", "Lena"}]; dims = ImageDimensions[img]; rimg = RandomImage[1, dims]; Manipulate[ ImageAdd[Binarize[rimg, t], img] , {t, 0, 1}]. Note the final image is completely free of noise. But the problem with displaying it pixel-by-pixel is that there are 262144 frames in the animation because it's one frame for every pixel. I tried iteratively applying ReplacePixelValue on a shuffled list of pixel positions, but Mathematica is extremely slow. $\endgroup$
    – flinty
    Commented Oct 18, 2020 at 16:25
  • 1
    $\begingroup$ Please consider a different example. Here's why: losinglena.com. $\endgroup$
    – JimB
    Commented Oct 18, 2020 at 19:34

1 Answer 1

2
$\begingroup$

Here is one of many possible approaches:

img = ExampleData[{"TestImage", "Mandrill"}];
data = ImageData[img];
{w, h} = ImageDimensions[img];

(* Total number of images *)
nImages = 100;

(* Random choice for which pixels are on or off for each image *)
r = Table[RandomVariate[BernoulliDistribution[i/(i + 1)], {w, h}], {i, nImages}];

(* All white for first image *)
r[[1]] = ConstantArray[0, {w, h}]; 

(* Full picture for last image *)
r[[nImages]] = ConstantArray[1, {w, h}]; 

(* Determine the pixels turned to white for each image so that pixels turned on 
   in one image stay on in the next *)
Do[r[[i]] = r[[i + 1]]*r[[i]], {i, nImages - 1, 2, -1}];

(* Create images *)
images = Table[Image[(r[[i]]*data) /. {0., 0., 0.} -> {1, 1, 1}], {i, 1, nImages}];

ListAnimate@images

One image from list of images

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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