Answer the question
In order to leave comments, you need to log in
How to find an image by mask?
Let's say a picture where in the green (for example) frame is the piece I need for analysis.
Is there a way to find this area using CV2 or something else?
This image can be anywhere on the page. We need to find the relative/absolute coordinates of this image. I understand that you need to use a mask, but I did not find how to use it.
for example, here is an image, my avatar is in a green circle (let's assume that I know exactly the size of the circle and color, but I don't know the location and what's inside the circle):
Answer the question
In order to leave comments, you need to log in
Happened!
import cv2
import pyautogui
import numpy as np
small_image = cv2.imread('mask.png')
image = pyautogui.screenshot()
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
result = cv2.matchTemplate(small_image, image, cv2.TM_CCOEFF)
_,_,_,xy = cv2.minMaxLoc(result)
MPx,MPy = xy
trows,tcols = small_image.shape[:2]
cropped_image = image[MPy:MPy+trows, MPx:MPx+tcols]
cv2.imwrite("CroppedImage.png", cropped_image)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question