如何提取彩色边框内的图像区域?

How to extract area of an image within a colored border?

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

I want to extract the area of the image within the yellow box..

作为参考,我正在使用 pdfplumber's im.draw_rect function, which requires ImageMagick and Ghostscript. I have looked everywhere I can for a solution to this problem, and while Mark Setchell's answer to the question 从 PDF 中提取此图像已经接近,我遇到了一些意外错误。

这是我目前尝试过的方法:

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)

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

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

然而,为了克服关于最终 RGB 值的不确定性,可能还因平台、软件等而异,我建议使用 HSV color space, which also works using Pillow, cf. modes.

的颜色阈值

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

剩下的就是找到合适的色相、饱和度和明度范围(例如,使用一些 HSV color picker), and NumPy's boolean array indexing 来掩盖给定图像中的黄色区域。

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

最后,这是一些代码:

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
----------------------------------------