因此,我尝试拍摄监视器的屏幕截图,而仅在抓取屏幕的一部分时才这样做。我知道我可以使用mss或opencv,枕头或任何其他支持边界框的屏幕截图库...但是,不是随机猜测坐标是什么...而我的意思是用边界框坐标截屏设置,然后查看它是否与我实际试图获得的照片相近。
例如:我的试用坐标为10,10,500,500,而实际上我需要的实际坐标为15,40,200,300(这些坐标已组成)
我解决此问题的想法是拥有一个工具,该工具可以让我单击并拖动所需图像(屏幕的一部分)周围的边界框,并让程序返回结果,例如15,40,200,300。 A 此外,如果可以画出我的方框,那将非常有帮助! 如果还有另一种方法可以实现这个目标,我也会对此持开放态度。
谢谢。
答案 0 :(得分:0)
这个想法是在感兴趣的区域周围单击并拖动边界框以获取坐标。为此,我们必须捕获鼠标单击的事件动作,并记录ROI的开始和结束坐标。 OpenCV允许我们通过处理鼠标单击事件来做到这一点。每当触发鼠标单击事件时,OpenCV都会将信息中继到我们的extract_coordinates
回调函数中。为了处理事件,OpenCV需要各种参数:
按下的左键单击记录左上角坐标,释放的左键单击记录右下角坐标。然后,我们在ROI周围绘制一个边界框,并将左上和右下矩形区域的坐标打印到控制台。右键单击将重置图像。
提取边界框坐标小部件:
import cv2
class ExtractImageWidget(object):
def __init__(self):
self.original_image = cv2.imread('placeholder.PNG')
# Resize image, remove if you want raw image size
self.original_image = cv2.resize(self.original_image, (640, 556))
self.clone = self.original_image.copy()
cv2.namedWindow('image')
cv2.setMouseCallback('image', self.extract_coordinates)
# Bounding box reference points and boolean if we are extracting coordinates
self.image_coordinates = []
self.extract = False
def extract_coordinates(self, event, x, y, flags, parameters):
# Record starting (x,y) coordinates on left mouse button click
if event == cv2.EVENT_LBUTTONDOWN:
self.image_coordinates = [(x,y)]
self.extract = True
# Record ending (x,y) coordintes on left mouse bottom release
elif event == cv2.EVENT_LBUTTONUP:
self.image_coordinates.append((x,y))
self.extract = False
print('top left: {}, bottom right: {}'.format(self.image_coordinates[0], self.image_coordinates[1]))
# Draw rectangle around ROI
cv2.rectangle(self.clone, self.image_coordinates[0], self.image_coordinates[1], (0,255,0), 2)
cv2.imshow("image", self.clone)
# Clear drawing boxes on right mouse button click
elif event == cv2.EVENT_RBUTTONDOWN:
self.clone = self.original_image.copy()
def show_image(self):
return self.clone
if __name__ == '__main__':
extract_image_widget = ExtractImageWidget()
while True:
cv2.imshow('image', extract_image_widget.show_image())
key = cv2.waitKey(1)
# Close program with keyboard 'q'
if key == ord('q'):
cv2.destroyAllWindows()
exit(1)