Finding a sub-image within an image (template match) lazarus / 39-Graphics ---------------------------------------------------------------------- JonBondy · 2025-08-18 12:45 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 · 2025-08-18 16:08 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-> ---------------------------------------------------------------------- Martin_fr · 2025-08-18 16:29 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 · 2025-08-18 16:33 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  bmp: TBitmap;  Rd, Rs: TRect;   // Rectangle of subimage in destination and source image, respectively.  P: TPoint;  // Left/top corner of subimagebegin  bmp := TBitmap.Create;  try    bmp.PixelFormat := Image1.Picture.Bitmap.PixelFormat;    bmp.SetSize(Image2.Width, Image2.Height);    Rd := Rect(0, 0, bmp.Width, bmp.Height);    P := Point(350, 150);    Rs := Rect(P.X, P.Y, P.X + bmp.Width, P.Y + bmp.Height);    bmp.Canvas.CopyRect(Rd, Image1.Picture.Bitmap.Canvas, Rs);    Image2.Picture.Assign(bmp);    bmp.SaveToFile('subimage.bmp');  finally    bmp.Free;  end;end;  See attachment. ---------------------------------------------------------------------- gidesa · 2025-08-18 18:24 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  ) ---------------------------------------------------------------------- JonBondy · 2025-08-18 21:33 @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 · 2025-08-19 10:30 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 · 2025-08-19 11:16 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 · 2025-08-19 16:58 Quote from: JonBondy on August 19, 2025, 01:16:43 pmThank 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 · 2025-08-19 19:15 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 ---------------------------------------------------------------------- gidesa · 2025-08-19 19:28 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 · 2025-08-20 14:53 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-> ---------------------------------------------------------------------- LeP · 2025-08-20 17:22 @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 · 2025-08-20 17:30 Quote from: Dzandaa on August 20, 2025, 04:53:47 pmIf 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_templat e_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.