【问题标题】:How do you directly overlay a scatter plot on top of a jpg image in matplotlib / Python?如何在 matplotlib / Python 中直接在 jpg 图像上叠加散点图?
【发布时间】:2011-07-01 16:44:36
【问题描述】:

我需要快速绘制作为跟踪算法输出的 jpg 帧。与 jpg 帧一起使用的是文本文件,其中包含用于定位正在跟踪的图像目标的简单 (x,y) 数据。我想使用 matplotlib 绘制 jpg 图像,然后覆盖从文本文件中读取并存储到 Pythonic 列表中的 (x,y) 数据的散点图。下面是绘制 jpg 图像的代码,但是在我对 matplotlib、scipy 和 PIL 手册和帮助页面所做的所有搜索中,我找不到任何解释如何维护这个绘图窗口并简单地覆盖散点图的东西图像中不同 (x,y) 位置的简单标记。非常感谢任何帮助。

import matplotlib.pyplot as plt;
im = plt.imread(image_name);
implot = plt.imshow(im);
plt.show()

【问题讨论】:

    标签: python image matplotlib overlay plot


    【解决方案1】:

    这应该可行:

    import matplotlib.pyplot as plt
    im = plt.imread('test.png')
    implot = plt.imshow(im)
    plt.plot([100,200,300],[200,150,200],'o')
    plt.show()
    

    请记住,图像中的每个像素都是 x,y 轴上的一个单位。 'o' 是让plot 函数使用圆而不是直线的简写方式。

    【讨论】:

    • 鉴于它是背景图像,您可能想要操作 alpha 通道,您可以这样做:im[:, :, -1] = .7
    【解决方案2】:

    pyplot.scatter() 函数正是为此量身定制的:

    import matplotlib.pyplot as plt
    im = plt.imread(image_name)
    implot = plt.imshow(im)
    
    # put a blue dot at (10, 20)
    plt.scatter([10], [20])
    
    # put a red dot, size 40, at 2 locations:
    plt.scatter(x=[30, 40], y=[50, 60], c='r', s=40)
    
    plt.show()
    

    请参阅documentation 了解更多信息。

    【讨论】:

    • +1 是的,这会将轴定向到典型的像素编号:(0,0) = 左上角
    • @Paul 请问典型的像素编号有什么好处:(0,0) = 左上角
    • 但是当我在plt.scatter中设置透明参数alpha时,图片的透明度也发生了变化……
    • 没有imshow可以做到这一点吗?当我必须绘制一百个这样的实例时,打开的显示窗口拒绝在我的系统中关闭并且阻塞了流程。
    【解决方案3】:

    我知道这已经得到解答,但同样 zorder 也可以工作。如果您想在散点图上方或下方放置一些东西,这很好

    import matplotlib as plt
    im = plt.imread(image_name)
    plt.imshow(im,zorder=1)
    plt.scatter(x,y,zorder=2)
    plt.show()
    

    lower zorder 表示它低于其他东西

    【讨论】:

      【解决方案4】:

      这是一种无需调用 imshow() 即可直接在图像上绘图的方法。

      它将绘图渲染为与输入具有相同形状的透明图像,然后将其 alpha 合成到输入图像上。

      import numpy as np
      import matplotlib.pyplot as plt
      import imageio
      from contextlib import contextmanager
      
      @contextmanager
      def plot_over(img, extent=None, origin="upper", dpi=100):
          h, w, d = img.shape
          assert d == 3
          if extent is None:
              xmin, xmax, ymin, ymax = -0.5, w + 0.5, -0.5, h + 0.5
          else:
              xmin, xmax, ymin, ymax = extent
          if origin == "upper":
              ymin, ymax = ymax, ymin
          elif origin != "lower":
              raise ValueError("origin must be 'upper' or 'lower'")
          fig = plt.figure(figsize=(w / dpi, h / dpi), dpi=dpi)
          ax = plt.Axes(fig, (0, 0, 1, 1))
          ax.set_axis_off()
          ax.set_xlim(xmin, xmax)
          ax.set_ylim(ymin, ymax)
          fig.add_axes(ax)
          fig.set_facecolor((0, 0, 0, 0))
          yield ax
          fig.canvas.draw()
          plot = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8).reshape(h, w, 4)
          plt.close(fig)
          rgb = plot[..., :3]
          alpha = plot[..., 3, None]
          img[...] = ((255 - alpha) * img.astype(np.uint16) + alpha * rgb.astype(np.uint16)) // 255
      
      img = imageio.imread("image.jpg")
      img_with_plot = img.copy()
      with plot_over(img_with_plot) as ax:
          ax.scatter(...)
          # etc
      imageio.imwrite("result.png", img_with_plot)
      

      【讨论】:

        猜你喜欢
        • 2012-06-26
        • 2021-07-11
        • 1970-01-01
        • 2012-07-14
        • 2020-10-10
        • 2014-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多