【问题标题】:Drawing on image with matplotlib and opencv2, update image?使用 matplotlib 和 opencv2 在图像上绘图,更新图像?
【发布时间】:2014-03-06 18:22:19
【问题描述】:

我正在尝试使用 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 上绘制图像时更新它?

【问题讨论】:

    标签: python opencv matplotlib draw


    【解决方案1】:

    每次更新图像时都需要调用 plt.draw()。

    【讨论】:

    • 当我这样做时,我收到错误QCoreApplication::exec: The event loop is already running
    • 对不起,我记错了。您调用 draw not show 进行更新。
    • 使用plt.draw() 也不会更新图像。我相信这会更新我没有的情节。
    • canvas.draw()plt.gcf().canvas.draw() 更新情节。但是您还需要更新图中的数据。
    【解决方案2】:

    这是一种 hacky 答案,但它对我有用,所以我认为你可以尝试一下。这个想法是只删除子图的图像堆栈上的顶部项目,然后用更新的图像重新绘制它。

    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()
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2021-01-31
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      相关资源
      最近更新 更多