N
N
Name_Zagotovka2020-11-22 17:46:43
Python
Name_Zagotovka, 2020-11-22 17:46:43

How to use cv2.matchTemplate without saving the source to disk?

We have the following code. actions. We make a screen. looking for "zzz.png" on the screen. If found, we display the coordinates.
But if you try to send a screenshot of a variable (the code below), then we get the error "Expected Ptr for argument 'image'", if I understand correctly, it swears that there is a mandatory argument that the matchTemplate function is waiting for, and which is allegedly not specified ...

def chek():
    img = ImageGrab.grab()
    template = cv2.imread("zzz.png")
    res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED)
    min_v, max_v, min_pt, max_pt = cv2.minMaxLoc(res)
    threshold=0.98
    loc = np.where( res >= threshold)
    # print(str(x)+'    '+str(y))
    coord=1
    for pt in zip(*loc[:: -1]):
        x,y=pt
        print('coord = '+str(coord))
        print(x)
        print(y)
        coord=coord+1


However, if we correct the code a little, namely, save the screenshot made, then we read it through read (correction below), then everything takes off.
img1 = ImageGrab.grab()
    img.save('test.png')
    img = cv2.imread("test.png")
    template = cv2.imread("zzz.png")
    res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED)


Question: is it possible to pass a screen to a variable, i.e. without saving to a file, and then reading from the saved file.
Thank you in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-11-22
@fox_12

Can. Read about BytesIO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question