Repository: opencv
Issue: matchTemplate inconsistent results with M1 Chip 
Labels: bug,platform: ios/osx,platform: arm

##### System information (version)
<!-- Example
- OpenCV => 4.6.0
- Operating System / Platform => MacOS Monterey 12.5.1
- Chip => Apple M1 Pro
-->

- OpenCV => 4.6.0
- Operating System / Platform => MacOS Monterey 12.5.1
- Chip => Apple M1 Pro
- Python 3.10.6

##### Detailed description
We have heavy use of `matchTemplate` for different use cases our team has been developing on all platform

- Windows
- Linux
- Mac x86

Recently when some of developers upgraded with `Mac M1 Chip` they started to have issues same code that has `confidence` of `1` is not returning `nan` for **only Mac M1**. 

##### Steps to reproduce
We are using OpenCV via python here is requirements.txt
```
opencv-python-headless==4.6.0.66
opencv-contrib-python==4.6.0.66
```
And here is the code snippet
```
import cv2 as cv
from matplotlib import pyplot as plt
img_grey = cv.cvtColor(cv.imread('1.png'), cv.COLOR_BGR2GRAY)
template = cv.cvtColor(cv.imread('horizontal_small.png'), cv.COLOR_BGR2GRAY)
mask = cv.cvtColor(cv.imread('horizontal_small-mask.png'), cv.COLOR_BGR2GRAY)
w, h = template.shape[::-1]
methods = ['cv.TM_CCOEFF_NORMED']
for meth in methods:
    method = eval(meth)
    # Apply template Matching
    res = cv.matchTemplate(image=img_grey, templ=template, method=method, mask=mask)
    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv.rectangle(img_grey,top_left, bottom_right, 255, 2)
    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.title(f'Matching Result {max_val}'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img_grey,cmap = 'gray')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)
    plt.show()
``` 
Code above return `confidence` of `1` on other platforms and `nan` on Mac M1
![horizontal_small-mask](https://user-images.githubusercontent.com/109785821/189101341-e3c353b4-0dbf-411b-8957-7678020ca406.png)
![horizontal_small](https://user-images.githubusercontent.com/109785821/189101345-f0ac83c7-e3d6-4eda-80d9-76756f907d48.png)
![1](https://user-images.githubusercontent.com/109785821/189101301-5b8d7c8e-8dd0-4eaa-bcf4-f3c8e4665845.png)
<img width="1213" alt="result_M1" src="https://user-images.githubusercontent.com/109785821/189101926-766182c3-1375-4896-b252-cdfe25c9fa0f.png">

##### Issue submission checklist

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with forum.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with questions without a real issue statement will be
   closed.
   -->
 - [x] I updated to the latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only the latest release for each branch.
   The ticket is closed if the problem is not reproduced with the modern version.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       forum.opencv.org, Stack Overflow, etc and have not found any solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://forum.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as an archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->