是否可以将 OpenCV 设置为以图像的左下角为原点?
我正在使用 cv2.imshow() 在窗口中显示图像,允许我点击图像并获取鼠标点击事件的坐标。我可以使用 this question 的部分答案使图像显示我想要的方式(在 x 轴上反转)但是当我单击图像以检索坐标时,原点仍在图像的左上角。
import cv2 # Version 3.4.5
import imutils as im
import numpy as np
import matplotlib.pyplot as plt
list_of_coords = []
def store_mouse_coords(event, x, y, flags, params):
global list_of_coords
if event == cv2.EVENT_LBUTTONDOWN:
list_of_coords.append((x, y))
print(list_of_coords[-3:])
return list_of_coords
def select_points_on_im(image, window_name='Select points'):
image = image.copy()
image = image[::-1, :, :]
while True:
cv2.namedWindow(window_name)
cv2.setMouseCallback(window_name, store_mouse_coords)
cv2.imshow(window_name, image)
#plt.imshow(window_name, image, plt.gca().invert_yaxis,plt.show())
key = cv2.waitKey(0) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
return
select_points_on_im(image)
注释行 plt.imshow(window_name, image, plt.gca().invert_yaxis,plt.show())
是我想要实现的但使用 来自 OpenCV 的 imshow
以便我可以将 is 与我的回调函数一起使用。我使用的是 Python3 和 OpenCV 3.4.5 版。
最佳答案
基于 Mark Setchell's comment ,我将行 adj_y = image.shape[0] - y
添加到鼠标回调以实现所需的行为。一个让我困惑了一段时间的小陷阱,numpy
的 shape 属性返回行,即 y,x:
list_of_coords = []
def store_mouse_coords(event, x, y, flags, params):
global list_of_coords
if event == cv2.EVENT_LBUTTONDOWN:
adj_y = image.shape[0] - y # adjusted y
print('image shape is {}'.format(image.shape[:2]))
print('y was {}, image height minus y is {}'.format(y, adj_y))
list_of_coords.append((x, adj_y))
return list_of_coords
关于python - 如何设置 OpenCV 将原点用作图像的左下角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54594236/