-1

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: Train And I have this template (first character of the speed) which is in the downer right corner of the bigger image above:

Template

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:

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.

I'm the man.
  • 207
  • 2
  • 13
  • 1
    I gues you have got misprint here `img_rgb = cv2.imread('mta-screen_2020-01-01_12-07-24.png.png')` duplicate file extension – weAreStarsDust Jan 01 '20 at 19:08

1 Answers1

0
img_rgb = cv2.imread('mta-screen_2020-01-01_12-07-24.png.png')

I found the answer if you see there is two .png instead of one. Now, I'm trying to solve the problem, because it isn't really detecting what I need, so I'm now trying to crop down the image to make the downer right corner seeable only.

I'm the man.
  • 207
  • 2
  • 13