如何通过鼠标点击而不是猜测和获得 ROI 边界框坐标查看 [英] How to get ROI bounding box coordinates with mouse clicks instead of guess & check

查看:22
本文介绍了如何通过鼠标点击而不是猜测和获得 ROI 边界框坐标查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图截取我的显示器的屏幕截图,并且在这样做时只抓取屏幕的一部分.我知道我可以使用 mss 或 opencv、pillow 或任何其他支持边界框的屏幕截图库......但是,不是随机猜测坐标是什么......我的意思是用边界框坐标截取屏幕截图设置,并查看它是否与我实际尝试获取的图片相近.

So I am attempting to take a screenshot of my monitor and only grab part of the screen when doing so. I know I can use mss or opencv, pillow or any other screenshotting library that supports bounding boxes... However, instead of randomly guessing on what the coordinate are... and what I mean by this is taking a screenshot with bounding box coordinates set, and seeing if it is anywhere close to what I am actually trying to get the picture of.

例如:我的试验坐标是 10,10,500,500 而实际上我需要的实际坐标是 15,40,200,300(这些坐标是组成的)

For example: my trial coordinates would be 10,10,500,500 when in reality my actual coordinates that I need are 15,40,200,300 (these coordinates are made up)

我解决这个问题的想法是要么有一个工具允许我在我需要的图像(屏幕的一部分)周围单击并拖动一个边界框,然后让程序返回结果,例如 15,40,200,300.另外,如果我的盒子可以如图所示绘制,那将非常有帮助!如果有其他方法可以实现这一目标,我也会对此持开放态度.

My idea to solve this problem is to either have a tool that allows me to click and drag a bounding box around the image (part of the screen) that I need and have the program return the results, such as 15,40,200,300. Also, if I box could be drawn as it is shown that would be really helpful! If there is another way of doing achieving this goal I would be open to this as well.

谢谢.

推荐答案

这个想法是在感兴趣的区域周围单击并拖动一个边界框以获得坐标.为此,我们必须捕获鼠标单击的事件动作并记录 ROI 的开始和结束坐标.OpenCV 允许我们通过处理鼠标点击事件来做到这一点.任何时候触发鼠标点击事件,OpenCV 都会将信息传递给我们的 extract_coordinates 回调函数.为了处理事件,OpenCV 需要各种参数:

The idea is to click-and-drag a bounding box around a region of interest to obtain the coordinates. To do this, we must capture the event actions of a mouse click and record the starting and ending coordinates of the ROI. OpenCV allows us to do this by processing mouse click events. Anytime a mouse click event is triggered, OpenCV will relay the information to our extract_coordinates callback function. In order to handle the event, OpenCV requires various arguments:

  • 事件:发生的事件(左/右按下或释放鼠标点击)
  • x:事件的 x 坐标
  • y:事件的 y 坐标
  • flags:OpenCV 传递的相关标志
  • Parameters:OpenCV 传递的额外参数
  • event: Event that took place (left/right pressed or released mouse click)
  • x: The x-coordinate of event
  • y: The y-coordinate of event
  • flags: Relevant flags passed by OpenCV
  • Parameters: Extra parameters passed by OpenCV

按下左键记录左上角坐标,而释放左键单击记录右下角坐标.然后我们在 ROI 周围绘制一个边界框,并将左上角和右下角矩形区域的坐标打印到控制台.右键单击将重置图像.

A pressed left click records the top left coordinates while a released left click records the bottom right coordinates. We then draw a bounding box around the ROI and print the coordinates of the top left and bottom right rectangular region to the console. A right click will reset the image.

提取边界框坐标小部件:

Extract bounding box coordinates widget:

import cv2

class BoundingBoxWidget(object):
    def __init__(self):
        self.original_image = cv2.imread('1.jpg')
        self.clone = self.original_image.copy()

        cv2.namedWindow('image')
        cv2.setMouseCallback('image', self.extract_coordinates)

        # Bounding box reference points
        self.image_coordinates = []

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

        # Record ending (x,y) coordintes on left mouse button release
        elif event == cv2.EVENT_LBUTTONUP:
            self.image_coordinates.append((x,y))
            print('top left: {}, bottom right: {}'.format(self.image_coordinates[0], self.image_coordinates[1]))
            print('x,y,w,h : ({}, {}, {}, {})'.format(self.image_coordinates[0][0], self.image_coordinates[0][1], self.image_coordinates[1][0] - self.image_coordinates[0][0], self.image_coordinates[1][1] - self.image_coordinates[0][1]))

            # Draw rectangle 
            cv2.rectangle(self.clone, self.image_coordinates[0], self.image_coordinates[1], (36,255,12), 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__':
    boundingbox_widget = BoundingBoxWidget()
    while True:
        cv2.imshow('image', boundingbox_widget.show_image())
        key = cv2.waitKey(1)

        # Close program with keyboard 'q'
        if key == ord('q'):
            cv2.destroyAllWindows()
            exit(1)

这篇关于如何通过鼠标点击而不是猜测和获得 ROI 边界框坐标查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆