-1

[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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
kwalid600
  • 21
  • 5
  • Can you add your 2 images ? – David Jan 23 '22 at 22:25
  • @David the firt image "ecran.PNG" is the the screenshot that i take it's suposed to be a screenshot of my desktop and the second image "dossier.PNG" is an icon of a folder a ihave add it to my post so you can check it now – kwalid600 Jan 23 '22 at 22:37
  • Be sure that the images format are the same because you read it with cv2.IMREAD_UNCHANGED. Maybe one of the image have an alpha channel so read them with cv2.IMREAD_COLOR – David Jan 23 '22 at 22:50
  • please review [ask] and [mre]. – Christoph Rackwitz Jan 23 '22 at 23:55
  • Which image is which dossier and bureau? The template image must be the smaller in matchTemplate(). Be sure you have listed them in the the correct order in matchTemplate(). – fmw42 Jan 24 '22 at 05:10
  • @David thank you for taking time to helping me , i tried the method you recommended "cv2.IMREAD_COLOR" and it works . – kwalid600 Jan 24 '22 at 08:17

1 Answers1

1

the problem was whit the cv2.IMREAD_UNCHANGED methode just use cv2.IMREAD_COLOR and it will works well this is the finale working code:


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_COLOR)
dossier = cv2.imread('C:/Users/ASUS/Desktop/Image-reconizer/dossier.PNG', cv2.IMREAD_COLOR)
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()

kwalid600
  • 21
  • 5