0

The corner coordinates of square_1 = (0, 0, 1920, 1080). I then define square_2 as a smaller ROI within square one using numpy slicing like so roi = square_1[y1:y2, x1:x2]. I then resize square_1 using square_resize = cv2.resize(square_1, (960, 540), interpolation = cv2.INTER_AREA) . However, now my ROI is no longer accurate. I have a tool which tells me the screen coords of the mouse pos, which is how I find the dimensions of the ROI, but I need a function that translates the ROI coordinates I find, given the coordinates of square_1, in terms of the coordinates of square_resize.

EDIT: Solved using Panda50's answer. grab_screen() is my own custom function for getting screenshots. Here is my code if it helps anyone. It does not give 100% accurate coords but you can play around some and narrow it down.

from cv2 import cv2
import numpy as np

y1 = int(92 / 2)
y2 = int(491 / 2)
x1 = int(233 / 2)
x2 = int(858 / 2)

# grab screen and convert to RGB
screen = grab_screen(region = (0, 0, 1920, 1080))
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)

# resize screen
screen = cv2.resize(screen, (960, 540), interpolation = cv2.INTER_AREA)

# define ROI
roi = screen[y1:y2, x1:x2].copy()

cv2.imshow('roi', roi)

cv2.waitKey()
cv2.destroyAllWindows()
1
  • Please see this recent answer from me. It's about adapting bounding boxes while resizing the parent image, but it's the same for adapting ROIs while resizing the parent image. Commented Jan 27, 2021 at 9:58

1 Answer 1

1

In python, = associate one variable with another. By changing square_1 you'll also change roi .

You have to use :

roi = square_1[y1:y2, x1:x2].copy()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.