Object-recognition in images using multiple templates
Multi-Template-Matching is a package to perform object-recognition in images using one or several smaller template images.
The template and images should have the same bitdepth (8,16,32-bit) and number of channels (single/Grayscale or RGB).
The main function
MTM.matchTemplates returns the best predicted locations provided either a score_threshold and/or the expected number of objects in the image.
Using pip in a python environment,
pip install Multi-Template-Matching
import MTMshould work.
Example jupyter notebooks can be downloaded from the tutorial folder of the github repository and executed in the newly configured python environement.
The package MTM contains mostly 2 important functions:
matchTemplates(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=float("inf"), score_threshold=0.5, maxOverlap=0.25, searchBox=None)
This function searches each template in the image, and return the best N_object location which offer the best scores and which do not overlap above the
list of tuples (LabelString, Grayscale or RGB numpy array) templates to search in each image, associated to a label
image : Grayscale or RGB numpy array
image in which to perform the search, it should be the same bitDepth and number of channels than the templates
method : int
one of OpenCV template matching method (0 to 5), default 5=0-mean cross-correlation
expected number of objects in the image
score_threshold: float in range [0,1]
if N>1, returns local minima/maxima respectively below/above the score_threshold
maxOverlap: float in range [0,1]
This is the maximal value for the ratio of the Intersection Over Union (IoU) area between a pair of bounding boxes. If the ratio is over the maxOverlap, the lower score bounding box is discarded.
searchBox : tuple (X, Y, Width, Height) in pixel unit
optional rectangular search region as a tuple
- Pandas DataFrame with 1 row per hit and column "TemplateName"(string), "BBox":(X, Y, Width, Height), "Score":float
- if N=1, return the best match independently of the score_threshold
- if N<inf, returns up to N best matches that passed the score_threshold
- if N=inf, returns all matches that passed the score_threshold
findMatches performs the same detection without the Non-Maxima Supression.
The 2nd important function is
drawBoxesOnRGB to display the detections as rectangular bounding boxes on the initial image.
To be able to visualise the detection as colored bounding boxes, the function return a RGB copy of the image if a grayscale image is provided.
It is also possible to draw the detection bounding boxes on the grayscale image using drawBoxesOnGray (for instance to generate a mask of the detections).
drawBoxesOnRGB(image, hits, boxThickness=2, boxColor=(255, 255, 00), showLabel=True, labelColor=(255, 255, 0), labelScale=0.5 )
This function returns a copy of the image with predicted template locations as bounding boxes overlaid on the image The name of the template can also be displayed on top of the bounding boxes with showLabel=True.
image : numpy array
image in which the search was performed
(pandas DataFrame) hits as returned by matchTemplates or findMatches
thickness of bounding box contour in pixels. -1 will fill the bounding box (useful for masks).
boxColor: (int, int, int)
RGB color for the bounding box
showLabel: Boolean, default True
Display label of the bounding box (field TemplateName)
labelColor: (int, int, int)
RGB color for the label
labelScale: float, default=0.5 scale for the label sizes
- outImage: RGB image
original image with predicted template locations depicted as bounding boxes
If you use this implementation for your research, please cite:
Multi-Template Matching: a versatile tool for object-localization in microscopy images;
Laurent SV Thomas, Jochen Gehrig
bioRxiv 619338; doi: https://doi.org/10.1101/619338
Origin of the work
This work has been part of the PhD project of Laurent Thomas under supervision of Dr. Jochen Gehrig at:
ACQUIFER a division of DITABIS AG
Digital Biomedical Imaging Systems AG
Freiburger Str. 3
This project has received funding from the European Unionâ€™s Horizon 2020 research and innovation program under the Marie Sklodowska-Curie grant agreement No 721537 ImageInLife.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size Multi_Template_Matching-1.5.1-py3-none-any.whl (23.7 kB)||File type Wheel||Python version py3||Upload date||Hashes View hashes|
|Filename, size Multi-Template-Matching-1.5.1.tar.gz (10.1 kB)||File type Source||Python version None||Upload date||Hashes View hashes|
Hashes for Multi_Template_Matching-1.5.1-py3-none-any.whl
Hashes for Multi-Template-Matching-1.5.1.tar.gz