且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-23 16:29:28

由于@michael_j_ward的建议,我找到了一个解决方案.我浏览了这些讨论和教程,并阅读了matplotlib轴的文档.这就是我想出的/已更改的

Thanks to the suggestions from @michael_j_ward, I found a solution. I looked through those discussions and tutorials as well as read the documentation for the matplotlib axes. This is what I came up with/altered

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()

屏幕截图

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

It allows me to open images and draw on them to highlight or annotate significant portions of the image.