繁体   English   中英

如何减去/遮蔽图像中的区域 Python OpenCV

[英]How to subtract/black-out regions within an image in Python OpenCV

我正在从事一个涉及使用计算机视觉自动化视频游戏的项目。 我的下一个任务是将游戏的 UI 元素与实际游戏的视野分开。 例如:

我们将截取整个客户端 window 的屏幕截图,如下所示: 客户端截图

然后我们将在屏幕上定位各种 UI 元素(这是通过模板匹配完成的,请参阅: 使用 OpenCV Python 与透明图像模板进行模板匹配)。

然后,我们需要从客户端屏幕截图中减去匹配的模板,最终得到如下内容: 已减去 UI 元素的客户端屏幕截图

这使我能够在游戏的视野中执行计算机视觉功能,而没有 UI 元素干扰的风险。

假设我们有以下代码:

# Let's assume we have already taken a screenshot of the client window
client_window = cv2.imread('client_window.png')

# Here are the three UI areas I'd like to remove/blackout from the above image
ui_chatbox = {'left': 0, 'top': 746, 'width': 520, 'height': 167}
ui_minimap = {'left': 878, 'top': 31, 'width': 212, 'height': 207}
ui_inventory = {'left': 849, 'top': 576, 'width': 241, 'height': 337}

我们如何从client_window矩阵中屏蔽 UI 元素边界框的像素?

我最终通过使用切片将范围内的所有像素设置为黑色来解决这个问题。 我想到这个是因为我知道裁剪图像是一个类似的过程,两者都涉及选择一系列像素:

import cv2
import numpy as np

# Let's assume we have already taken a screenshot of the client window
client_window = cv2.imread('client_window.png')
cv2.imshow('client_window', client_window)
cv2.waitKey(0)

# Here are the three UI areas I'd like to remove/blackout from the above image
ui_chatbox = {'left': 0, 'top': 746, 'width': 520, 'height': 167}
ui_minimap = {'left': 878, 'top': 31, 'width': 212, 'height': 207}
ui_inventory = {'left': 849, 'top': 576, 'width': 241, 'height': 337}

client_window[ui_chatbox['top']:ui_chatbox['top'] + ui_chatbox['height'],
                ui_chatbox['left']:ui_chatbox['left'] + ui_chatbox['width']] = np.array([0,0,0])
client_window[ui_minimap['top']:ui_minimap['top'] + ui_minimap['height'],
                ui_minimap['left']:ui_minimap['left'] + ui_minimap['width']] = np.array([0,0,0])
client_window[ui_inventory['top']:ui_inventory['top'] + ui_inventory['height'],
                ui_inventory['left']:ui_inventory['left'] + ui_inventory['width']] = np.array([0,0,0])
cv2.imshow('result', client_window)
cv2.waitKey(0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM