Skip to content
firepick1 edited this page Mar 8, 2014 · 8 revisions

OpenCV matchTemplate wrapper with FireSight extensions.

Correlation-based template matching is quite powerful. The frequency spectrum of a template is multiplied by the frequency spectrum of an image to obtain the correlation. Maxima (or minima, depending on the method) indicate the candidates for matching. If the images to be recognized are all in a flat plane orthogonal to the camera, there is little or no need to account for perspective skew or near/far size adjustments. For this reason, matchTemplate alone can be used quite effectively in a great majority pick-and-place use cases.

  • method Default is CV_TM_CCOEFF_NORMED.
FireSight extensions
  • output Image for stage output. Default is current, which displays working image. To use input image, use input; to see correlation image, use corr.
  • threshold If maxVal is below this value, then no matches will be reported
  • corr Normalized recognition threshold in the interval [0,1]. Used to determine best match of candidates. For CV_TM_CCOEFF, CV_TM_CCOEFF_NORMED, CV_TM_CCORR, and CV_TM_CCORR_NORMED methods, this is a minimum threshold for positive recognition; for all other methods, it is a maximum threshold.
  • angle Match template at specified angle in degrees

Model

{
    "maxVal":0.97696870565414429,
    "rects":[
      {
        "x":467.0,
        "y":68.0,
        "width":29.0,
        "height":37.0,
        "angle":0.0,
        "corr":1.0
      },
      {
        "x":518.0,
        "y":69.0,
        "width":29.0,
        "height":37.0,
        "angle":0.0,
        "corr":0.99414855241775513
      },
      {
        "x":711.0,
        "y":71.0,
        "width":29.0,
        "height":37.0,
        "angle":0.0,
        "corr":0.99687016010284424
      },
      {
        "x":760.0,
        "y":71.0,
        "width":29.0,
        "height":37.0,
        "angle":0.0,
        "corr":0.99490123987197876
      }
    ]
  }
  • maxVal The maximum value in the correlation matrix
  • rects The RotatedRect values of the matched positions
  • x Center x
  • y Center y
  • height Height of rectangle
  • width Widht of rectangle
  • corr Normalized correlation over the interval [0,1], with respect to maxVal

Example: Find 37x29 rectangles in a pcb image pipeline

firesight -i img/pcb.jpg -p json/matchCCOEFF_NORMED.json -o target/matchCCOEFF_NORMED.jpg
Input image
Template

The template is remarkably simple and can even be generated by the pipeline. It is just a 37x29 rectangle:

Matched rectangles
Correlation output

Bright spots in the correlation image correspond to matches:

Example: Find 37x29 rectangles rotated 45 degrees pipeline

firesight -i img/pcb.jpg -p json/match45.json -o target/match45.jpg

Example: Find template at 0, 90, 180, and 270 degrees pipeline

firesight -i img/chris-reference.jpg -p json/chris-match-search.json -o target/chris-match.jpg

Here is a simple way to recognize four orientations with a single pipeline. Although easy to put together, it quadruples the total time taken to match the templates. Other solutions such as template ring projection can create templates capable of recognizing arbitrary or specific angles.

Clone this wiki locally