Skip to main content

cv - Detect features of multiple templates (:cv_detect_templates)

Declaration

results, visualized = img:cv_detect_templates(templates_with_options)

Parameters

  • templates_with_options
    Optional, table. Each sequential element is an image object; named fields are options

    Option fields
    {
    should_visualize = false | true, -- optional, whether to output visualization, default false
    detector = 0 | 1 | 2, -- optional, feature detector, 0: ORB, 1: SIFT, 2: SURF, default 0: ORB
    score_thresh = number_value, -- optional, percent score threshold for feature matching, default 90
    }

Returns

  • results
    Table. Collection of detected template features on the image

    Result structure
    {
    { -- result 1
    { -- vertex 1
    ["y"] = number_value,
    ["x"] = number_value,
    },
    { -- vertex 2
    ["y"] = number_value,
    ["x"] = number_value,
    },
    { -- vertex 3
    ["y"] = number_value,
    ["x"] = number_value,
    },
    { -- vertex 4
    ["y"] = number_value,
    ["x"] = number_value,
    },
    confidence = number_value, -- aggregated confidence of all feature points
    angle = number_value, -- rotation angle
    index = number_value, -- index of the matched template in the input collection
    },
    { -- result 2
    { -- vertex 1
    ["y"] = number_value,
    ["x"] = number_value,
    },
    { -- vertex 2
    ["y"] = number_value,
    ["x"] = number_value,
    },
    { -- vertex 3
    ["y"] = number_value,
    ["x"] = number_value,
    },
    { -- vertex 4
    ["y"] = number_value,
    ["x"] = number_value,
    },
    confidence = number_value,
    angle = number_value,
    index = number_value,
    },
    ...
    }
  • visualized
    Image object. When options.should_visualize is true, this is the visualization image

Description

Detect features on the image from a set of small image templates; works with transformed/rotated templates
Each template only returns the best result whose score exceeds score_thresh
Supported in versions >= 20250625

Notes

score_thresh (input) differs from confidence (output)
score_thresh filters feature points; confidence is aggregated template confidence

Example

local cv = require("image.cv")
local img1 = image.load_file(XXT_SCRIPTS_PATH..'/1.png')
local img2 = image.load_file(XXT_SCRIPTS_PATH..'/2.png')

local results, visimg = screen.image():cv_detect_templates({
img1, img2;
score_thresh = 90,
detector = 1, -- SIFT
should_visualize = true, -- when true, second return value is an image
})

nLog(results)
dialog():add_image(visimg):show()