是否可以将OpenCV设置为使用图像的左下角作为原点?
我正在使用cv2.imshow()在窗口中显示图像,该窗口允许我单击图像并获取mouseclick事件的坐标。我可以使用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
,以便可以与我的回调函数一起使用。我正在使用Python3和OpenCV版本3.4.5。
答案 0 :(得分:2)
基于Mark Setchell's comment,我在鼠标回调中添加了行adj_y = image.shape[0] - y
以实现所需的行为。一个小陷阱让我有些困惑,numpy
的shape属性返回行,即col,即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