繁体   English   中英

对象检测 - 如何将坐标内的操作像素重新应用于原始图像以突出显示 RoI

[英]Object detection- How to re-apply manipulated pixels within co-ordinates to the original image to highlight RoI

我有一组 RGB 图像(1920 x 1080)的坐标存储为列表列表,它是 RoI(兴趣区域),如下所示

[[1538, 234, 1626, 313],
[421, 231, 472, 288],
[918, 199, 988, 270],
[802, 0, 963, 114],
[1732, 0, 1909, 108]]

然后我应用以下条件来更改上面列表列表中坐标内的特定像素值,仅通过将像素更改为白色和黑色来突出显示这些 RoI。

for coords in list_of_coords:

            roi = orig[coords[1]:coords[3], coords[0]:coords[2]]

            roi [(roi > 200)] = 0
            roi [(roi < 200)] = 255

我现在如何采用这些新修改的像素值并将它们应用到具有一定透明度的原始图像之上? 理想情况下,图像的其余部分将是相同的,而这些坐标内的值将在其顶部有一个透明的蒙版。

以下是所需输出的示例。 这是我从 Mask RCNN 中获取的,在矩形坐标中,添加了轻微的蓝色阴影以突出显示 RoI。 矩形框用上面列表的列表表示,蓝色阴影是上述条件的操作。

在此处输入图片说明

我认为您正在寻找的答案是alpha blending 如果您有描述中给出的两个图像(大小相同)(我们称它们为imgannotation ),您可以执行以下操作:

alpha = 0.5 
# How much you want img to be opaque, alpha = 1 leads to your result being img,
# alpha = 0 to result==annotation
# with alpha = 0.5, you will get a blending with each image have the same 
# importance
result = cv2.addWeighted(img, alpha, annotation, 1-alpha, 0)

您可以在 此处找到有关该功能的一些说明。

现在,如果您的图像大小不同,则需要混合它们之前调整它们的大小(我会调整较小的图像以获得与较大图像相同的尺寸),因此它应该如下所示:

width = img.shape[1]
height = img.shape[0]
dim = (width, height)
annotation = cv2.resize(annotation, dim) 

我仍然不确定,如果我完全理解,你想要实现什么,但这是我试图回答你的问题:

基本上,对于每组坐标,您需要执行以下操作:

  1. 获取原始图像的 ROI 剪切图。 (你已经有了。)
  2. 生成一些二进制掩码(相对于某些指标或您拥有的任何信息)以指示您想要操作的 ROI 内的哪个部分,例如您图像中显示的汽车。 在下面的代码中,我选择用一些高蓝色通道值来掩盖任何像素。
  3. 生成与 ROI 大小相同的混合图像,并使用与生成的蒙版相关的纯色填充所有像素。 在下面的代码中,我选择设置一些纯绿色。
  4. 使用 OpenCV 的addWeighted方法进行 alpha 混合。
  5. 将更新后的 ROI 剪切复制回原始图像。

这是我的示例图像和坐标的最终输出:

最终输出

那是我的代码:

import cv2
from matplotlib import pyplot as plt
import numpy as np

# Load image
orig = cv2.imread('path/to/your/image.png')
orig_back = orig.copy()

# Set up list of list of coordinates (i.e. rectangular ROIs)
list_of_coords = [[90, 0, 260, 30],
                  [60, 180, 100, 250],
                  [300, 300, 380, 370]]

# Set up alpha for alpha blending
alpha = 0.5

for coords in list_of_coords:

    # Get ROI cutout of image
    roi = orig_back[coords[1]:coords[3], coords[0]:coords[2]]
    roi_back = roi.copy()

    # Generate some mask; examplary here: Mask anything with some high
    # blue channel value
    mask = roi[:, :, 0] > 172

    # Generate blend image; exemplary here: Blend with solid green
    blend = np.zeros_like(roi)
    blend[mask, 1] = 255

    # Alpha blending original image with blend image
    roi[mask, :] = cv2.addWeighted(roi[mask, :], alpha,
                                   blend[mask, :], 1 - alpha,
                                   0)

    # Copy updated ROI cutout back to image
    orig[coords[1]:coords[3], coords[0]:coords[2]] = roi

    # Add rectangle to image for location
    orig = cv2.rectangle(orig,
                         (coords[0], coords[1]),
                         (coords[2], coords[3]),
                         (0, 255, 0),
                         2)

    # Some visualization output for better understanding
    plt.figure(0, figsize=(16, 8))
    plt.subplot(221)
    plt.imshow(cv2.cvtColor(roi_back, cv2.COLOR_BGR2RGB))
    plt.title('ROI cutout of image')
    plt.subplot(222)
    plt.imshow(mask, cmap='gray')
    plt.title('Masked portion of ROI with high blue channel value')
    plt.subplot(223)
    plt.imshow(cv2.cvtColor(blend, cv2.COLOR_BGR2RGB))
    plt.title('Blend image, solid green')
    plt.subplot(224)
    plt.imshow(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB))
    plt.title('Updated ROI cutout of image')
    plt.tight_layout()
    plt.show()


# Final output
cv2.imshow('Original image with updates and rectangles', orig)
cv2.waitKey(0)
cv2.destroyAllWindows()

而且,这是第一个 ROI 的附加可视化:

额外的可视化

希望这就是你要找的!

----------------------------------------
System information
----------------------------------------
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.5
Matplotlib:  3.3.1
NumPy:       1.19.1
OpenCV:      4.4.0
----------------------------------------

暂无
暂无

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

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