用可复现缺损掩膜修补彩色照片


原文:Fill in defects with inpainting,scikit-image 0.26.x 官方示例。原页无可确认的个人署名,维护方为 scikit-image team。以下完整翻译说明,保留完整原代码、结果图与参考资料,随后是独立编校说明。

图像修补(inpainting)[1] 是重建图像或视频中丢失、受损部分的过程。修复利用未受损区域中已有的信息自动进行。这个例子展示如何用基于双调和方程 [2][3][4] 的算法修补掩膜标出的像素。

完整示例

原例读取宇航员照片,生成六个块状缺损、几条细长缺损及随机小点,再把同一掩膜应用于所有颜色通道。最后调用双调和修补,绘制原图、掩膜、缺损图和结果图。为便于逐行核对,上游变量名、英文注释、图表标题和参数原样保留。

import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage.morphology import disk, dilation
from skimage.restoration import inpaint

image_orig = data.astronaut()

# Create mask with six block defect regions
mask = np.zeros(image_orig.shape[:-1], dtype=bool)
mask[20:60, 0:20] = 1
mask[160:180, 70:155] = 1
mask[30:60, 170:195] = 1
mask[-60:-30, 170:195] = 1
mask[-180:-160, 70:155] = 1
mask[-60:-20, 0:20] = 1

# Add a few long, narrow defects
mask[200:205, -200:] = 1
mask[150:255, 20:23] = 1
mask[365:368, 60:130] = 1

# Add randomly positioned small point-like defects
rstate = np.random.default_rng(0)
for radius in [0, 2, 4]:
    # larger defects are less common
    thresh = 3 + 0.25 * radius  # make larger defects less common
    tmp_mask = rstate.standard_normal(image_orig.shape[:-1]) > thresh
    if radius > 0:
        tmp_mask = dilation(tmp_mask, disk(radius, dtype=bool))
    mask[tmp_mask] = 1

# Apply defect mask to the image over the same region in each color channel
image_defect = image_orig * ~mask[..., np.newaxis]

image_result = inpaint.inpaint_biharmonic(image_defect, mask, channel_axis=-1)

fig, axes = plt.subplots(ncols=2, nrows=2)
ax = axes.ravel()

ax[0].set_title('Original image')
ax[0].imshow(image_orig)

ax[1].set_title('Mask')
ax[1].imshow(mask, cmap=plt.cm.gray)

ax[2].set_title('Defected image')
ax[2].imshow(image_defect)

ax[3].set_title('Inpainted image')
ax[3].imshow(image_result)

for a in ax:
    a.axis('off')

fig.tight_layout()
plt.show()
官方四联图:原始宇航员照片、缺损掩膜、缺损照片及双调和修补结果
scikit-image 0.26.x 官方页面发布的实际结果图;宇航员原照片来自 NASA。本次没有运行算法,不把此图标作本次实验输出。

官方页面记录脚本总运行时间为 0.471 秒,这是其文档构建环境的记录,不是性能承诺或本次实测。原页底部提供 Python 源码、Jupyter Notebook、ZIP 下载,以及通过 Binder 在浏览器运行示例的入口。

编校说明:掩膜怎样定义缺损

data.astronaut() 返回形状为 (512, 512, 3)、dtype 为 uint8 的彩色图像。image_orig.shape[:-1] 去掉颜色轴,因此 mask 是 512×512 的二维布尔数组:True/1 表示待修补像素,False/0 表示已知像素。

前六处切片生成矩形缺损;负索引从图像末端计数,因而有些缺损位于下半部分。后面三处切片生成细线。随机部分用 np.random.default_rng(0) 固定种子,处理半径 0、2、4,阈值 3 + 0.25 * radius 随半径上升,让较大缺损的种子更少;半径大于 0 时使用圆盘结构元素膨胀种子点。

mask[..., np.newaxis] 增加一个轴,使二维掩膜可以广播到所有颜色通道;~mask 对布尔值取反,乘法将缺损区域的三个通道同时置零。告诉修补算法“哪里未知”的是独立 mask,而不是“像素是否黑色”:原本完好的黑色像素不应自动被当成缺损。

channel_axis=-1 指定最后一维是颜色通道。已核对 v0.26.0 函数签名为 inpaint_biharmonic(image, mask, *, split_into_regions=False, channel_axis=None),要求 mask 与一个图像通道形状相同。源码会把图像转为浮点表示,返回值不能直接假定仍是原图的 uint8。

版本说明:本稿依据 0.26.x 页面及 v0.26.0 源码,不能直接套用旧版 multichannel 参数。本文所列的候选复现基线为 CPython 3.11、scikit-image 0.26.0、NumPy 2.2.6、SciPy 1.15.3、Matplotlib 3.10.3;它不是本次安装并验证通过的锁文件。固定种子有助于复现,但跨库版本和平台仍应核对完整环境、输入和输出。

输入、质量与适用边界

缺损掩膜全部由代码生成,不需要另找手绘 mask,也不用下载模型。astronaut 数据说明将照片归于 NASA Great Images,注明没有已知版权限制且已进入公有领域。官方数据加载器会在需要时尝试缓存或获取缺失数据;离线复现前应确认 astronaut.png 已随安装包或本地缓存准备好。

双调和修补利用邻域信息延续局部结构,不能证明恢复了缺损区域原本真实的内容。大面积纹理、人脸细节和复杂边缘未必能可靠重建。本例只有视觉对照,没有 PSNR、SSIM 或独立质量评分。不要把自然的修补结果误认为真实图像证据。

本次只读取公开页面、固定版本实现与数据说明,未安装软件、未运行教程,也未计算质量指标。实际项目还应检查图像尺寸、mask 形状与类型、内存使用,以及结果保存时的数据类型转换。

原文参考资料

署名与许可

scikit-image 官方示例;原文版权 © 2013–2025 the scikit-image team。宇航员照片来自 NASA Great Images,数据来源说明为公有领域。以下保留 v0.26.0 完整许可文件;其中 Files: * 的 BSD-3-Clause 适用于本例,其他文件例外一并列明。

Files: *
Copyright: 2009-2022 the scikit-image team
License: BSD-3-Clause

Files: doc/source/themes/scikit-image/layout.html
Copyright: 2007-2010 the Sphinx team
License: BSD-3-Clause

Files: skimage/feature/_canny.py
       skimage/filters/edges.py
       skimage/filters/_rank_order.py
       skimage/morphology/_skeletonize.py
       skimage/morphology/tests/test_watershed.py
       skimage/morphology/watershed.py
       skimage/segmentation/heap_general.pxi
       skimage/segmentation/heap_watershed.pxi
       skimage/segmentation/_watershed.py
       skimage/segmentation/_watershed_cy.pyx
Copyright: 2003-2009 Massachusetts Institute of Technology
           2009-2011 Broad Institute
           2003 Lee Kamentsky
           2003-2005 Peter J. Verveer
License: BSD-3-Clause

Files: skimage/filters/thresholding.py
       skimage/graph/_mcp.pyx
       skimage/graph/heap.pyx
Copyright: 2009-2015 Board of Regents of the University of
           Wisconsin-Madison, Broad Institute of MIT and Harvard,
           and Max Planck Institute of Molecular Cell Biology and
           Genetics
           2009 Zachary Pincus
           2009 Almar Klein
License: BSD-2-Clause

File: skimage/morphology/grayreconstruct.py
      skimage/morphology/tests/test_reconstruction.py
Copyright: 2003-2009 Massachusetts Institute of Technology
           2009-2011 Broad Institute
           2003 Lee Kamentsky
License: BSD-3-Clause

File: skimage/morphology/_grayreconstruct.pyx
Copyright: 2003-2009 Massachusetts Institute of Technology
           2009-2011 Broad Institute
           2003 Lee Kamentsky
           2022 Gregory Lee (added a 64-bit integer variant for large images)
License: BSD-3-Clause

File: skimage/segmentation/_expand_labels.py
Copyright: 2020 Broad Institute
           2020 CellProfiler team
License: BSD-3-Clause

File: skimage/exposure/_adapthist.py
Copyright: 1994 Karel Zuiderveld
License: BSD-3-Clause

Function: skimage/morphology/_skeletonize_various_cy.pyx:_skeletonize_loop
Copyright: 2003-2009 Massachusetts Institute of Technology
           2009-2011 Broad Institute
           2003 Lee Kamentsky
License: BSD-3-Clause

Function: skimage/_shared/version_requirements.py:_check_version
Copyright: 2013 The IPython Development Team
License: BSD-3-Clause

Function: skimage/_shared/version_requirements.py:is_installed
Copyright: 2009-2011 Pierre Raybaut
License: MIT

File: skimage/feature/_fisher_vector.py
Copyright: 2014 2014 Dan Oneata
License: MIT

File: skimage/_vendored/numpy_lookfor.py
Copyright: 2005-2023, NumPy Developers
License: BSD-3-Clause

File: skimage/transform/_thin_plate_splines.py
Copyright: 2007 Zachary Pincus
License: BSD-3-Clause

License: BSD-2-Clause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE HOLDERS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

License: BSD-3-Clause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the University nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.
.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE HOLDERS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

License: MIT

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容