[enter image description here][1]good evening, I would like to make a screenshot and use it on templat matcching but every time i tried i have this error , i'm not a pro programer i just do that for the fun if someone could help me that's will be amazing. thnx.
the error i get :
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1164: error: (-215:Assertion failed) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function 'cv::matchTemplate'[enter image description here][2]
if i use a sceenshot taked whit an softwer it works perfectli but whe i try whit screenshot taken whit this script it doesnt work .
import cv2
import numpy as np
import pyautogui
from PIL import Image
#take a screenshot
pyautogui.screenshot().save("ecran.png")
# Load the image and convert to 32-bit RGB
im = Image.open("ecran.png").convert('RGB')
# Save result
im.save("ecran.PNG")
#input()
bureau = cv2.imread('C:/Users/ASUS/Desktop/Image-reconizer/ecran.PNG', cv2.IMREAD_UNCHANGED)
dossier = cv2.imread('C:/Users/ASUS/Desktop/Image-reconizer/dossier.PNG', cv2.IMREAD_UNCHANGED)
result = cv2.matchTemplate(bureau, dossier, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc, = cv2.minMaxLoc(result)
w = dossier.shape[1]
h = dossier.shape[0]
pos1 = max_loc[0] + h
pos2 = max_loc[1] + w
yloc, xloc = np.where(result >= 0.55)
rectangle = []
for (x, y) in zip(xloc, yloc):
rectangle.append([int(x), int(y), int(w), int(h)])
rectangle.append([int(x), int(y), int(w), int(h)])
rectangle, weights = cv2.groupRectangles(rectangle, 1, 0.2)
for (x, y, w, h) in rectangle:
rec = cv2.rectangle(bureau, (x, y), (x + w, y + h), (0, 255, 0), 2)
window = cv2.imshow("rectangle ", rec)
cv2.waitKey()
this version of code is for using 2 image directly whitout screenshot:
import cv2
import numpy as np
import pyautogui
from PIL import Image
bureau = cv2.imread('C:/Users/ASUS/Desktop/Image-reconizer/ecran1.PNG', cv2.IMREAD_UNCHANGED)
dossier = cv2.imread('C:/Users/ASUS/Desktop/Image-reconizer/dossier.PNG', cv2.IMREAD_UNCHANGED)
result = cv2.matchTemplate(bureau, dossier, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc, = cv2.minMaxLoc(result)
w = dossier.shape[1]
h = dossier.shape[0]
pos1 = max_loc[0] + h
pos2 = max_loc[1] + w
yloc, xloc = np.where(result >= 0.55)
rectangle = []
for (x, y) in zip(xloc, yloc):
rectangle.append([int(x), int(y), int(w), int(h)])
rectangle.append([int(x), int(y), int(w), int(h)])
rectangle, weights = cv2.groupRectangles(rectangle, 1, 0.2)
for (x, y, w, h) in rectangle:
rec = cv2.rectangle(bureau, (x, y), (x + w, y + h), (0, 255, 0), 2)
window = cv2.imshow("rectangle ", rec)
cv2.waitKey()
error image : [1]: https://i.stack.imgur.com/G0f5R.png
dossier.png [2]: https://i.stack.imgur.com/i1qea.png
ecran1.png(this image the screen shot taken whit a softwer wwhen i use this image instead of taken screenshot whit the script the code works perfectli) [3]: https://i.stack.imgur.com/bQPFJ.jpg
this is an image of te result when i use the image 3 instead of a screenshot (the program works ) [4]: https://i.stack.imgur.com/eAEXT.jpg