# How to Improve the Accuracy of Pattern Matches

**URL:** <https://forum.opencv.org/t/how-to-improve-the-accuracy-of-pattern-matches/7232>\
**Category:** C++\
**Created:** [January 22, 2022, 8:32am UTC](https://forum.opencv.org/t/how-to-improve-the-accuracy-of-pattern-matches/7232 "2022-01-22T08:32:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tumanovalex](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@tumanovalex](https://forum.opencv.org/u/tumanovalex)\
**Post date:** [January 22, 2022, 8:32am UTC](https://forum.opencv.org/t/how-to-improve-the-accuracy-of-pattern-matches/7232/1 "2022-01-22T08:32:56Z")

</div>

The result of the function matchTemplate is represented by the image matchTemplate4096.jpg. For the function, matchTemplate chose different methods, selected the best result. The image clearly shows different “luminous” points from the background: 8 points in one series, 32 series in one line, a total of 16 series lines - that is, a total of 4096 points. In the program:

```auto
for (int i = 115; i < 121; i++)
{
  threshold(img, dst_tresh, i, 255, THRESH_BINARY);
  int nccomps = connectedComponentsWithStats(dst_tresh, labels_tresh, stats_tresh, centroids_tresh);
  out << i << '\t' << nccomps << endl;
  imwrite(to_string(i) + "_" + to_string(nccomps) + "_" + fileName, dst_tresh);
}

```

using connectedComponentsWithStats I try to match the threshold value and correctly determine the number of points. None of the results match the number of points counted manually. The closest result is achieved at a threshold of 118-4095 points. However, in the lower right row I see 3 extraneous points, and in the first, second and fourth vertical rows of points some points are absent. Please tell me what OpenCV features can be used to improve the accuracy of determining the number of points in an image. The project project is referenced [CppOpenCVstudy.zip — Яндекс.Диск](https://disk.yandex.ru/d/OO_Z5iKBRv3eyg)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 22, 2022, 11:14am UTC](https://forum.opencv.org/t/how-to-improve-the-accuracy-of-pattern-matches/7232/2 "2022-01-22T11:14:39Z")

</div>

we cannot see your image. can you try to upload it here, again ?  
also, please explain, how template matching is related to the cc code you show

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 22, 2022, 12:05pm UTC](https://forum.opencv.org/t/how-to-improve-the-accuracy-of-pattern-matches/7232/3 "2022-01-22T12:05:33Z")

</div>

I managed to navigate through the [yandex.ru](http://yandex.ru) link into that jpg image.

so you’re analyzing die shots of ROM, or something like that.

what I see there appears to be the result of `matchTemplate`.

I see a good array of local maxima, but the picture also indicates that your template wasn’t covering entire cells (template wasn’t as large as it could be). a template that is too small/generic will also result in responses where you don’t want them.

you can catch these maxima with some non-maximum suppression. dilation and equality comparison can do that, combined with absolute thresholds that are barely above background levels.
