在matplotlib和opencv2中绘制图像,如何更新图像?

0 投票
2 回答
2040 浏览
提问于 2025-04-17 21:10

我正在尝试使用matplotlib和opencv2在一张图片上绘制一个遮罩,但我在matplotlib的图表中叠加这些图片,并跟踪鼠标事件。目前我可以在图片上绘制,但它在视图中并没有更新。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
import math

class Painter(object):

    def __init__(self, ax, img):
        self.showverts = True
        self.figure = plt.figure(1)
        self.button_pressed = False
        self.img = img
        self.brush_size = 50
        self.color = 255

        canvas = self.figure.canvas
        canvas.mpl_connect('button_press_event', self.button_press_callback)
        canvas.mpl_connect('button_release_event', self.button_release_callback)
        canvas.mpl_connect('motion_notify_event', self.on_move)

    def button_press_callback(self, event):
        if(event.button == 1):
            self.button_pressed = True
            x = int(math.floor(event.xdata))
            y = int(math.floor(event.ydata))
            cv2.circle(self.img, (x, y), int(self.brush_size / 2), (self.color, self.color, self.color), -1)
            #update the image

    def button_release_callback(self, event):
        self.button_pressed = False
        cv2.imwrite('test.png', self.img)

    def on_move(self, event):
        if(self.button_pressed):
            x = int(math.floor(event.xdata))
            y = int(math.floor(event.ydata))
            cv2.circle(self.img, (x, y), int(self.brush_size / 2), (self.color, self.color, self.color), -1)
            #update the image

def draw_demo():
    imgOver = np.zeros((717,1465,3), np.uint8)
    imgMain = mpimg.imread('zebra.png')
    #imgMain = np.random.uniform(0, 255, size=(500, 500))

    ax = plt.subplot(111)
    ax.imshow(imgMain, interpolation='nearest', alpha=1)
    ax.imshow(imgOver, interpolation='nearest', alpha=0.6)

    pntr = Painter(ax, imgOver)
    plt.title('Click on the image to draw')
    plt.show()

if __name__ == '__main__':
    draw_demo()

我该如何在绘制时更新pyplot中的图片呢?

2 个回答

1

每次你更新图像的时候,都需要调用 plt.draw() 这个命令。

0

这是一种有点小技巧的解决办法,但对我来说有效,所以我觉得你也可以试试。这个方法的思路就是删除子图像堆栈最上面的那一项,然后用更新后的图像重新绘制它。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
import math

class Painter(object):
    def __init__(self, ax, img):
        self.showverts = True
        self.figure = plt.figure(1)
        self.button_pressed = False
        self.img = img
        self.brush_size = 50
        self.ax = ax
        self.color = 255

        canvas = self.figure.canvas
        canvas.mpl_connect('button_press_event', self.button_press_callback)
        canvas.mpl_connect('button_release_event', self.button_release_callback)
        canvas.mpl_connect('motion_notify_event', self.on_move)

    def button_press_callback(self, event):
        if(event.button == 1):
            self.button_pressed = True
            x = int(math.floor(event.xdata))
            y = int(math.floor(event.ydata))
            cv2.circle(self.img, (x, y), int(self.brush_size / 2), (self.color, self.color, self.color), -1)
            #update the image

    def button_release_callback(self, event):
        self.button_pressed = False
        self.ax.images.pop()
        self.ax.imshow(self.img, interpolation='nearest', alpha=0.6)
        plt.draw()
        cv2.imwrite('test.png', self.img)

    def on_move(self, event):
        if(self.button_pressed):
            x = int(math.floor(event.xdata))
            y = int(math.floor(event.ydata))
            cv2.circle(self.img, (x, y), int(self.brush_size / 2), (self.color, self.color, self.color), -1)
            #update the image

def draw_demo():
    global imgMain
    imgOver = np.zeros((717,1465,3), np.uint8)
    imgMain = mpimg.imread('zebra.png')
    #imgMain = np.random.uniform(0, 255, size=(500, 500))

    ax = plt.subplot(111)
    ax.imshow(imgMain, interpolation='nearest', alpha=1)
    ax.imshow(imgOver, interpolation='nearest', alpha=0.6)

    pntr = Painter(ax, imgOver)
    plt.title('Click on the image to draw')
    plt.show()

if __name__ == '__main__':
    draw_demo()

撰写回答