将移动的水平线添加到ipython交互式小部件

时间:2014-04-28 21:49:37

标签: ipython ipython-notebook

根据找到here的教程,我试图创建一个IPython 2.0小部件,它可以让我向图像添加一条或多条水平线,然后根据滑块的运动垂直移动它们。我当前的尝试看起来像(来自IPython 2.0笔记本):

from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display
import skimage
from skimage import data, filter, io

i = data.coffee()
io.Image(i)

def add_lines(image, line1 = 100, line2 = 200):
  new_image = image
  new_image[line1,:,:] = 0
  new_image[line2,:,:] = 0
  new_image = io.Image(new_image)
  display(new_image)
  return new_image

lims = (0,400,2)
w = interactive(add_lines, image=fixed(i), line1 = lims, line2 = lims)
display(w)

结果如下: lines are added, image is not redrawn

相反,我希望图像使用相同的背景图像重绘,并根据滑块值更新行位置。使用IPython交互式窗口小部件机制,有没有办法重用“基础”图像并用新行重新绘制它?

如果重要,我当前安装的版本是:

$ ipython --version

2.0.0

$ python --version

Python 2.7.6 :: Anaconda 1.9.0(x86_64)

1 个答案:

答案 0 :(得分:2)

您应该处理原始图像的副本,如下所示:

from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display
import skimage
from skimage import data, filter, io

i = data.coffee()

def add_lines(image, line1 = 100, line2 = 200):
  new_image = image.copy()  # Work on a copy
  new_image[line1,:,:] = 0
  new_image[line2,:,:] = 0
  new_image = io.Image(new_image)
  display(new_image)

lims = (0,400,2)
interactive(add_lines, image=fixed(i), line1 = lims, line2 = lims)
相关问题