0

i'm trying to update three of the plots in my subplot. I think the problem is that the old image isn't cleared, so the new one is plotted on top of the old one..but i`m not really sure..maybe there is also another problem. But it's possible to plot a new image with cv2.imshow()...problem is that i need to plot multiple images in a subplot. How can i fix it ?

Depending on the position of the slider the mask is changing.

Thanks a lot!

img.fig2, img.ax2 = plt.subplots()
for i in range(3):
    plt.subplot(2, 3, i + 1)
    plt.imshow(img[i])
    plt.xticks([])
    plt.yticks([])

def update (val):
     ...........
   for i in range(3):
        res[i] = cv2.cvtColor(res[i], cv2.COLOR_HSV2RGB)
        fig2 = plt.figure(2)
        fig2.add_subplot(2, 3, 4 + i)
        plt.imshow(res[i])
        plt.xticks([])
        plt.yticks([])
    plt.draw()
plt.show()

whole code of interest:

fig2, ax2 = plt.subplots()
for i in range(3):
    plt.subplot(2, 3, i + 1)
    plt.imshow(img[i])
    plt.xticks([])
    plt.yticks([])
plt.pause(0.1)

def update(val):
    hsv1 = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
    hsv2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
    hsv3 = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV)

    l_b = np.array([shl.val, ssl.val, svl.val])
    u_b = np.array([shu.val, ssu.val, svu.val])

    mask1 = cv2.inRange(hsv1, l_b, u_b)
    mask2 = cv2.inRange(hsv2, l_b, u_b)
    mask3 = cv2.inRange(hsv3, l_b, u_b)

    res1 = cv2.bitwise_and(img1, img1, mask=mask1)
    res2 = cv2.bitwise_and(img2, img2, mask=mask2)
    res3 = cv2.bitwise_and(img3, img3, mask=mask3)
    res = [res1, res2, res3]

    plt.clf()
    for i in range(3):
        res[i] = cv2.cvtColor(res[i], cv2.COLOR_HSV2RGB)
        fig2 = plt.figure(2)
        fig2.add_subplot(2, 3, 4 + i)
        plt.imshow(res[i])
        plt.xticks([])
        plt.yticks([])
    fig2.canvas.draw_idle()
    plt.pause(0.01)
    plt.draw_all()



shl.on_changed(update)
ssl.on_changed(update)
svl.on_changed(update)
shu.on_changed(update)
ssu.on_changed(update)
svu.on_changed(update)


plt.show()
braX
  • 11,506
  • 5
  • 20
  • 33
Max
  • 1
  • 1
  • have you tried using ```plt.clf()``` ? – bjornsing Apr 02 '20 at 09:13
  • when i put plt.clf() another figure is cleared. How can i choose the figure which has to be cleared ? I'm also don`t sure anymore if it`s just plotting on top or if there is also another problem – Max Apr 02 '20 at 09:22

1 Answers1

0

You're right about imshow() : it adds new images on top of the existing ones and you need to call it only once before your loop. However, it also sets the color table and you'll need to provide it with a way to know the data range. Then you can call set_data() on the AxesImage imshow returned and you will need to call flush_events() to update the screen.

Sometimes flush_events() will happen transparently because another plot activates it, but you need to make sure it happens at least once.

Here is a working solution (link)

Camion
  • 1,264
  • 9
  • 22