查看您提供的图像的实际像素值,您可以看到标记几乎是 (99.99%) 纯白色,并且这不会出现在图像的其他地方,因此您可以使用简单的 99.99% 阈值将其隔离.
我更喜欢命令行中的 ImageMagick,所以我会这样做:
convert sample.dcm -threshold 99.99% -negate mask.png
convert sample.dcm mask.png -compose darken -composite result.jpg
当然,如果示例图片不具有代表性,您可能需要更加努力。让我们看看……
如果简单阈值不适用于您的图像,我会查看“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
希望您现在可以看到划分徽标角落的点,因此我们可以要求 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
如果您希望脚本自动执行此操作,我会使用类似的内容,将其保存为 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