Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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)

share|improve this question
2  
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 at 10:06
    
Don't worry, I will do so! –  blochwave Mar 31 at 10:07
add comment

2 Answers 2

up vote 6 down vote accepted

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.

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

Mathematica graphics

share|improve this answer
    
What about if I want each of the overlapping patches to contribute equally to a pixel? –  blochwave Mar 31 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 Narain Mar 31 at 8:24
    
I've changed the question title! –  blochwave Mar 31 at 8:27
    
(And sorry for making the question unclear at first @belisarius) –  blochwave Mar 31 at 14:49
1  
@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 at 14:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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