gpt4 book ai didi

python - 使用 matplotlib 和 opencv2 在图像上绘图,更新图像?

转载 作者:行者123 更新时间:2023-12-02 17:50:39 25 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 和 opencv2 在图像上绘制蒙版,但将图像覆盖在 matplotlib 图中,然后跟踪鼠标事件。我目前可以在图像上绘图,但它不会在 View 中更新。

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 上的图像?

最佳答案

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

关于python - 使用 matplotlib 和 opencv2 在图像上绘图,更新图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22232812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com