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

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

问题描述

因此,我尝试拍摄监视器的屏幕截图,并且在这样做时仅抓取屏幕的一部分.我知道我可以使用mss或opencv,枕头或支持边界框的任何其他屏幕截图库...但是,不是随机猜测坐标是什么...而我的意思是拍摄带有边界框坐标的屏幕截图设置,然后查看它是否与我实际试图获得的照片相近.

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坐标
  • 标志:OpenCV传递的相关标志
  • 参数: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天全站免登陆