overlapping

Hey guys, its me again... I managed to combine 2 images together, but the problem I am facing right now is that if the 2 images overlap, I need to make an algorithm to average the overlapping pixel values
Does anybody have a clue, how I could average the pixel values
Many Thanks.
Hi Upo!
I hope this isn't too late.Yes it's very easy.So you want to have an image wich is sligtly transparent over the other, wright??This is what I understand from your question.
If that's what you want than it's really easy.This is the principle:
say you have 2 images,for this example, both the same size.So you have Image1 and Image2.
Let's say both images have the size WxH, where W is the width and H the height.These images both have a number of WxH pixels.So Image1 has pixel1_ij, and Image2 has pixel2_ij, where pixel_ij is the pixel at the i and j cooedinates from the image.Of course, 0<=i<W and 0<=j<H.
To blen the images you actualy have to blend the pixels corespendently.Like this:
1
2
3
4
5
6
7
for(i=0;i<W;i++)
{
   for(j=0;j<H;j++)
   {
      pixel3 [i][j] = ( pixel1 [i][j] + pixel2 [i][j] )/2;
   }
}


As you can see the third image, Image3 is obtained pixel by pixel by making an average between the other 2 coresponding pixels of the other 2 images.This produces an efect in wich both images have the same transparancy, that is, both are as important.The method above introduces a third image.If you don't want that you can just change the values of one of the 2 images, for example image 1.Like this:
1
2
3
4
5
6
7
for(i=0;i<W;i++)
{
   for(j=0;j<H;j++)
   {
      pixel2 [i][j] = ( pixel1 [i][j] + pixel2 [i][j] )/2;
   }
}

Anyway, maybe you want one of the images to be more or less transparent over the other.For this you must use the weight mean or wathever you call it in English.This is the formula:
x3=( x1*p1 + x2*p2)/(p1 + p2) , where:

x3 - the mean betwwent x1 and x2
p1 - the weight or importance of x1
p2 - the weight or importance of x2

If you want to give the transparency in percentages, like 30% transparent, then you must understand that p1+p2 represents 100%, and of course
p1= 100% - p2

So if you want the formula where image1 is 30% transparent , that means that image1 has an importance of 30%, so the formula is:
1
2
3
x3= ( 30*x1 + 70*x2 )/100
and the generelazid formula is:
x3= ( x1*p + x2*(100-p) )/100

Remember: 30 + 70 = 100, if you use 30, 70 and 110 you get unrealistic effects.

Very important!!!
In the real world you well have to make the average between the RGB valus of the pixels, not between the pixels's values themselfs.Otherwise it won't look realistic.It will look interesting dough.So you would have to do this:
1
2
3
4
5
6
7
8
9
for(i=0;i<W;i++)
{
   for(j=0;j<H;j++)
   {
      pixel3_R [i][j] = ( pixel1_R [i][j] + pixel2_R [i][j] )/2;
      pixel3_G [i][j] = ( pixel1_G [i][j] + pixel2_G [i][j] )/2;
      pixel3_B [i][j] = ( pixel1_B [i][j] + pixel2_B [i][j] )/2;
   }
}


If you're interested in these kind of image blending than you could play around with different formulas, like:
 
pixel3 = ln( pixel1 ) + sin( pixel2 )

Who knows what interesting effect this blending will bring.Of course, you would have to calculate the actual RGB values, but i give just a simple formula.Remember: always calculate the RGB values separetly, do not compute the value of the pixel itself, first decompose the pixel in the RGB values and compute these 3 values for every pixel.That is if you want realistic effects.

Please reply if you have read my message, because i know i answered your question a litlle late, so i want to know if you have at least read it.Sorry if i was too late.
The 2 images are vector images yes? If they are bitmap I dont think you can do it if I understand your question correctly.
They are 2D arrays.It doesn't matter.You probally use a function that reads an image file and returns either an 1D arrary, a 2D arrary or there vector equivalents(1D vector, 2D vector).
My help was shoing you how to blend 2 images.You have to adjust the code to wathever the functions that read an image return.But you can do that.It's easy.
Good luck!
Or your functions could return the RGB values directly for each pixel, in this case you would definatly have an 1D array or 1D vector which have WxHx3 elements (3 values for each pixel, R,G and B).
Topic archived. No new replies allowed.