Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: merge two images  (Read 4692 times)

klausbits

• New Member
• Posts: 29
merge two images
« on: July 07, 2016, 11:00:00 am »
Hi.

I want to interpolate between two images like this

Code: Pascal  [Select][+][-]
1. var picture1,picture2,picture3: Tpicture;
2. picture1:= Tpicture.create;
3. picture2:= Tpicture.create;
4. picture3:= Tpicture.create;
7.
8. picture3 = 0.7*picture1 + 0.3*picture2;
9.

how can I do this?

• Hero Member
• Posts: 10750
Re: merge two images
« Reply #1 on: July 07, 2016, 11:06:05 am »
You need to set transparency levels for those pictures so the one shines through the other.

klausbits

• New Member
• Posts: 29
Re: merge two images
« Reply #2 on: July 07, 2016, 11:26:56 am »
thanks for the tip. I have to think about this if it serves my purpose.

Rather I thought to have
red3:=0.7*red1+0.3*red2;
green3:=...
blue3:=...

• Hero Member
• Posts: 10750
Re: merge two images
« Reply #3 on: July 07, 2016, 11:35:39 am »
@Klausbits:
Yup, that is basically what transparency does <very big smile> so why re-invent wheels? I never got further than an octagonal one....

You got the algorithm almost spot on but think about weights of the values. Now let your drivers do the real work
« Last Edit: July 07, 2016, 11:41:23 am by Thaddy »

klausbits

• New Member
• Posts: 29
Re: merge two images
« Reply #4 on: July 07, 2016, 11:55:12 am »

I am not sure thsat this is the same (pardon me, I am a physicist;-)

How would you program your proposal?

circular

• Hero Member
• Posts: 3694
Re: merge two images
« Reply #5 on: July 07, 2016, 03:11:28 pm »
How to do that with BGRABitmap:

Code: Pascal  [Select][+][-]
1. uses BGRABitmap, BGRABitmapTypes;
2. var bmp1,bmp2,bmp3: TBGRABitmap;
3. begin
4.   bmp1 := TBGRABitmap.Create('d:\image1.jpg');
5.   bmp2 := TBGRABitmap.Create('d:\image2.jpg');
6.   bmp3 := TBGRABitmap.Create(bmp1.Width,bmp1.Height); //note that size may be different between bmp1 and bmp2
7.   bmp3.CrossFade(Rect(0,0,bmp3.Width,bmp3.Height), bmp1, bmp2, round(0.3*255), dmSet);
8.   bmp1.Free;
9.   bmp2.Free;
10.   ...
11. end;

It works with images that have transparent parts as well.
Conscience is the debugger of the mind

klausbits

• New Member
• Posts: 29
Re: merge two images
« Reply #6 on: July 07, 2016, 04:27:50 pm »
to circular:

thank you, I'll try it.

Meanwile I worked around with
colorm:=picm.bitmap.Canvas.Pixels[nw,nh];      //color of picm, but has only 256 colors :-(
or
fpcolorm1:=picm1.bitmap.Canvas.getColor(nw,nh);  //FPcolor of picm1 does not work :-(

klausbits

• New Member
• Posts: 29
Re: merge two images
« Reply #7 on: July 07, 2016, 08:43:43 pm »
many thanks for your tips. Here my own solution, I hope it works:
Code: Pascal  [Select][+][-]
1.   begin                //see diary 6.July 2016
2.     picm:= TPicture.Create;
3.     picm1:= TPicture.Create;
4.     for n:=0 to nwidth-1 do
5.     begin
6. //        picture2[n]:= TPicture.Create;   //already defined
7.       nr:=n;                 //real of n
8.       mr:=nr*nmax/nwidth;    //real of m
9.       m:=floor(mr);
10.       dmr:=mr-m;            //fade position
11.       Str(m:4,nstring);
12.       fastring:=files+'\b'+nstring+'.jpg';
13.       Str(m+1:4,nstring);
14.       fbstring:=files+'\b'+nstring+'.jpg';
15.       picm.LoadFromFile(fastring);      //image 1
16.       picm1.LoadFromFile(fbstring);     //image 2
17.       for nh:=0 to nheight-1 do
18.       begin
19.         for nw:=0 to nwidth-1 do        //for every pixel do
20.         begin
21.           colorm:=picm.bitmap.Canvas.Pixels[nw,nh];      //color of picm
22.           RedGreenBlue(colorm, mRed, mGreen, mBlue);     //bytes! -> only 512 different colors?
23.           colorm1:=picm1.bitmap.Canvas.Pixels[nw,nh];      //color of picm1
24.           RedGreenBlue(colorm1, m1Red, m1Green, m1Blue);
25. //          fpcolorm1:=picm1.bitmap.Canvas.getColor(nw,nh);  //FPcolor of picm1 does not work
26.           nred:=round((1-dmr)*mRed+dmr*m1Red);             //to be scaled by 256?  or 8?
27.           ngreen:=round((1-dmr)*mGreen+dmr*m1Green);
28.           nblue:=round((1-dmr)*mBlue+dmr*m1Blue);
29.  //         function RGBToColor(R, G, B: Byte): TColor;
30.           colorn:=RGBToColor(nred, ngreen, nblue);
31.           picture2[n].bitmap.Canvas.Pixels[nw,nh]:=colorn;   //image 3
32.         end;
33.       end;
34.     end;
35.     Str(n:4,nstring);
36.     fastring:=files+'\bb'+nstring+'.jpg';  // bb... different name to not overwrite b... files of user
37.     picture2[n].SaveToFile(fastring); //
38.     picture2[n].free; //
39.   end;
40.

circular

• Hero Member
• Posts: 3694
Re: merge two images
« Reply #8 on: July 07, 2016, 09:44:51 pm »
That's the idea.
Conscience is the debugger of the mind