Forum > Graphics

Fast way of comparing 2 bitmaps

(1/5) > >>

Pi:
Do you know one of the fastest ways of comparing 2 bitmaps colour-wise?

eg. in this case I have 2 folders of bmp sequences from videos, and I want to go through the bitmaps in folder 1 looking for the closest bitmap to it in folder 2 (eg. the one that looks the same/nearly the same).
There's 642 bmps in folder 1 and 814 bmps in folder 2.

I'm currently loading each image into an image control (so 2 image controls on a form) and doing a for loop through the pixels and looking at the colour
eg. 


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---col_1:=image1.Picture.Bitmap.Canvas.Pixels[x,y]; Then using getrvalue, getgvalue etc. to find the differences (using abs to make it a positive number) between the rgb values at each pixel position, totalling them up for an image comparison and storing the image nr with the lowest difference.

The images are 1080x1920 and it takes a long time to process every pixel (eg. if I compared every pixel of every image in folder 1 with every image in folder 2 I think it would take years to complete).
I changed it so it skips pixels (so in the for loop if it's not divisible by a certain number then go back to the loop. That will reduce precision but it still isn't fast enough with it just looking at every 100 pixels in the x & y.

Is there a faster way? Can someone please give me a hint how to do this using scanline if that will be a lot faster? Is using scanline the fastest way? It might be that comparing the grey-scale values might be good enough if comparing all the red, green and blue values will be too slow.

winni:
Hi!

Install the BGRAbitmapPack available through the Online Package Manager.

This code creates copies of image1 and image2.
Then it compares the raw data of the two bitmaps and the difference is available in  dred,dgreen,dblue and dalpha.
There you have to put your code for the difference.

BGRAbitmap is not optimized for speed, but it is explicit faster than the TCanvas which is realy slow.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses ....,BGRABitmap, BGRABitmapTypes;..... procedure TForm1.Button1Click(Sender: TObject);var bmp1,bmp2 : TBGRAbitmap;    p1,p2:PBGRAPixel;    i : integer;    dred,dgreen,dblue, dalpha : Integer;begin  bmp1 := TBGRAbitmap.create(image1.Picture.Bitmap);  bmp2 := TBGRAbitmap.create(image2.Picture.Bitmap);  if bmp1.NbPixels <> bmp2.NbPixels then    begin    showMessage ('Different Dimensions!');    exit;    end;  p1 := bmp1.data;  p2 := bmp2.data;  for i := 0 to bmp1.NbPixels-1 do     begin     dred := p2^.red -p1^.red;     dgreen := p2^.green -p1^.red;     dblue := p2^.blue -p1^.blue;     dalpha := p2^.alpha -p1^.alpha;      .....     inc(p1);     inc(p2)     end;  bmp1.free;  bmp2.free;end;
Winni

Pi:
Thanks a lot. I'm installing that now and will try that code.

edit: Thanks a lot. I've used and edited the code (after doing the online install and adding it to one of the package things) and it seems to be working now, faster than before. Thank you.

furious programming:
If you want to iterate very fast through bitmap pixels, just use TBitmap.ScanLine and compare pixel by pixel using pointers.

dje:
You could automate the imagemagick command line tools if you don't want to code it from scratch.

https://legacy.imagemagick.org/Usage/compare/#compare
https://imagemagick.org/script/compare.php
https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=29587
https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=30175
http://www.fmwconcepts.com/imagemagick/similar/index.php

edit: There is a ton of information if you google "find similar images using imagemagick"

It looks like imagemagick handles all kinds of variations in images, including size, color, images in images, etc.

http://www.imagemagick.org/discourse-server/viewtopic.php?t=32522

Navigation

[0] Message Index

[#] Next page

Go to full version