第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何找到通過使用 matplotlib 繪制的圖傳遞的像素

如何找到通過使用 matplotlib 繪制的圖傳遞的像素

我使用以下代碼繪制一個(gè)函數(shù):t = np.arange(0., 5., 0.2)plt.plot(t, (t**2)+10*np.sin(t))plt.axis('off')我想知道如何將繪圖保存為 0/1 數(shù)組,如果繪圖通過,像素值為 1,否則為 0。一個(gè)后續(xù)問題是,如果我用一些線寬繪制圖,我希望像素值只有在圖的“中心”線上時(shí)才為 1,否則為 0。我應(yīng)該怎么做?謝謝!
查看完整描述

1 回答

?
Cats萌萌

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊

可以通過多種方式將圖形轉(zhuǎn)換為 RGBA 數(shù)組。最簡單的可能是將文件另存為 PNG,然后使用plt.imread或類似的方式再次加載文件。如果這對你來說似乎是迂回的,你可以使用plot2img我在下面使用的,它抓取畫布并通過中間表示將其轉(zhuǎn)換為數(shù)組作為字符串緩沖區(qū)。

之后,只需對圖像進(jìn)行閾值化并提取中軸,使用scikit-image.

http://img1.sycdn.imooc.com//638eeea90001a35206070372.jpg

#!/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()


查看完整回答
反對 回復(fù) 2022-12-06
  • 1 回答
  • 0 關(guān)注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號