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?


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)

  • 2
    $\begingroup$ 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. $\endgroup$
    – halirutan
    Mar 31, 2014 at 10:06
  • $\begingroup$ Don't worry, I will do so! $\endgroup$ Mar 31, 2014 at 10:07

2 Answers 2


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:

Image mask

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

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_}] := 
      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}
patchesCombine[patches_, masks_] := Module[{masked},
  masked = ImageMultiply[img, #] & /@ masks;
  Fold[ImageAdd, First@masked, Rest@masked]

And can be used like this:

 masks[ImageDimensions[img], {256, 256}, {128, 128}]


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

  • $\begingroup$ Yes, that's what I was looking for, thank you! $\endgroup$ Mar 31, 2014 at 9:58
ip = ImagePartition[ExampleData[{"TestImage", "Lena"}], 40, {10, 10}] ;
ImageAssemble[Map[ImageCrop[#, {10, 10}] &, ip, {2}]]

Mathematica graphics

  • $\begingroup$ What about if I want each of the overlapping patches to contribute equally to a pixel? $\endgroup$ Mar 31, 2014 at 8:20
  • $\begingroup$ 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. $\endgroup$
    – user484
    Mar 31, 2014 at 8:24
  • $\begingroup$ I've changed the question title! $\endgroup$ Mar 31, 2014 at 8:27
  • $\begingroup$ (And sorry for making the question unclear at first @belisarius) $\endgroup$ Mar 31, 2014 at 14:49
  • 1
    $\begingroup$ @blochwave No problem. I'll leave the answer anyway. Somebody may find it useful in the future for a slightly different problem $\endgroup$ Mar 31, 2014 at 14:51

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.