 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Paul Guest
|
Posted: Mon Nov 22, 2004 8:12 pm Post subject: Difference between bitmaps |
|
|
Hi all,
I am searching for a fast way to get the difference between two bitmaps.
I have found the (free) TBitmap32 component, which should be much faster
than the standard bitmap,
but it's not easy to start with.
Does anyone here know another fast method ?
Paul
|
|
| Back to top |
|
 |
Pavel Vymazal Guest
|
Posted: Mon Nov 22, 2004 9:22 pm Post subject: Re: Difference between bitmaps |
|
|
What do you mean by "difference"?
A possible solution would be to draw one of the bitmaps using CopyRect with
Mode set to COPY, then draw the second one over it with Mode set to XOR.
This way, equal pixels would become BLACK, while differing pixels will
become (RGB-wise) colored as the RGB difference between the two pixels.
The question is if this kind of "difference information" is helpful.
"Paul" <paul.blommaerts (AT) telenet (DOT) be> píše v diskusním příspěvku
news:41a2481e (AT) newsgroups (DOT) borland.com...
| Quote: | Hi all,
I am searching for a fast way to get the difference between two bitmaps.
I have found the (free) TBitmap32 component, which should be much faster
than the standard bitmap,
but it's not easy to start with.
Does anyone here know another fast method ?
Paul
|
|
|
| Back to top |
|
 |
Nils Haeck Guest
|
Posted: Tue Nov 23, 2004 12:19 am Post subject: Re: Difference between bitmaps |
|
|
Hello Paul,
What kind of difference? Do you want to just flag pixels that are different
(only even by one bit) or do you want some kind of quality factor, where 0
means no difference and the bigger the value the bigger the difference.
In first case, you can work with any type of bitmap and simply compare
pixels. If they're equal, you flag them (or increment a counter).
In second case, easiest to do is to use RGB bitmaps, and take the absolute
or square of pixel channel value differences.
| Quote: | I am searching for a fast way to get the difference between two bitmaps.
I have found the (free) TBitmap32 component, which should be much faster
than the standard bitmap,
|
Not neccesarily. But certainly true if you were planning to use the Pixels
property.
Here's some code for (standard) TBitmaps with PixelFormat = pf24bits. I just
wrote it down without checking, so there might be errors:
function BitmapDifference(BmpA, BmpB: TBitmap): double;
var
x, y: integer;
P, Q: PByte;
Diff: int64;
begin
if not assigned(BmpA) or not assigned(Bmp)
or (BmpA.PixelFormat <> pf24bit) or (BmpB.PixelFormat <> pf24bit)
or (BmpA.Width <> BmpB.Width) or (BmpA.Height <> BmpB.Height)
or (BmpA.Width * BmpA.Height = 0) then
raise Exception.Create('Cannot compare bitmaps');
Diff := 0;
for y := 0 to BmpA.Height - 1 do begin
P := BmpA.Scanline[y];
Q := BmpB.Scanline[y];
for x := 0 to BmpA.Width - 1 do begin
Diff := Diff + Sqr(P^ - Q^); inc(P); inc(Q);
Diff := Diff + Sqr(P^ - Q^); inc(P); inc(Q);
Diff := Diff + Sqr(P^ - Q^); inc(P); inc(Q);
end;
end;
Result := Sqrt(Diff / (BmpA.Width * BmpA.Height));
end;
This outputs differences as mean euclidian distance in RGB space averaged
over pixel count. Routine should be pretty fast.
A point not touched by this is comparing bitmaps that are actually not
aligned to each other, or different in scale or rotation. If you want to do
that, you face a much more difficult problem, for which my company has a
solution (demo available).
Kind regards,
Nils Haeck
www.simdesign.nl
"Paul" <paul.blommaerts (AT) telenet (DOT) be> wrote
| Quote: | Hi all,
I am searching for a fast way to get the difference between two bitmaps.
I have found the (free) TBitmap32 component, which should be much faster
than the standard bitmap,
but it's not easy to start with.
Does anyone here know another fast method ?
Paul
|
|
|
| Back to top |
|
 |
Paul Guest
|
Posted: Tue Nov 23, 2004 5:35 pm Post subject: Re: Difference between bitmaps |
|
|
Hi all,
I am trying to send image-streams to a host over the internet.
Instead of sending the complete bitmaps, I am searching for the
fastest way to do this.
The code I use takes a new snapshot ,compares it
with the preveous image and inverts it.
This way, I have the changes of the last snapshot inverted.
The rest of the image is black.
After compression, the inverted image wich contains all
changed pixels is transferred to the host.
I have already speeded up the compression part,
but I find the ' graphics part ' too slow.
The code I use now
bmp:= TBitmap.Create;
GetPic(bmp);
dif:= TBitmap.Create;
dif.Assign(bmp);
R:= Tect(0, 0, dif.Width, dif.Height);
dif.Canvas.CopyMode:= cmSrcInvert;
dif.Canvas.CopyRect(R, curbmp.Canvas, R); //curbmp is the previous
snapshot
curbmp.Assign(bmp);
curbmp.Dormant;
curbmp.FreeImage;
Paul
|
|
| Back to top |
|
 |
Nils Haeck Guest
|
Posted: Tue Nov 23, 2004 10:36 pm Post subject: Re: Difference between bitmaps |
|
|
That shouldn't be so slow..
But make sure that you also time your GetPic() routine, perhaps that is the
bottleneck.
Also, make sure that all bitmaps use the same PixelFormat.
What is your maximum achievable framerate?
If you want to speed things up you could use device independent bitmaps and
then use MMX to do the pixel diffing. Have a look at Graphics32 for
low-level MMX examples.
Kind regards,
Nils Haeck
www.simdesign.nl
"Paul" <paul.blommaerts (AT) telenet (DOT) be> wrote
| Quote: | Hi all,
I am trying to send image-streams to a host over the internet.
Instead of sending the complete bitmaps, I am searching for the
fastest way to do this.
The code I use takes a new snapshot ,compares it
with the preveous image and inverts it.
This way, I have the changes of the last snapshot inverted.
The rest of the image is black.
After compression, the inverted image wich contains all
changed pixels is transferred to the host.
I have already speeded up the compression part,
but I find the ' graphics part ' too slow.
The code I use now
bmp:= TBitmap.Create;
GetPic(bmp);
dif:= TBitmap.Create;
dif.Assign(bmp);
R:= Tect(0, 0, dif.Width, dif.Height);
dif.Canvas.CopyMode:= cmSrcInvert;
dif.Canvas.CopyRect(R, curbmp.Canvas, R); //curbmp is the previous
snapshot
curbmp.Assign(bmp);
curbmp.Dormant;
curbmp.FreeImage;
Paul
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|