从图像标签创建边界框
Create Bounding Boxes from Image Labels
我有一张二维彩色图像和一张标签图像(标签的投影)
标签图片输出如下:
[[16 16 16 ... 16 16 16 ]
[16 16 16 ... 16 16 16 ]
[16 16 16 ... 16 16 16 ]
...
[ 2 2 2 ... 2 2 2 ]
[ 2 2 2 ... 2 2 2 ]
[ 2 2 2 ... 2 2 2 ]]
如何在原始二维彩色图像中的所有对象(由标签表示)周围绘制边界框?
使用 NumPy 的 boolean array indexing and OpenCV's boundingRect 函数可以轻松完成此任务。
来自 here,我拍摄了这张照片
和这个分割掩码
掩码是索引图像,OpenCV 有问题,另请参阅 . That's why, we'll also use Pillow 了解此任务。
代码如下:
import cv2
import numpy as np
from PIL import Image
# Read color image
img = cv2.imread('0.jpg')
# Read mask; OpenCV can't handle indexed images, so we need Pillow here
# for that, see also:
mask = np.array(Image.open('0_mask.png'))
# Iterate all colors in mask
for color in np.unique(mask):
# Color 0 is assumed to be background or artifacts
if color == 0:
continue
# Determine bounding rectangle w.r.t. all pixels of the mask with
# the current color
x, y, w, h = cv2.boundingRect(np.uint8(mask == color))
# Draw bounding rectangle to color image
out = cv2.rectangle(img.copy(), (x, y), (x+w, y+h), (0, int(color), 0), 2)
# Show image with bounding box
cv2.imshow('img_' + str(color), out)
# Show mask
cv2.imshow('mask', mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
这些是输出:
我决定输出几张图片,因为对于给定的图片,边界框严重重叠。要将所有矩形绘制到同一个图像,只需将相应的 rectangle
命令替换为
img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, int(color), 0), 2)
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
NumPy: 1.19.2
OpenCV: 4.4.0
Pillow: 7.2.0
----------------------------------------
我有一张二维彩色图像和一张标签图像(标签的投影)
标签图片输出如下:
[[16 16 16 ... 16 16 16 ]
[16 16 16 ... 16 16 16 ]
[16 16 16 ... 16 16 16 ]
...
[ 2 2 2 ... 2 2 2 ]
[ 2 2 2 ... 2 2 2 ]
[ 2 2 2 ... 2 2 2 ]]
如何在原始二维彩色图像中的所有对象(由标签表示)周围绘制边界框?
使用 NumPy 的 boolean array indexing and OpenCV's boundingRect 函数可以轻松完成此任务。
来自 here,我拍摄了这张照片
和这个分割掩码
掩码是索引图像,OpenCV 有问题,另请参阅
代码如下:
import cv2
import numpy as np
from PIL import Image
# Read color image
img = cv2.imread('0.jpg')
# Read mask; OpenCV can't handle indexed images, so we need Pillow here
# for that, see also:
mask = np.array(Image.open('0_mask.png'))
# Iterate all colors in mask
for color in np.unique(mask):
# Color 0 is assumed to be background or artifacts
if color == 0:
continue
# Determine bounding rectangle w.r.t. all pixels of the mask with
# the current color
x, y, w, h = cv2.boundingRect(np.uint8(mask == color))
# Draw bounding rectangle to color image
out = cv2.rectangle(img.copy(), (x, y), (x+w, y+h), (0, int(color), 0), 2)
# Show image with bounding box
cv2.imshow('img_' + str(color), out)
# Show mask
cv2.imshow('mask', mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
这些是输出:
我决定输出几张图片,因为对于给定的图片,边界框严重重叠。要将所有矩形绘制到同一个图像,只需将相应的 rectangle
命令替换为
img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, int(color), 0), 2)
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
NumPy: 1.19.2
OpenCV: 4.4.0
Pillow: 7.2.0
----------------------------------------