python - 删除 dicom 图像中的像素注释

标签 python image numpy image-processing dicom

我正在分析医学图像。所有图像都有一个带有位置的标记。看起来像这样 enter image description here

在这张图片中是“TRH RMLO”标注,但在其他图片中可以不一样。大小也不同。图像被裁剪,但您会看到组织从右侧开始。 我发现这些标记的存在扭曲了我的分析。

我怎样才能删除它们?

我像这样在 python 中加载图像

import dicom
import numpy as np

img = dicom.read_file(my_image.dcm)
img_array = img.pixel_array

图像是一个 numpy 数组。白色文本总是被一个大的黑色区域包围(黑色的值为零)。标记在每个图像中的位置不同。

如何在不损坏组织数据的情况下删除白色文本。

更新

添加了第二张图片

enter image description here

更新 2: 这是两个原始的 dicom 文件。所有个人信息已被删除。edit:removed

最佳答案

查看您提供的图像的实际像素值,您可以看到标记几乎 (99.99%) 是纯白色,而这不会出现在图像的其他地方,因此您可以使用简单的 99.99% 阈值将其隔离.

我更喜欢在命令行中使用 ImageMagick,所以我会这样做:

convert sample.dcm -threshold 99.99% -negate mask.png

enter image description here

convert sample.dcm mask.png -compose darken -composite result.jpg

enter image description here

当然,如果样本图片不具有代表性,可能还得加把劲。让我们看看...

如果简单的阈值对您的图像不起作用,我会查看“Hit and Miss Morphology”。基本上,您将图像阈值设为纯黑白 - 大约 90%,然后您寻找特定的形状,例如标签上的角标记。所以,如果我们想在黑色背景上寻找白色矩形的左上角,我们使用 0 来表示“这个像素必须是黑色”1 表示“这个像素必须是白色”- 表示“我们不关心”,我们将使用这种模式:

0 0 0 0 0
0 1 1 1 1
0 1 - - -
0 1 - - -
0 1 - - -

希望你能在那里看到一个白色矩形的左上角。在终端中就像这样:

convert sample.dcm -threshold 90% \
  -morphology HMT '5x5:0,0,0,0,0 0,1,1,1,1 0,1,-,-,- 0,1,-,-,- 0,1,-,-,-' result.png

现在我们还想寻找右上角、左下角和右下角,所以我们需要旋转图案,当您添加 > 时 ImageMagick 可以轻松完成 标志:

convert sample.dcm -threshold 90% \
   -morphology HMT '5x5>:0,0,0,0,0 0,1,1,1,1 0,1,-,-,- 0,1,-,-,- 0,1,-,-,-' result.png

enter image description here

希望您现在可以看到标定 Logo 角的点,这样我们就可以要求 ImageMagick 修剪所有无关黑色的图像,只留下白点,然后告诉我们边界框:

cconvert sample.dcm -threshold 90% \
   -morphology HMT '5x5>:0,0,0,0,0 0,1,1,1,1 0,1,-,-,- 0,1,-,-,- 0,1,-,-,-' -format %@ info:
308x198+1822+427

因此,如果我现在在这些坐标周围绘制一个红色框,您可以看到检测到标签的位置 - 当然在实践中我会绘制一个黑色框来覆盖它,但我正在解释这个想法:

convert sample.dcm -fill "rgba(255,0,0,0.5)" -draw "rectangle 1822,427 2130,625" result.png

enter image description here

如果您希望脚本自动执行此操作,我会使用类似这样的东西,将其保存为 HideMarker:

#!/bin/bash
input="$1"
output="$2"

# Find corners of overlaid marker using Hit and Miss Morphology, then get crop box
IFS="x+" read w h x1 y1 < <(convert "$input" -threshold 90% -morphology HMT '5x5>:0,0,0,0,0 0,1,1,1,1 0,1,-,-,- 0,1,-,-,- 0,1,-,-,-' -format %@ info:)

# Calculate bottom-right corner from top-left and dimensions
((x1=x1-1))
((y1=y1-1))
((x2=x1+w+1))
((y2=y1+h+1))
convert "$input" -fill black -draw "rectangle $x1,$y1 $x2,$y2" "$output"

然后你会这样做以使其可执行:

chmod +x HideMarker

然后像这样运行它:

./HideMarker someImage.dcm  result.png

关于python - 删除 dicom 图像中的像素注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714570/

相关文章:

python - apt-get 安装 python 文件还是 pip 安装?

python - 一维卷积的对称边界条件

python - Microsoft Bot Emulator 显示 "sending failed. Retry"。 VSCode 显示 KeyError : 'HTTP_CONTEXT_TYPE'

python - Pyspark:时间戳的平均值

python - 如何在 Tkinter 标签中使用 base64 编码的图像字符串?

python - 将满足特定条件的 scipy.sparse 矩阵的行设置为零

python - 使用带有 SELECT 的 sqlite 准备好的语句

python - 在 Windows 上将 cx_Freeze 安装到 python

html - 如何使背景图像和背景图像中的元素响应?

Java BufferedImage 从 Canvas 返回黑色图像