I have this code (which used in the documentation) for template matching:
img_rgb = cv2.imread('mta-screen_2020-01-01_12-07-24.png.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
path = 'D:\!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_!Piton\MTA_pyautogui\TrainImgs' + chr(92) + '1.png'
template = cv2.imread(path, 0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = nm.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
cv2.imwrite('res.png', img_rgb)
So, I have this picture (to detect on) which has the speed in the downer right corner:
And I have this template (first character of the speed) which is in the downer right corner of the bigger image above:
And I have the output error:
Traceback (most recent call last):
File "D:/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_!Piton/MTA_pyautogui/main.py", line 38, in <module>
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
File Tree:
So, my goal is to make a template from every number and detect the train's speed. I don't really care about the method or the speed I just want an output which tells the speed of the train. So, for that, I wanted to use the OpenCV
template matching.