How to Improve the Accuracy of Pattern Matches

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:

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 — Яндекс.Диск

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

I managed to navigate through the 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.