【问题标题】:How to extract area of an image within a colored border?如何在彩色边框内提取图像区域?
【发布时间】:2021-06-25 00:02:19
【问题描述】:

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

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

作为参考,我使用pdfplumberim.draw_rect 函数从PDF 中提取此图像,该函数需要ImageMagick 和Ghostscript。我到处寻找解决这个问题的方法,虽然 Mark Setchell 对问题 Python: How to cut out an 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)

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

【问题讨论】:

  • 您看到的重复的 255 是白色边框,因此看起来是正确的。我猜值 [247,213,83] 实际上并没有出现在您的图像中,请尝试将鼠标悬停在 GIMP 或 Photoshop 中的黄色上以获取坐标,然后查看 Numpy 数组中的该区域 - 记住将 Y 先放在 X 后.
  • 您可以使用magick image.png -format %@ info: 找到带有 ImageMagick 的修剪框,因此黄色
  • 如果您使用np.mean(image[...,0],axis=1) 获得每行像素的平均值,您将看到所有边框行的平均值为 255,而图像所在位置的平均值会更低,因为它更暗。同样,使用axis=0,您可以获得所有列的平均值,并找到平均值小于 255 的第一列和最后一列。
  • @MarkSetchell,感谢您的洞察力!我是图像处理的新手,对 numpy 不是很熟悉,这些指针对我有很大帮助!再次感谢您!

标签: python numpy image-processing imagemagick python-imaging-library


【解决方案1】:

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

尽管如此,为了克服关于确定 RGB 值的不确定性,可能还会因平台、软件等而变化,我建议使用 HSV color space 使用颜色阈值,它也可以使用 Pillow,参见。 modes.

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

剩下的就是找到合适的色调、饱和度和值范围(例如,使用一些HSV color picker)和 NumPy 的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
----------------------------------------

【讨论】:

  • 非常感谢——我在我的系统上进行了测试,它运行良好。再次感谢!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
  • 2016-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多