Skip to main content

UTTM(Unsupervised-Torch-Template-Matching) is a tool for robust 2D template matching based on torch unsupervised learning.

Project description

Unsupervised-Torch-Template-Matching

A repository for robust 2D template matching based on torch unsupervised learning.

Installation

pip install uttm

The main dependencies are:

  • numpy
  • torch
  • torchvision
  • opencv-python

Pipeline

1. Preprocess templates and segmentations

By default the binary template and segmentation images can aibitrary.

The preprocessing step will automatically turn a mask into a 512*512 image, where the center of minimum enclosing circle is at the image center and the radius of the circle is 128. The processing step will keep the infomation of padding, translation, rotation and scaling, so that we can restore the templates back to original images.

2. Compute statistics by unsupervised learning

3. Fine-tune the rotation by 2d-icp (optional)

4. Visualization for matching evaluation (optional)

Data Preparation

We provide the example data used for template matching, the input mainly

  • template images
  • segmentation image
  • (Optional) Origina image before segmentation, used only for visualization

For custom data, a user can either extract foreground mask through online platforms like https://www.fotor.com/features/background-remover/, or locally run segmentation model like Segment Anthing (https://huggingface.co/docs/transformers/model_doc/sam) or Birefnet(https://huggingface.co/ZhengPeng7/BiRefNet).

Parameters for our Template_Matcher class

Inputs

  • angle_per_rotation: angle for each rotation resolution for one template (e.g. if we define it as 10, we will have 36 preprocessed separate masks for one template). It's defined through class initialization or function reset_params()
  • template: a list of binary template masks. Defined through function get_templates
  • segmentation masks: a list of binary segmentation masks. Defined through function get_masks

Outputs

  • template_scores_for_segmentations: n * m array for n segmentations and m templates;
  • matching_info: matching infomation according to maximum score of per m-dimention array for n segmentations, including best matched template index, rotation wrt. input template, translation and scales of template to original segmentation image (represented by center position and radius of minimum enclosing circle).

How to use

Assume the inputs are in ./templates/, ./segmentations/.

import os

import cv2
import numpy as np
from matplotlib import pyplot as plt

import uttm


def main():
    ### define image, template and segmentation paths
    original_img = cv2.imread("./images/XXDQ_00.png")
    filtered_seg_img = cv2.imread("./segmentations/XXDQ_00.png", 0)
    templates_folder = './templates/'

    '''
    read templates into a list
    '''
    templates_list = []
    templates_names = sorted(os.listdir(templates_folder))
    for template_name in templates_names:
        temp_template = cv2.imread(templates_folder + template_name, 0)
        temp_template = temp_template > 0
        templates_list.append(temp_template)

    '''
    load segmentation masks (after running SAM) into a list
    '''
    unique_ids = np.unique(filtered_seg_img.flatten())

    segmentation_masks = []
    for seg_value in unique_ids:
        if seg_value == 0:
            continue  # skip background
        temp_seg_mask = filtered_seg_img * (filtered_seg_img == seg_value)
        segmentation_masks.append(temp_seg_mask)

    '''
    run NN template matching
    '''
    # wthetehr refine after NN matching
    if_refine = True

    # intialize matcher
    matcher = uttm.Template_Matcher(angle_per_rotation=5)

    # feed templates and masks to matcher
    matcher.get_templates(templates_list)
    matcher.get_masks(segmentation_masks)
    # matcher.reset_params()  

    # run unsupervised NN matching
    matcher.match_by_nn()

    print(matcher.matching_info)
    # visualize matching results
    merge_img = matcher.visualize_result(original_img)
    plt.imshow(merge_img)
    plt.axis('off')
    plt.show()

    cv2.imwrite("../results/XXDQ_00_01.png", merge_img)

    # run refinement 
    if if_refine:
        matcher.refine_by_icp()

        print(matcher.matching_info)
        # visualize matching results
        merge_img = matcher.visualize_result(original_img)
        plt.imshow(merge_img)
        plt.axis('off')
        plt.show()

        cv2.imwrite("../results/XXDQ_00_02.png", merge_img)


if __name__ == "__main__":
    main()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uttm-1.0.6.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uttm-1.0.6-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file uttm-1.0.6.tar.gz.

File metadata

  • Download URL: uttm-1.0.6.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for uttm-1.0.6.tar.gz
Algorithm Hash digest
SHA256 badbfb2795c1682b11e6d6a50bd103c8bb3861e75665a18360aef4f877551397
MD5 672ad7fd135f4c383a62525abbe67865
BLAKE2b-256 eeb7947674020ada1e73beae2d1c84ed12961d2deb91db112fa7b8e65b39f233

See more details on using hashes here.

File details

Details for the file uttm-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: uttm-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for uttm-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 47ec80d51c84d1965e1e78443d75e5cd33c5cc7f825e49fb04a300f83b45ee9b
MD5 10a80ad46e1a97d6f3e6fd16db93e250
BLAKE2b-256 edef758bd5213cea3f1395f71508cc99b1f443d1626e1112bf06f8d9bbebdac5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page