I have about 1000 images of 1000px $\times$ 1000px and a list which contains the coordinates of the pixels to select, for each of the 1000 images. Each pixel is used once, so the final image is a full image made of parts of all the other images.
Here is an example code for 2 images:
img1 = ImageData[Import["ExampleData/lena.tif"]];
img2 = ImageData[ColorNegate[Import["ExampleData/lena.tif"]]];
img = img1;
ptsToReplace = RandomInteger[{1, 116}, 10000] // Partition[#, 2] &;
Table[img[[Sequence @@ ptsToReplace[[i]]]] =
img2[[Sequence @@ ptsToReplace[[i]]]], {i, 1,
Length[ptsToReplace]}];
ImageAssemble[Map[Image, {img1, img2, img}]]
Below are the two images and the output on the right which is a mix of them:
My question is about what strategy to use:
- Loading all the images, then converting all of them as arrays with
ImageData
and then building the final image by iterating on the pixel works, but is of course quickly limited by the memory as it stores everything (images + arrays). - I also tried to iterate on the images: for each image, get all the pixels that I will need to take, copy them to the final image, and proceed again with next image. This is OK as far as memory is concerned, but it is too slow.
- I thought about more efficient ways such as creating mask images which would have black dots on the coordinates of the pixels which have to be used for a given image, and then multiply the images. The goal being to avoid iteration on pixels and use faster build-in functions. But I don't know how to do this efficiently (e.g. not building the mask by iterating over the pixels).
What would be an efficient technique, in both CPU and memory? I insist that the coordinates to replace (ptsToReplace
here) are independent of the images, so I really have to use a table (for each image) of table of points as an input.