# Reconstructing an image from overlapping patches

The Mathematica function ImagePartition can create overlapping sub-images with the pixel offset options {dw, dh}:

ImagePartition[image, {w, h}, {dw, dh}]


Meanwhile, the ImageAssemble function has no such option documented.

Hence my question is: how would one reconstruct an image made up of overlapping patches?

• Is there an "easy" way?
• Or do I need to work through the list of patches one-by-one?

Update:

If I want each overlapping patch to contribute equally to the value of a single pixel in the final image, what would I do?

(Sorry, should have been clearer - have changed the title)

-
Welcome to our dedicated Mathematica site. If you like an answer, and you surely do because you accepted one, then you should definitely upvote the answer too. Currently, you cannot do this because you don't have enough reputation points, but once you get there, don't forget it. –  halirutan Mar 31 '14 at 10:06
Don't worry, I will do so! –  blochwave Mar 31 '14 at 10:07

I think this could be what you're looking for. First let's create a list of patches:

img = ExampleData[{"TestImage", "Lena"}];
patches = ImagePartition[img, 256, {128, 128}]


I'm not going to use ImageAssemble to put these back together. The trick is to create black and white masks, one for each patch. This is one example:

And then to use ImageMultiply to create the following intermediate step:

Now since black is {0,0,0} and each image has a the right dimensions we can add them all together using ImageAdd. The implementation looks like this:

img = ExampleData[{"TestImage", "Lena"}];
{w, h} = ImageDimensions[img];
masks[{w_, h_}, {partW_, partH_}, {offsetX_, offsetY_}] :=
Flatten@Table[
Graphics[{
Black, Rectangle[{0, 0}, {w, h}],
White, Rectangle[{offx, offy}, {offx + partW, offy + partH}]
}, ImageSize -> {w, h},
Method -> {"ShrinkWrap" -> True}], {offx, 0,
offsetX Floor[(w - partW)/offsetX], offsetX}, {offy, 0,
offsetY Floor[(h - partH)/offsetY], offsetY}
];
]


And can be used like this:

patchesCombine[
patches,
]


The arguments supplied to masks are the same arguments that you would supply to ImagePartition.

-
Yes, that's what I was looking for, thank you! –  blochwave Mar 31 '14 at 9:58
ip = ImagePartition[ExampleData[{"TestImage", "Lena"}], 40, {10, 10}] ;
ImageAssemble[Map[ImageCrop[#, {10, 10}] &, ip, {2}]]


-
What about if I want each of the overlapping patches to contribute equally to a pixel? –  blochwave Mar 31 '14 at 8:20
Unfortunately your result is smaller than the original image, so even without considering overlapping patches it's not really the inverse of the ImagePartition operation. –  Rahul Mar 31 '14 at 8:24
I've changed the question title! –  blochwave Mar 31 '14 at 8:27
(And sorry for making the question unclear at first @belisarius) –  blochwave Mar 31 '14 at 14:49
@blochwave No problem. I'll leave the answer anyway. Somebody may find it useful in the future for a slightly different problem –  belisarius Mar 31 '14 at 14:51