删除dicom图像中的像素注释 [英] remove pixel annotations in dicom image

查看:201
本文介绍了删除dicom图像中的像素注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这张图片中的TRH RMLO注释,但它可以在其他图像中有所不同。尺寸也各不相同。图像被裁剪,但您看到组织正在右侧开始。
我发现这些标记的存在会扭曲我的分析。



如何删除它们?



<我在这样的python中加载图像

  import dicom 
import numpy as np

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

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



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



更新



添加了第二张图片





UPDATE2
这是两个原始的dicom文件。所有个人信息都已删除。编辑:删除

解决方案

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



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

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

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



当然,如果样本图像不具代表性,您可能需要更加努力。让我们来看看......



如果简单的阈值对你的图像不起作用,我会看命中和错过形态。基本上,您将图像阈值设置为纯黑色和白色 - 大约90%,然后您会查找特定形状,例如标签上的角标记。因此,如果我们想在黑色背景上查找白色矩形的左上角,我们使用 0 来表示此像素必须为黑色 1 表示此像素必须为白色 - 表示我们不在乎,我们会使用这种模式:

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

希望你能看到白色矩形的左上角。在终端中就是这样:

 转换sample.dcm -threshold 90%\ 
-morphology HMT '5x5:0,0,0,0,0 0,1,1,1,1 0,1, - , - , - 0,1, - , - , - 0,1, - , - , - '结果.png

现在我们还要寻找右上角,左下角和右下角,所以我们需要旋转模式,当您添加> 标志时, ImageMagick 会轻易地执行:

 转换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, - , - , - ' - 格式%@ info:
308x198 + 1822 + 427

所以,如果我现在画一个在这些坐标周围的红色框,你可以看到标签被检测到的位置 - 当然在实践中我会画一个黑盒子来覆盖它,但我在解释这个想法:

 转换sample.dcm -fillrgba(255,0,0,0.5) - 矩形1822,427 2130,625result.png 

< img src =https://i.stack.imgur.com/L3hpk.pngalt =在此输入图像说明>



如果你想要一个脚本自动执行,我会使用类似的东西,将其保存为 HideMarker

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

#Find corne使用命中和缺失形态学的重叠标记的rs,然后获得裁剪框
IFS =x +读取w h x1 y1< <(转换为$ input-threshold 90%-morphology HMT'5x5> ;:0,0,0,0,0 0,1,1,1,1 0,1, - , - , - 0,1, - , - , - 0,1, - , - , - ' - 格式%@ info :)

#从左上角计算右下角和尺寸
((x1 = x1-1))
((y1 = y1-1))
((x2 = x1 + w + 1))
((y2 = y1 + h + 1))
转换$ input-fill black -drawrectangle $ x1,$ y1 $ x2,$ y2$ output

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

  chmod + x HideMarker 

并按以下方式运行:

  ./ HideMarker someImage.dcm result.png 


I am analyzing medical images. All images have a marker with the position. It looks like this

It is the "TRH RMLO" annotation in this image, but it can be different in other images. Also the size varies. The image is cropped but you see that the tissue is starting on the right side. I found that the presence of these markers distort my analysis.

How can I remove them?

I load the image in python like this

import dicom
import numpy as np

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

The image is then a numpy array. The white text is always surrounded by a large black area (black has value zero). The marker is in a different position in each image.

How can I remove the white text without hurting the tissue data.

UPDATE

added a second image

UPDATE2: Here are two of the original dicom files. All personal information has been removed.edit:removed

解决方案

Looking at the actual pixel values of the image you supplied, you can see that the marker is almost (99.99%) pure white and this doesn't occur elsewhere in the image so you can isolate it with a simple 99.99% threshold.

I prefer ImageMagick at the command-line, so I would do this:

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

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

Of course, if the sample image is not representative, you may have to work harder. Let's look at that...

If the simple threshold doesn't work for your images, I would look at "Hit and Miss Morphology". Basically, you threshold your image to pure black and white - at around 90% say, and then you look for specific shapes, such as the corner markers on the label. So, if we want to look for the top-left corner of a white rectangle on a black background, and we use 0 to mean "this pixel must be black", 1 to mean "this pixel must be white" and - to mean "we don't care", we would use this pattern:

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

Hopefully you can see the top left corner of a white rectangle there. That would be like this in the Terminal:

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

Now we also want to look for top-right, bottom-left and bottom-right corners, so we need to rotate the pattern, which ImageMagick handily does when you add the > flag:

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

Hopefully you can see dots demarcating the corners of the logo now, so we could ask ImageMagick to trim the image of all extraneous black and just leave the white dots and then tell us the bounding box:

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

So, if I now draw a red box around those coordinates, you can see where the label has been detected - of course in practice I would draw a black box to cover it but I am explaining the idea:

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

If you want a script to do that automagically, I would use something like this, saving it as 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"

Then you would do this to make it executable:

chmod +x HideMarker

And run it like this:

./HideMarker someImage.dcm  result.png

这篇关于删除dicom图像中的像素注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆