8
$\begingroup$

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)

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

2 Answers 2

6
$\begingroup$

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

And can be used like this:

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

Result

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

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

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ What about if I want each of the overlapping patches to contribute equally to a pixel? $\endgroup$ Commented 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
    Commented Mar 31, 2014 at 8:24
  • $\begingroup$ I've changed the question title! $\endgroup$ Commented Mar 31, 2014 at 8:27
  • $\begingroup$ (And sorry for making the question unclear at first @belisarius) $\endgroup$ Commented 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$ Commented Mar 31, 2014 at 14:51

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.