我正在尝试使用滑块(例如,应用不同值的阈值操作)交互地更改图像内容。
当我执行代码时,出现了意外的行为:图形显示了两次,第一次我执行
如何仅将图像显示在 HBox 中?
当我使用滑块改变值时,得到以下结果 => After changing value。
我的代码如下:
#%matplotlib ipympl
%matplotlib widget
import matplotlib.pyplot as plt
import cv2
import numpy as np
import ipywidgets as widgets
from ipywidgets import HBox, IntSlider
from IPython.display import Image
def update_lines(change):
ret,thresh2 = cv2.threshold(img_gray,change.new,255,cv2.THRESH_BINARY)
plt.imshow(thresh2)
fig.canvas.flush_events()
image = cv2.imread("Untitled.jpg")
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret,thresh2 = cv2.threshold(img_gray,30,255,cv2.THRESH_BINARY)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
slider = IntSlider(
orientation='vertical',
step=1,
value=127,
min=0,
max=255
)
display(HBox([slider, fig.canvas]))
slider.observe(update_lines, names='value')
当我执行代码时,出现了意外的行为:图形显示了两次,第一次我执行
fig = plt.figure()
,第二次我执行 display(HBox([slider, fig.canvas]))
=> 参见 The figure is displayed twice。如何仅将图像显示在 HBox 中?
当我使用滑块改变值时,得到以下结果 => After changing value。