Python 如何查找通过matplotlib绘制的绘图传递的像素

Python 如何查找通过matplotlib绘制的绘图传递的像素,python,image,numpy,matplotlib,pixel,Python,Image,Numpy,Matplotlib,Pixel,我使用以下代码绘制函数: t = np.arange(0., 5., 0.2) plt.plot(t, (t**2)+10*np.sin(t)) plt.axis('off') 我想知道如何将绘图保存为0/1数组,如果绘图通过,则像素值为1,否则为0 接下来的一个问题是,如果我用一些线宽绘制绘图,我希望像素值只有在绘图的“中心”线上时才为1,否则为0。我该怎么做?谢谢大家! 可以通过多种方式将图形转换为RGBA阵列。 最简单的方法可能是将文件保存为PNG,然后使用plt.imread或类似工具

我使用以下代码绘制函数:

t = np.arange(0., 5., 0.2)
plt.plot(t, (t**2)+10*np.sin(t))
plt.axis('off')
我想知道如何将绘图保存为0/1数组,如果绘图通过,则像素值为1,否则为0


接下来的一个问题是,如果我用一些线宽绘制绘图,我希望像素值只有在绘图的“中心”线上时才为1,否则为0。我该怎么做?谢谢大家!

可以通过多种方式将图形转换为RGBA阵列。 最简单的方法可能是将文件保存为PNG,然后使用
plt.imread
或类似工具再次加载文件。如果这对您来说似乎有点迂回,那么您可以使用我在下面使用的
plot2img
,它抓取画布,并通过作为字符串缓冲区的中间表示将其转换为数组

之后,只需对图像进行阈值化并提取中轴,这可以使用
scikit image
提供的功能轻松完成

#/usr/bin/env python
"""
https://stackoverflow.com/q/62014554/2912349
"""
将numpy作为np导入
将matplotlib.pyplot作为plt导入
从matplotlib.backends.backend_agg导入图Canvasagg
从skimage.color导入rgb2gray
从skimage.filters导入阈值\u otsu
从skimage.形态学导入中轴
def plot2img(图,删除页边空白=真):
# https://stackoverflow.com/a/35362787/2912349
# https://stackoverflow.com/a/54334430/2912349
如果删除_页边距:
图子批次调整(左=0,下=0,右=1,上=1,wspace=0,hspace=0)
画布=图Canvasagg(图)
canvas.draw()
img_作为_字符串,(宽度,高度)=画布。打印到_缓冲区()
返回np.fromstring(img_as_string,dtype='uint8')。重塑((高度、宽度、4))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
t=np.arange(0,5,0.2)
y=(t**2)+10*np.sin(t)
#以较大的图形打印,以使生成的图像具有高分辨率
图,ax=plt.子批次(图尺寸=(20,20))
ax.绘图(t,y)
ax.轴(“关闭”)
#将图形转换为RGBA阵列
as_rgba=plot2img(图)
#关闭使用非交互式Agg后端制作的绘图,以便稍后打开另一个
plt.close(“全部”)
#对图像设置阈值
as_灰度=rgb2gray(as_rgba)
阈值=阈值大津(灰度)
as_bool=as_灰度<阈值
#找中线
中线=中间轴(如图所示)
#绘图结果
图(ax1,ax2)=plt.子批次(1,2)
ax1.imshow(如图所示,cmap='gray')
ax2.imshow(中线,cmap='gray')
plt.show()
#!/usr/bin/env python
"""
https://stackoverflow.com/q/62014554/2912349
"""

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.backends.backend_agg import FigureCanvasAgg

from skimage.color import rgb2gray
from skimage.filters import threshold_otsu
from skimage.morphology import medial_axis


def plot2img(fig, remove_margins=True):
    # https://stackoverflow.com/a/35362787/2912349
    # https://stackoverflow.com/a/54334430/2912349

    if remove_margins:
        fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

    canvas = FigureCanvasAgg(fig)
    canvas.draw()
    img_as_string, (width, height) = canvas.print_to_buffer()
    return np.fromstring(img_as_string, dtype='uint8').reshape((height, width, 4))


if __name__ == '__main__':

    t = np.arange(0., 5., 0.2)
    y = (t**2)+10*np.sin(t)

    # plot in a large figure such that the resulting image has a high resolution
    fig, ax = plt.subplots(figsize=(20, 20))
    ax.plot(t, y)
    ax.axis('off')

    # convert figure to an RGBA array
    as_rgba = plot2img(fig)

    # close plot made with non-interactive Agg backend so that we can open the other later
    plt.close('all')

    # threshold the image
    as_grayscale = rgb2gray(as_rgba)
    threshold = threshold_otsu(as_grayscale)
    as_bool = as_grayscale < threshold

    # find midline
    midline = medial_axis(as_bool)

    # plot results
    fig, (ax1, ax2) = plt.subplots(1, 2)
    ax1.imshow(as_bool, cmap='gray_r')
    ax2.imshow(midline, cmap='gray_r')
    plt.show()