首页 > 解决方案 > 有什么方法可以反转赏金框坐标外部的背景。使用蟒蛇?

问题描述

蟒蛇爱好者!

我是一个初学者-pythonist。

我正在使用 YOLO V3 来识别和定位带有赏金框的图像。它会给出赏金箱坐标。

有没有办法通过使用任何一个 python 成像库来反转赏金框之外的图像背景?

假设我们从图像中收到了多个赏金框坐标。

我没有太多时间去探索新事物。我正在为一家 xyz 公司做项目。

提前谢谢。!

标签: computer-visionpython-imaging-libraryopencv3.0

解决方案


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, ...]

在此处输入图像描述


推荐阅读