首页 > 解决方案 > 如何在彩色边框内提取图像区域?

问题描述

我正在尝试根据图像上彩色框的边框提取图像的分段区域(见下文)。

我想提取黄色框中的图像区域。.

作为参考,我正在使用pdfplumberim.draw_rect函数从 PDF 中提取此图像,该函数需要 ImageMagick 和 Ghostscript。我到处寻找解决这个问题的方法,虽然 Mark Setchell 对Python: How to cut out a area with specific color from image (OpenCV, Numpy)问题的回答已经接近,但我遇到了一些意想不到的错误.

这是我到目前为止所尝试的:

import numpy as np
from PIL import Image, ImageFilter
impath = r'Path\to\drawn_p9_image.png'
im = Image.open(impath).convert('RGB')
na = np.array(im)
orig= na.copy()
im = im.filter(ImageFilter.MedianFilter(3))
yellowY, yellowX = np.where(np.all(na==[247,213,83],axis=2))
top, bottom = yellowY[0], yellowY[-1]

但是当我运行最后一行时,我收到了这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index 0 is out of bounds for axis 0 with size 0

所以 NumPy 数组实际上并没有捕获它应该捕获的数据。当我检查 NumPy 数组时,它输出如下:

>>> na
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)

我不确定为什么这种方法不起作用,并且正在寻找有关如何解决它的一些指导。如果可以提供更简单的解决方案,我可以在最终裁剪的图像中看到黄色边界。

标签: pythonnumpyimage-processingimagemagickpython-imaging-library

解决方案


正如马克在评论中已经指出的那样,黄色矩形没有[247, 213, 83]. 例如,ImageJ[255, 255, 0]返回纯黄色。因此,使用此值可能已经有所帮助。

然而,为了克服关于确定 RGB 值的不确定性,可能还会因平台、软件等而变化,我建议使用HSV 颜色空间的颜色阈值,这也适用于 Pillow,参见。模式

您只需要注意适当的值范围:例如,色调通道的值在[0 ... 360](degree) 范围内,这些值被映射到一个完整的 8 位无符号整数,即[0 ... 255]. [0 ... 100]同样,饱和度和值从(percent)映射到[0 ... 255]

剩下的就是找到合适的色调、饱和度和值范围(例如,使用一些HSV 颜色选择器),以及 NumPy 的布尔数组索引来掩盖给定图像中的黄色区域。

对于最终的裁剪,您可以添加一些额外的边框来摆脱黄色边框本身。

最后,这里有一些代码:

import numpy as np
from PIL import Image


# Convert degree range (0 - 360) to uint8 value range (0 - 255)
def deg_to_uint8(deg):
    return deg / 360 * 255


# Convert percentage range (0 - 100) to uint8 value range (0 - 255)
def perc_to_uint8(perc):
    return perc / 100 * 255


# Open image, and convert to HSV color space for NumPy slicing
img = Image.open('MDRBG.png')
hsv = np.array(img.convert('HSV'))

# Masking color-ish area via NumPy slicing using upper and/or lower
# bounds for hue, saturation, and value
box = hsv[..., 0] > deg_to_uint8(55)        # Hue > 55°
box &= hsv[..., 0] < deg_to_uint8(65)       # Hue < 65°
box &= hsv[..., 1] > perc_to_uint8(80)      # Saturation > 80%
box &= hsv[..., 2] > perc_to_uint8(80)      # Value > 80%

# Find x, y coordinates of masked area; extract first and last elements
xy = np.argwhere(box)
t, b = xy[[0, -1], 0]
l, r = xy[[0, -1], 1]

# For better cropping, maybe add some additional border
bl, bt, br, bb = (3, 3, 3, 3)

# Actual cropping of the image
crop = img.crop((l + bl, t + bt, r - br, b - bb))
crop.save('crop.png')

而且,这就是输出:

输出

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
NumPy:         1.20.2
Pillow:        8.1.2
----------------------------------------

推荐阅读