E
E
Elvis2022-03-07 21:36:55
OpenCV
Elvis, 2022-03-07 21:36:55

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):
62264fd7e181c012115467.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Elvis, 2022-03-08
@Dr_Elvis

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)

mask.png is an image with an alpha channel where only the green border is visible
CroppedImage.png is the result found in the screenshot(I'm making a screenshot)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question