Search code examples
opencvoverlayjavacv

How to overlay an picture with a given mask


I want to overlay an image in a given image. I have created a mask with an area, where I can put this picture:

Image Hosted by ImageShack.us http://img560.imageshack.us/img560/1381/roih.jpg

The problem is, that the white area contains a black area, where I can't put objects.

How can I calculate efficiently where the subimage must to put on? I know about some functions like PointPolygonTest. But it takes very long.

EDIT:

The overlay image must put somewhere on the white place. For example at the place from the blue rectangle.

Image Hosted by ImageShack.us http://img513.imageshack.us/img513/5756/roi2d.jpg


Solution

  • If I understood correctly, you would like to put an image in a region (as big as the image) that is completely white in the mask.

    In this case, in order to get valid regions, I would apply an erosion to the mask using a kernel of the same size as the image to be inserted. After erosion, all valid regions will be white.

    The image you show however has no 200*200 regions that is entirely white, so I must have misunderstood...

    But if you what to calculate the region with the least black in the mask, you could apply a blur instead of an erosion and look for the maximal intensity pixel in the blurred mask.

    In both case you want to insert the sub-image so that its centre is on the position maximal intensity pixel of the eroded/blurred mask.

    Edit:

    If you are interested in finding the region that would be the most distant from any black pixel to put the sub-image, you can define its centre as the maximal value of the distance transform of the mask.

    Good luck,