Mss grab to numpy to cv2MatchTemplate

493 Views Asked by At

sorry in advance if the question as been requested .

I want to take a screenshot with MSS , send it to CV2 ( for template matching ) with NUMPY (and after do euclidian distance for fin the closest from a point ) .

But i can't :(

I think is a problem of "passing the numpy array to workable thing for cv2.MatchTemplate .

( the template is little image , much more than the "screen rect" )

I have try a lots of RGB to BRG and co , .....

I dont seeM what is wrong

( all commented line is for helping see where i am ) , a some other thing that i have try .

The 3 parts of the screen grabed , showed by cv2.imshow are black ( do not displayed correctly )

if we use a png in replacement of screenshot with mss this code work perfectly

this is where i am :

import cv2
import numpy as np
import mss 
import time
import numpy as np
from scipy.spatial.distance import cdist

time.sleep(10)
################################################################################
def screen_template_closest ()  :   
    with mss.mss() as sct:
                      
             # Part of the screen to capture 
             monitor = {"top": 1059, "left": 62, "width": 285, "height": 640}
      
             #Get raw pixels from the screen, save it to a Numpy array
             img = np.array(sct.grab(monitor))
             print("image shape",img.shape)
            
    # Display the picture
    cv2.imshow("OpenCV/Numpy normal", img)
    # Display the picture in grayscale
    cv2.imshow('OpenCV/Numpy grayscale',cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY))

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
    template = cv2.imread('C:\\Users\\Faon\\projetadminEnv3\\test\\templates.PNG',0)
    w, h = template.shape[::-1]
    res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
    threshold = 0.75
    #Counts the number of found matches, which are currently overlapping
    loc = np.where( res >= threshold)
    
    coordsFinded = list()
    count = 0
    mask = np.zeros(img.shape[:2], np.uint8)
    
    for pt in zip(*loc[::-1]):
        if mask[pt[1] + h//2, pt[0] + w//2] != 255:
            mask[pt[1]:pt[1]+h, pt[0]:pt[0]+w] = 255
            count += 1
            cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,255,0), 3)
            coordsFinded.append(pt)
            
    cv2.imshow('Matches',img)
    cv2.waitKey(0) & 0xFF== ord('q')

    #closest distance from pta
    pta = np.array([[1036, 746]])   
    others1 = np.array(coordsFinded)
    print("others (all coords of finded images ) ",others1)
    distances = cdist(pta, others1)

    winnerClose =  others1[distances.argmin()]
    print("the closest point is    : ",winnerClose)      
#######################################################################################

print(screen_template_closest()) 

I will greatly appraciante any help .

0

There are 0 best solutions below