Repository: opencv
Issue: OpenCV 4.7.0, masked matchTemplate, TM_CCOEFF_NORMED return "inf" values in C++
Labels: bug

### System Information

// C++ user
OpenCV version: 4.7.0
Operating System / Platform: Windows 11 - 10.0.22621.1105
Compiler & compiler version: Visual Studio 22

### Detailed description

MatchTemplate with mask, gives wrong results with mask usage.


### Steps to reproduce

```
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // Load source image and template
    Mat src = imread("imagen.png", IMREAD_GRAYSCALE);
    Mat templateImage = imread("imagent.png", IMREAD_GRAYSCALE);

    Mat templateImage_form = imread("imagent.png", IMREAD_UNCHANGED);
	
	// if is ALPHA imagen
	Mat result;
	double tolerance = 0.85;
	if (templateImage_form.channels() == 4) {


		// Separates the image of the template and the alpha mask
		vector<Mat> channels;	
		split(templateImage_form, channels);
		Mat mask = channels[3];

		// Fix mask
		threshold(mask, mask, 1, 255, THRESH_BINARY);
		if (mask.type() != CV_8UC1) {
			mask.convertTo(mask, CV_8UC1);
		}

		auto empty_mask = 0;
		for (int i = 0; i < mask.rows; i++) {
			for (int j = 0; j < mask.cols; j++) {
				if (mask.at<uchar>(i, j) == 0) {
					empty_mask++;
				}
			}
		}
		
		if (empty_mask == 0) {
			// Apply the matchTemplate function with the source image and the template
			std::cout << "mask is not used. Size: " << empty_mask << std::endl;
			matchTemplate(src, templateImage, result, TM_CCOEFF_NORMED);
		} else {
			// Apply the matchTemplate function with the source image and the template
			std::cout << "mask is used. Size: " << empty_mask << std::endl;
			matchTemplate(src, templateImage, result, TM_CCOEFF_NORMED, mask); // < -- BUG IN THIS CASE (mask is used)
		}
	} else {
		// Apply the matchTemplate function with the source image and the template
		matchTemplate(src, templateImage, result, TM_CCOEFF_NORMED);
	}

	// Get all results with tolerance > tolerance
	vector<Point> points;
	for (int i = 0; i < result.rows; i++) {
		for (int j = 0; j < result.cols; j++) {
			if (result.at<float>(i, j) > tolerance) {
				points.push_back(Point(j, i));
			}
		}
	}

	// Draw all points
	for (int i = 0; i < points.size(); i++) {
		rectangle(src, points[i], Point(points[i].x + templateImage.cols, points[i].y + templateImage.rows), Scalar(0, 0, 255), 2);
	}

    // Load the image file to be processed
    imshow("Result", src);
    waitKey(0);

    return 0;
}
```

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)

Example images

Image used:
![imagent](https://user-images.githubusercontent.com/11653030/219092639-56439afa-6827-4b42-b004-16d93d1e34fb.png)

**Template used:**
![imagen](https://user-images.githubusercontent.com/11653030/219092646-1eb9f01f-adbe-480a-b7aa-188a0896df8f.png)

Result:
![image](https://user-images.githubusercontent.com/11653030/219093797-38761079-76bd-4160-8e0f-aea201c81649.png)

Color result:
![image](https://user-images.githubusercontent.com/11653030/219094123-6f00734f-f1c7-4bf0-80f6-15e280741a7b.png)

**Another template with small amount of alpha pixels:**
![image](https://user-images.githubusercontent.com/11653030/219094613-f2fe25a9-0e42-4aed-8d2f-08c4741c2cd5.png)

Result:
![image](https://user-images.githubusercontent.com/11653030/219094793-dffbdc3b-acf8-4123-87f9-53b08299d6e0.png)

**No alpha template, no mask:**
![image](https://user-images.githubusercontent.com/11653030/219094971-fea2eb1e-b6a9-4a69-87bf-4e3cd0bd6d59.png)

Result:
![image](https://user-images.githubusercontent.com/11653030/219095043-b648d43d-f21a-4dcd-9e1b-178282c1b653.png)

**Same code with 'TM_CCORR_NORMED' and 0.99 of threshold and color image:**

**Template:**
![image](https://user-images.githubusercontent.com/11653030/219098159-406c6eac-893c-43f4-8eec-4370010ccb59.png)

Result
![image](https://user-images.githubusercontent.com/11653030/219098067-9f1f9263-12e5-48c7-99c0-fc3737ac87c3.png)

Same template of back result, TM_SQDIFF_NORMED and 0.15 and inverted tolerance symbol:

Results
![image](https://user-images.githubusercontent.com/11653030/219099994-50f6a422-5f6a-4060-915b-d8d85bf33c73.png)

![image](https://user-images.githubusercontent.com/11653030/219100086-1c7927c6-df9f-41ad-ab1e-57a50d44ba25.png)