将IMShow频谱图转换为图像

发布于 2025-01-24 15:23:42 字数 2074 浏览 5 评论 0原文

我想将此处显示的小波映像(没有tick或标签)保存到PNG文件。

我尝试遵循解决方案在此处发布用于保存频谱图,但是这种方法对我不起作用。

这就是我得到的:

”在此处输入图像描述”

这是我使用的代码:

import librosa
import librosa.display

import os
import pywt
import matplotlib.pyplot as plt
import soundfile as sf

import skimage.io

from tftb.generators import anasing
from mpl_toolkits.axes_grid1 import make_axes_locatable

import numpy as np
from ssqueezepy import cwt
from ssqueezepy.visuals import plot, imshow

# Set the path to the Binary Class dataset
fulldatasetpath = 'G:/AudioFile'

input_file = (r'G:/Audiofile.wav')


[data1, sample_rate1] = sf.read(input_file)
#[sample_rate1, data1] = wav.read(input_file);
duration = len(data1)/sample_rate1

time = np.arange(0, duration, 1/sample_rate1) #time vector

#%%##############            Take CWT & plot                ##################################################
Wx, scales = cwt(data1, 'morlet')

imshow(Wx,  abs=1)
plt.show()

Wx = abs(Wx)

#%%##############            SAVE TO IMAGE                ###########################################
def scale_minmax(X, min=0.0, max=1.0):
    X_std = (X - X.min()) / (X.max() - X.min())
    X_scaled = X_std * (max - min) + min
    return X_scaled

wave1 = np.log(Wx + 1e-9) # add small number to avoid log(0)

# min-max scale to fit inside 8-bit range
img = scale_minmax(Wx, 0, 255).astype(np.uint8)
img = np.flip(img, axis=0)  # put low frequencies at the bottom in image
img = 255-img # invert. make black==more energy
out = 'out.png'
# save as PNG
skimage.io.imsave(out, img)

I would like to save just the wavelet image (no ticks nor labels) shown here to a png file.

enter image description here

I tried to follow the solution posted here for saving a spectrogram plot, but this approach is not working for me.

This is what I get:

enter image description here

This is the code that I have used:

import librosa
import librosa.display

import os
import pywt
import matplotlib.pyplot as plt
import soundfile as sf

import skimage.io

from tftb.generators import anasing
from mpl_toolkits.axes_grid1 import make_axes_locatable

import numpy as np
from ssqueezepy import cwt
from ssqueezepy.visuals import plot, imshow

# Set the path to the Binary Class dataset
fulldatasetpath = 'G:/AudioFile'

input_file = (r'G:/Audiofile.wav')


[data1, sample_rate1] = sf.read(input_file)
#[sample_rate1, data1] = wav.read(input_file);
duration = len(data1)/sample_rate1

time = np.arange(0, duration, 1/sample_rate1) #time vector

#%%##############            Take CWT & plot                ##################################################
Wx, scales = cwt(data1, 'morlet')

imshow(Wx,  abs=1)
plt.show()

Wx = abs(Wx)

#%%##############            SAVE TO IMAGE                ###########################################
def scale_minmax(X, min=0.0, max=1.0):
    X_std = (X - X.min()) / (X.max() - X.min())
    X_scaled = X_std * (max - min) + min
    return X_scaled

wave1 = np.log(Wx + 1e-9) # add small number to avoid log(0)

# min-max scale to fit inside 8-bit range
img = scale_minmax(Wx, 0, 255).astype(np.uint8)
img = np.flip(img, axis=0)  # put low frequencies at the bottom in image
img = 255-img # invert. make black==more energy
out = 'out.png'
# save as PNG
skimage.io.imsave(out, img)

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

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

发布评论

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

评论(1

殤城〤 2025-01-31 15:23:42

您可以设置轴的位置以覆盖整个图形,也可以使用firfigsize播放。例如:

import matplotlib.pyplot as plt
import numpy as np
from ssqueezepy import imshow

# test image
img = np.zeros((500, 40000, 3), dtype=int)
for i in range(img.shape[1]):
    img[:, i, 0] = int(abs(1 - 2 * i / img.shape[1]) * 255)

# create a figure and set the size
f = plt.figure(figsize=(8, 4))
# add a new axis into which ssqueezepy is going to plot
ax = f.add_subplot()
imshow(img)
# turn off tick labels
ax.axis(False)
# make the axis to cover the entire figure
ax.set_position([0, 0, 1, 1])

f.savefig("result.png")

You can set the position of the axis to cover the entire figure, and you can also play with figsize. For example:

import matplotlib.pyplot as plt
import numpy as np
from ssqueezepy import imshow

# test image
img = np.zeros((500, 40000, 3), dtype=int)
for i in range(img.shape[1]):
    img[:, i, 0] = int(abs(1 - 2 * i / img.shape[1]) * 255)

# create a figure and set the size
f = plt.figure(figsize=(8, 4))
# add a new axis into which ssqueezepy is going to plot
ax = f.add_subplot()
imshow(img)
# turn off tick labels
ax.axis(False)
# make the axis to cover the entire figure
ax.set_position([0, 0, 1, 1])

f.savefig("result.png")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文