如何在jupyter笔记本中使用python向图像添加视觉注释?

2024-02-22

总体目标是从某个服务器查看医学图像,向图像添加视觉注释(即,用红色突出显示/圈出肿瘤),然后将这些图像连同注释作为某种元数据上传回服务器。

查看/上传/存储元数据基本上已经完成,但我还没有找到任何允许我在 jupyter 笔记本中绘制图像的包。这可以操纵像素或者本质上在显示的图像之上创建一个新图像。

我目前正在使用 matplotlib 显示图像数据。

我见过一些使用 javascript 来跟踪鼠标移动和击键的方法,但不知道如何利用它来发挥我的优势。我可以使用任何包,只要它可以在 jupyter 中运行。


感谢@michael_j_ward 的建议,我找到了解决方案。我浏览了这些讨论和教程,并阅读了 matplotlib 轴的文档。这是我想出的/改变的

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import numpy as np
import dicom


class Annotator(object):
    def __init__(self, axes):
        self.axes = axes

        self.xdata = []
        self.ydata = []

    def mouse_move(self, event):
        if not event.inaxes:
            return

        x, y = event.xdata, event.ydata

        self.xdata.append(x)
        self.ydata.append(y)
        line = Line2D(self.xdata,self.ydata)
        line.set_color('r')
        self.axes.add_line(line)

        plt.draw()

    def mouse_release(self, event):
        # Erase x and y data for new line
        self.xdata = []
        self.ydata = []

path = '../sample.dcm'

data = dicom.read_file(path)

img = data.pixel_array

fig, axes = plt.subplots()
axes.imshow(img[0])
plt.axis("off")
plt.gray()
annotator = Annotator(axes)
plt.connect('motion_notify_event', cursor.mouse_move)
plt.connect('button_release_event', cursor.mouse_release)

axes.plot()

plt.show()

截屏 https://i.stack.imgur.com/ohezn.jpg

它允许我打开图像并在它们上绘图以突出显示或注释图像的重要部分。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在jupyter笔记本中使用python向图像添加视觉注释? 的相关文章

随机推荐