简体   繁体   中英

Translate coordinates between coordinate systems opencv python

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()

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM