Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change content of image interactively using slider widgets

I am trying to change content of an image interactively using a slider (e.g. for applying a median operation with different kernel sizes).

While this works well if I only show one resulting image (cf commented lines), I run into trouble when using the subplot function, since the image will not get updated.

What am I missing?

%matplotlib inline
from ipywidgets import interact, widgets
import matplotlib.pyplot as plt
import warnings

from skimage.morphology import disk
from skimage.filters import rank
from skimage.color import rgb2gray
import skimage.data


def f(Median_Size):
    selem = disk(int(Median_Size))
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        img_median = rank.median(img_gray, selem=selem) 

    ax_neu.imshow(img_median, cmap="gray")
    fig.canvas.draw()
    #plt.imshow(img_median, cmap="gray") #This would work
    #plt.show()

image = skimage.data.camera() #plt.imread("Test.png")       
img_gray = rgb2gray(image)

fig = plt.figure(figsize=(6, 4))
ax_orig = fig.add_subplot(121) 
ax_neu = fig.add_subplot(122) 

ax_orig.imshow(img_gray, cmap="gray")
ax_neu.imshow(img_gray, cmap="gray")

interact(f, Median_Size=widgets.IntSlider(min=1,max=21,step=2,value=1)) 
like image 255
tfv Avatar asked Jan 19 '18 06:01

tfv


1 Answers

Using %matplotlib notebook

Instead of the inline backend, you may use the notebook backend. This will allow to call figure.canvas.draw() as expected from running the code as a script. Replace the line %matplotlib inline by

%matplotlib notebook

and restart the Kernel.

enter image description here

Using display

You may display the newly changed figure after it has been changed. The drawback is that it creates the output twice. A workaround would then be to put interact in a new cell and capture the output from the first cell.

%%capture
%matplotlib inline
from ipywidgets import interact, widgets
from IPython.display import display
import matplotlib.pyplot as plt
import warnings

from skimage.morphology import disk
from skimage.filters import rank
from skimage.color import rgb2gray
import skimage.data


def f(Median_Size):
    selem = disk(int(Median_Size))
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        img_median = rank.median(img_gray, selem=selem) 

    ax_neu.imshow(img_median, cmap="gray")
    fig.canvas.draw()
    display(fig)

image = skimage.data.camera() #plt.imread("Test.png")       
img_gray = rgb2gray(image)

fig = plt.figure(figsize=(6, 4))
ax_orig = fig.add_subplot(121) 
ax_neu = fig.add_subplot(122) 

ax_orig.imshow(img_gray, cmap="gray")
ax_neu.imshow(img_gray, cmap="gray")

In a new cell

interact(f, Median_Size=widgets.IntSlider(min=1,max=21,step=2,value=1));

The output would then look like: enter image description here

like image 100
ImportanceOfBeingErnest Avatar answered Nov 01 '22 21:11

ImportanceOfBeingErnest