Recent

Author Topic: Finding a sub-image within an image (template match)  (Read 4682 times)

JonBondy

  • New Member
  • *
  • Posts: 34
Finding a sub-image within an image (template match)
« on: August 18, 2025, 02:45:50 pm »
Given a large image, I want to find a sub-image within the larger one.

Are there facilities within Lazarus (or libraries) to support approaches such as this, in Python?

https://stackoverflow.com/questions/17566752/how-to-find-subimage-using-the-pil-library

Dzandaa

  • Hero Member
  • *****
  • Posts: 520
  • From C# to Lazarus
Re: Finding a sub-image within an image (template match)
« Reply #1 on: August 18, 2025, 06:08:32 pm »
Hi,

@JonBondy:

Does the sub-image have the same properties (size) as the image to be searched for?
Will the image to search for in the big image always be the same or can it be anything?

B->
Regards,
Dzandaa

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12131
  • Debugger - SynEdit - and more
    • wiki
Re: Finding a sub-image within an image (template match)
« Reply #2 on: August 18, 2025, 06:29:30 pm »
Well the python example seems to use opencv.

There is a thread here about opencv with fpc. https://forum.lazarus.freepascal.org/index.php?topic=52213.0

I haven't looked any further. Don't know if it will work out of the box. Also don't know how high or low level you must adapt the code to call the relevant API.

wp

  • Hero Member
  • *****
  • Posts: 13358
Re: Finding a sub-image within an image (template match)
« Reply #3 on: August 18, 2025, 06:33:27 pm »
Not 100% sure what you mean with "subimage"... I'd use the CopyRect method of the canvas on which the image is drawn.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bmp: TBitmap;
  4.   Rd, Rs: TRect;   // Rectangle of subimage in destination and source image, respectively.
  5.   P: TPoint;  // Left/top corner of subimage
  6. begin
  7.   bmp := TBitmap.Create;
  8.   try
  9.     bmp.PixelFormat := Image1.Picture.Bitmap.PixelFormat;
  10.     bmp.SetSize(Image2.Width, Image2.Height);
  11.     Rd := Rect(0, 0, bmp.Width, bmp.Height);
  12.     P := Point(350, 150);
  13.     Rs := Rect(P.X, P.Y, P.X + bmp.Width, P.Y + bmp.Height);
  14.     bmp.Canvas.CopyRect(Rd, Image1.Picture.Bitmap.Canvas, Rs);
  15.     Image2.Picture.Assign(bmp);
  16.     bmp.SaveToFile('subimage.bmp');
  17.   finally
  18.     bmp.Free;
  19.   end;
  20. end;
  21.  
See attachment.

gidesa

  • Full Member
  • ***
  • Posts: 218
Re: Finding a sub-image within an image (template match)
« Reply #4 on: August 18, 2025, 08:24:11 pm »
The Python example use Opencv (import CV2).
But there is a complete wrapper to use all Opencv functions from FPC/Delphi:

https://github.com/gidesa/ocvWrapper46

(Disclaimer: I am the author  :))
« Last Edit: August 18, 2025, 08:27:55 pm by gidesa »

JonBondy

  • New Member
  • *
  • Posts: 34
Re: Finding a sub-image within an image (template match)
« Reply #5 on: August 18, 2025, 11:33:27 pm »
@gidesa: Thank you.  This looks like it may be what I want. 

Excuse my newbie-ness, but I only know how to install packages using the Online Package Manager.  How would I use your data to install something useful on my Windows Lazarus machine, please?

Jon

gidesa

  • Full Member
  • ***
  • Posts: 218
Re: Finding a sub-image within an image (template match)
« Reply #6 on: August 19, 2025, 12:30:16 pm »
First, see install instructions for Windows in github repository.
It's not a specific Lazarus or FPC package, so it's not cataloged in OPM.
Important: remember to set the paths as indicated in instructions.
Then you can open the various Lazarus examples and see how they work.
If you want, you can also install some components for Lazarus. See specific
instructions in file Readme inside "Lazarus-components" directory.
 

JonBondy

  • New Member
  • *
  • Posts: 34
Re: Finding a sub-image within an image (template match)
« Reply #7 on: August 19, 2025, 01:16:43 pm »
Thank you.  The main Read Me was so long, I initially gave up on finding installation instructions.

When I attempt to download your repository, I see the attached error (I hope it is there: I can't see it in Preview).


gidesa

  • Full Member
  • ***
  • Posts: 218
Re: Finding a sub-image within an image (template match)
« Reply #8 on: August 19, 2025, 06:58:41 pm »
Thank you.  The main Read Me was so long, I initially gave up on finding installation instructions.

When I attempt to download your repository, I see the attached error (I hope it is there: I can't see it in Preview).

I am sorry. It's a normal Github repository, I don't know why your pc raise that error.
I have tried to download, without no error. See image.


JonBondy

  • New Member
  • *
  • Posts: 34
Re: Finding a sub-image within an image (template match)
« Reply #9 on: August 19, 2025, 09:15:09 pm »
The PC raises the error because more than one of your files appear to have virus signatures, as determined by AVG anti-virus.

I finally got it installed into Lazarus.  What is the best example/demo for the Object Detector, please.

Jon
« Last Edit: August 19, 2025, 09:29:43 pm by JonBondy »

gidesa

  • Full Member
  • ***
  • Posts: 218
Re: Finding a sub-image within an image (template match)
« Reply #10 on: August 19, 2025, 09:28:50 pm »
Maybe your antivirus place only these exe in quarantine, as usual. But they are not important, you can delete all
exe and recompile with your Lazarus installation.
Instead wrapper Dll are very important, you could recompile them only with a C++ compiler as Visual C++.

Dzandaa

  • Hero Member
  • *****
  • Posts: 520
  • From C# to Lazarus
Re: Finding a sub-image within an image (template match)
« Reply #11 on: August 20, 2025, 04:53:47 pm »
Hi,

@JonBondy:

Here is a Little program I wrote today to search sub images (of the same size and colors) in a larger image I use BGRABitmap.

If you try to detect image of different size, I think you must wrote a DNN Network (OpenCv use that)

You can look at the CAI library for Pascal From Joao Paulo Schuler https://github.com/joaopauloschuler/neural-api

Good luck

B->
Regards,
Dzandaa

LeP

  • Full Member
  • ***
  • Posts: 138
Re: Finding a sub-image within an image (template match)
« Reply #12 on: August 20, 2025, 07:22:34 pm »
@Dzandaa
Typically, a search for "equality" of individual points in a base region is not what is required.

Search techniques are varied and depend on what is being searched for and where it is being found.
There are a multitude of algorithms (some very advanced and customized ones can even be created using basic computer vision functions), but the most commonly used are:

(we're talking about 2D technologies)

- Pattern matching;

- Classifiers (not DL) such as:
   a) MLP (Multi-Layer Perceptrons);
   b) SVM (Support-Vector Machines);
   c) GMM (Gaussian Mixture Models);
   d) KNN (K-Nearest Neighbors);

- Deep learning and CNNs (Convolutional Neural Networks);

- OCR (for character recognition) with or without CNNs;

What @Dzandaa is referring to is deep learning with CNNs, and it's the most challenging and expensive option.
It requires fairly powerful hardware to train the network (i.e., the database), unless you want to train the network in hours or days... or more.

Depending on your needs, try delving deeper into what I wrote above and check out what OpenCV offers.

I'll warn you right away that you'll need to spend many days studying and experimenting.

If you choose deep learning, I also recommend checking out the Python-based YOLO. It can help you generate the ONNX network (i.e., perform the training) and then you can use Lazarus and OpenCV for the runtime (i.e., the search).

Unless OpenCV has something ready-made.

I use paid (very expensive) computer vision libraries and don't use OpenCV.

Good luck for your work.

gidesa

  • Full Member
  • ***
  • Posts: 218
Re: Finding a sub-image within an image (template match)
« Reply #13 on: August 20, 2025, 07:30:02 pm »
If you try to detect image of different size, I think you must wrote a DNN Network (OpenCv use that)

Indeed Opencv doesn't use a neural network in the MatchTemplate function (the one used in Python example). It uses algorithm of "classic" computer vision.
See details here:https://docs.opencv.org/4.6.0/de/da9/tutorial_template_matching.html
Neural networks are used to do objects detection, that is a much more powerful technique, compared to template matching.
Anyway Opencv supports also neural networks, with dedicated classes.

 

TinyPortal © 2005-2018