spo*_*234 5 python numpy image image-processing dicom
我正在分析医学图像。所有图像都有一个带有位置的标记。看起来像这样

这是该图像中的“TRH RMLO”注释,但在其他图像中可能有所不同。而且大小也不同。图像已被裁剪,但您会看到组织从右侧开始。我发现这些标记的存在扭曲了我的分析。
我怎样才能删除它们?
我像这样在 python 中加载图像
import dicom
import numpy as np
img = dicom.read_file(my_image.dcm)
img_array = img.pixel_array
Run Code Online (Sandbox Code Playgroud)
那么图像就是一个 numpy 数组。白色文本始终被大片黑色区域包围(黑色值为零)。标记在每个图像中位于不同的位置。
如何在不损害组织数据的情况下删除白色文本。
更新
添加了第二张图片
UPDATE2:这是两个原始的 dicom 文件。所有个人信息已被删除。编辑:删除
查看您提供的图像的实际像素值,您可以看到标记几乎 (99.99%) 纯白色,并且这种情况不会发生在图像的其他位置,因此您可以使用简单的 99.99% 阈值将其隔离。
我更喜欢在命令行使用ImageMagick ,所以我会这样做:
convert sample.dcm -threshold 99.99% -negate mask.png
Run Code Online (Sandbox Code Playgroud)
convert sample.dcm mask.png -compose darken -composite result.jpg
Run Code Online (Sandbox Code Playgroud)
当然,如果样本图片不具有代表性,你可能还得下点功夫。让我们看看那个...
如果简单的阈值不适用于您的图像,我会看看“Hit and Miss Morphology”。基本上,您将图像阈值设置为纯黑和白(例如大约 90%),然后寻找特定的形状,例如标签上的角标记。因此,如果我们想要在黑色背景上查找白色矩形的左上角,并且我们使用0表示“该像素必须是黑色”,1表示“该像素必须是白色”并-表示“我们不不在乎”,我们会使用这种模式:
0 0 0 0 0
0 1 1 1 1
0 1 - - -
0 1 - - -
0 1 - - -
Run Code Online (Sandbox Code Playgroud)
希望您能在那里看到白色矩形的左上角。在终端中会是这样的:
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
Run Code Online (Sandbox Code Playgroud)
现在我们还想寻找右上角、左下角和右下角,因此我们需要旋转图案,当您添加标志时,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
Run Code Online (Sandbox Code Playgroud)
希望您现在可以看到划分徽标角的点,因此我们可以要求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
Run Code Online (Sandbox Code Playgroud)
因此,如果我现在在这些坐标周围画一个红色框,您可以看到检测到标签的位置 - 当然在实践中我会画一个黑色框来覆盖它,但我正在解释这个想法:
convert sample.dcm -fill "rgba(255,0,0,0.5)" -draw "rectangle 1822,427 2130,625" result.png
Run Code Online (Sandbox Code Playgroud)
如果您希望脚本自动执行此操作,我会使用类似的内容,将其另存为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"
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做以使其可执行:
chmod +x HideMarker
Run Code Online (Sandbox Code Playgroud)
并像这样运行它:
./HideMarker someImage.dcm result.png
Run Code Online (Sandbox Code Playgroud)