用Pyplot绘制图像上的输入点

发布于 2025-02-06 05:06:04 字数 947 浏览 1 评论 0原文

我想用Pyplot绘制图像,并在该图像上绘制一个点。 该点来自Pyplot中的输入字段。在这里,我有一个代码,您可以在其中提出点,但是在按Enter或搜索按钮后,它不会绘制重点。这是我的代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox

def imshow_rgb(img_bgr):
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
    plt.imshow(img_rgb)

ims = cv2.imread('plattegrondtekening.png', 1)
fig = plt.imshow(np.flipud(ims), cmap='gray', origin='lower')
plt.subplots_adjust(bottom=0.2)

initial_text = ""
x,y=[500,500]

def submit(text):
    x,y = list(map(int,text.split(",")))
    print(x,y)
    plt.plot(x, y, "ro")
    plt.show()
    
axbox = plt.axes([0.1, 0.05, 0.8, 0.075])
text_box = TextBox(axbox, 'search', initial=initial_text)
text_box.on_submit(submit)

plt.show()

图像绘图带有下面的输入字段,这是上面代码的输出

但是我希望它在x = 900和y = 800上显示一个点,当我在输入框中输入900,800时。

I want to plot an image with pyplot and on top of that image a point.
That point is from an input field in the pyplot. Here I have a piece of code, where you can put a point in, but after pressing enter, or search button it won't plot the point. Here is my code:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox

def imshow_rgb(img_bgr):
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
    plt.imshow(img_rgb)

ims = cv2.imread('plattegrondtekening.png', 1)
fig = plt.imshow(np.flipud(ims), cmap='gray', origin='lower')
plt.subplots_adjust(bottom=0.2)

initial_text = ""
x,y=[500,500]

def submit(text):
    x,y = list(map(int,text.split(",")))
    print(x,y)
    plt.plot(x, y, "ro")
    plt.show()
    
axbox = plt.axes([0.1, 0.05, 0.8, 0.075])
text_box = TextBox(axbox, 'search', initial=initial_text)
text_box.on_submit(submit)

plt.show()

image plot with input field below, this is the output of the code above

But I want that it shows a point on x=900 and y=800, when I enter 900,800 in the input box.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

快乐很简单 2025-02-13 05:06:04

我们必须首先使用plt.sca(ax)首先选择活动轴,并且为了刷新画布,我们可以使用fig.canvas.draw() and code>和图。 canvas.flush_events()

  • 替换fig = plt.imshow(np.flipud(ims),cmap ='灰色',oneration ='lower') with:

      fig = plt.figure()#保持数字以供以后使用。
     ax = plt.gca()#保留轴以进行以后使用。
     ax.imshow(np.flipud(ims),cmap ='灰色',origin ='lower')#在斧头斧头上显示图像
     
  • 替换plt.plt.plot(x,x,y,x,y,y,y,y,y,y,y,y,y,y of “ ro”)plt.show() with:

      plt.sca(ax)#设置活动轴
     plt.plot(x,y,“ ro”)
     图。canvas.draw()#刷新画布。
     图。CANVAS.FLUSH_EVENTS()
     

code示例:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox

ims = cv2.imread('plattegrondtekening.png', 1)
fig = plt.figure()  # Keep fig for later usage
ax = plt.gca()  # https://stackoverflow.com/questions/25505341/how-to-get-the-axesimages-from-matplotlib
ax.imshow(np.flipud(ims), cmap='gray', origin='lower')
plt.subplots_adjust(bottom=0.2)

initial_text = ""
x,y=[500,500]

def submit(text):
    x, y = list(map(int,text.split(",")))
    print(x,y)
    plt.sca(ax)  # https://stackoverflow.com/questions/19625563/matplotlib-change-the-current-axis-instance-i-e-gca
    plt.plot(x, y, "ro")
    fig.canvas.draw()  # https://stackoverflow.com/questions/4098131/how-to-update-a-plot-in-matplotlib
    fig.canvas.flush_events()
    
axbox = plt.axes([0.1, 0.05, 0.8, 0.075])
text_box = TextBox(axbox, 'search', initial=initial_text)
text_box.on_submit(submit)
plt.show()

We have to select the active axes first using plt.sca(ax) and for refreshing the canvas we may use fig.canvas.draw() and fig.canvas.flush_events().

  • Replace fig = plt.imshow(np.flipud(ims), cmap='gray', origin='lower') with:

     fig = plt.figure()  # Keep the figure for later usage.
     ax = plt.gca()  # Keep the axes for later usage.
     ax.imshow(np.flipud(ims), cmap='gray', origin='lower')  # Show the image on axes ax
    
  • Replace plt.plot(x, y, "ro") and plt.show() with:

     plt.sca(ax)  # Set active axes
     plt.plot(x, y, "ro")
     fig.canvas.draw()  # Refresh the canvas.
     fig.canvas.flush_events()
    

Code sample:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox

ims = cv2.imread('plattegrondtekening.png', 1)
fig = plt.figure()  # Keep fig for later usage
ax = plt.gca()  # https://stackoverflow.com/questions/25505341/how-to-get-the-axesimages-from-matplotlib
ax.imshow(np.flipud(ims), cmap='gray', origin='lower')
plt.subplots_adjust(bottom=0.2)

initial_text = ""
x,y=[500,500]

def submit(text):
    x, y = list(map(int,text.split(",")))
    print(x,y)
    plt.sca(ax)  # https://stackoverflow.com/questions/19625563/matplotlib-change-the-current-axis-instance-i-e-gca
    plt.plot(x, y, "ro")
    fig.canvas.draw()  # https://stackoverflow.com/questions/4098131/how-to-update-a-plot-in-matplotlib
    fig.canvas.flush_events()
    
axbox = plt.axes([0.1, 0.05, 0.8, 0.075])
text_box = TextBox(axbox, 'search', initial=initial_text)
text_box.on_submit(submit)
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文