views:

74

answers:

1

I'm looking for some algorithm to joint objects, for example, combine an apple into a tree in digital image and some demo in Matlab. Please show me some materials of that. Thanks for reading and helping me!!!

+1  A: 

I not sure if I undertand your question, but if you are looking to do some image overlaping, as does photoshop layers, you can use some image characteristics to, through that characteristc, determine the degree of transparency.

For example, consider using two RGB images. Image A will be overlapped by image B. To do it, we'll use image B brightness to determine transparency degree (255 = 100%). Intensity = pixel / 255;

NewPixel = (PixelA * (1 - Intensity)) + (PixelB * Intensity);

As intensity is a percentage and each pixel is multiplied by the complement of this percentage, the resulting sum will never overflow over 255 (max graylevel)

int WidthA = imageA.Width * channels;
int WidthB = imageB.Width * channels;

int width = Min(ImageA.Width, ImageB.Width) * channels;
int height = Min(ImageA.Height, ImageB.Height);

byte *ptrA = imageA.Buffer;
byte *ptrB = imageB.Buffer;

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x += channels, ptrA += channels, ptrB += channels)
    {

  //Take the intensity of the pixel. If RGB (channels = 3), intensity = (R+B+G) / 3. If grayscale, the pixel value is intensity itself
  int avg = 0;
        for (int j = 0; j < channels; ++j)
        {
            avg += ptrB[j];
        }

        //Obtain the intensity as a value between 0..100%
  double intensity = (double)(avg / channels) / 255;

        for (int j = 0; j < channels; ++j)
        {
   //Write in image A the resulting pixel which is obtained by multiplying Image B pixel 
   //by 100% - intensity plus Image A pixel multiplied by the intensity
            ptrA[j] = (byte) ((ptrB[j] * (1.0 - intensity)) + ((intensity) * ptrA[j]));
        }
    }

    ptrA = imageA.Buffer + (y * WidthA));
    ptrB = imageB.Buffer + (y * WidthB));
}

You can also change this algorithm in order to overlap Image A over B, in a different place. I'm assuming here the image B coordinate (0, 0) will overlap image A coordinate (0, 0).

But once again, I'm not sure if this is what you are looking for.

Andres
Thank Andres very much! What you said is nearly what I need. But I wonder that, in an image, there are many objects, for example, the chair, the table, the fridge... and I just want to overlap the apple on to the table, shall I have to determine the boundary of the table and apple? If yes, how can I do it?
3D
The first thing (or problem) you have to solve is: how do I determine the position of the object I want to overlap? Is it fixed, easy to find? Here you basically have a problem of "object segmentation", and this is not very simple.
Andres
Andres