computer-vision - 有什么方法可以反转赏金框坐标外部的背景。使用蟒蛇?
问题描述
蟒蛇爱好者!
我是一个初学者-pythonist。
我正在使用 YOLO V3 来识别和定位带有赏金框的图像。它会给出赏金箱坐标。
有没有办法通过使用任何一个 python 成像库来反转赏金框之外的图像背景?
假设我们从图像中收到了多个赏金框坐标。
我没有太多时间去探索新事物。我正在为一家 xyz 公司做项目。
提前谢谢。!
解决方案
import cv2
import numpy as np
# second parameter is 0. it means to read the image in grayscale
# just for example
img = cv2.imread(path_to_your_image, 0)
假设您的图像是
您可以使用以下代码获得倒置图像
img_inverted = 255 - img
你的边界框是
x, y, w, h = 100, 50, 120, 120
x
,y
是边界框的左上角坐标。w
,h
是边界框的宽度和高度
您可以使用此代码反转边界框外部的图像背景
img_inverted[y:y+h, x:x+w, ...] = img[y:y+h, x:x+w, ...]
# note that the first axis is `y` and the second is `x`
# ... — means take all the channels
# in grayscale we have only one channel,
# but this will work even if you have RGB image
同样适用于多个边界框
bboxes = [
[100, 50, 120, 120],
[10, 10, 20, 50],
[300, 30, 50, 50],
[300, 130, 50, 50]]
for bbox in bboxes:
x, y, w, h = bbox
img_inverted[y:y+h, x:x+w, ...] = img[y:y+h, x:x+w, ...]
推荐阅读
- rocksdb - 强制将 Rocksdb 内存表刷新到磁盘
- flutter - 使用 .net core API 服务在 Flutter 应用中注册后如何进行电子邮件验证
- python - 有没有办法在退出 Spyder 时自动执行 Python 文件(有点像启动的反面)?
- flutter - Rive 无法在 android 上加载资产
- docker - 使用 github-action 构建镜像并推送到 docker hub
- python - 如何检查元素是否与现有元素正交相邻(旁边)?
- firebase - 有没有其他方法可以向没有 firebase 的用户发送通知消息?
- c++ - 通过提升奇怪的http响应
- azure - 如何使用 Terraform 管理恢复的 Azure VM?
- excel - 更改 VBA 代码以在多个工作表中查找和替换