Python 代码:将语谱图、MFCC 特征图与原始特征图相乘生成新图片
import os
import cv2
import numpy as np
path_spectrograms = r'D:\论文代码\spectrograms'
path_spectrogram = r'D:\论文代码\语谱图'
path_mfcc = r'D:\论文代码\MFCC'
path_output = r'D:\论文代码\feature_multiplication'
# 获取所有子目录
subdirs = []
for root, dirs, files in os.walk(path_spectrograms):
for d in dirs:
subdirs.append(os.path.join(root, d))
# 处理每个子目录
for subdir in subdirs:
# 创建对应的输出目录
subdir_rel = os.path.relpath(subdir, path_spectrograms)
output_subdir = os.path.join(path_output, subdir_rel)
os.makedirs(output_subdir, exist_ok=True)
# 遍历子目录下的文件
for filename in os.listdir(subdir):
# 读取语谱图
spectrogram_path = os.path.join(subdir, filename)
spectrogram = cv2.imread(spectrogram_path, cv2.IMREAD_GRAYSCALE)
# 读取对应的特征图
spectrogram_rel = os.path.relpath(spectrogram_path, path_spectrograms)
feature_path = os.path.join(path_spectrogram, spectrogram_rel)
feature = cv2.imread(feature_path, cv2.IMREAD_GRAYSCALE)
feature = cv2.resize(feature, spectrogram.shape[::-1])
# 特征图相乘
feature = cv2.normalize(feature.astype(np.float32), None, 0, 1, cv2.NORM_MINMAX)
spectrogram = cv2.normalize(spectrogram.astype(np.float32), None, 0, 1, cv2.NORM_MINMAX)
result = (feature * spectrogram).astype(np.uint8)
# 读取对应的MFCC特征图
mfcc_path = os.path.join(path_mfcc, spectrogram_rel)
mfcc = cv2.imread(mfcc_path, cv2.IMREAD_GRAYSCALE)
mfcc = cv2.resize(mfcc, spectrogram.shape[::-1])
# 特征图相乘
mfcc = cv2.normalize(mfcc.astype(np.float32), None, 0, 1, cv2.NORM_MINMAX)
result = (result * mfcc).astype(np.uint8)
# 保存结果
output_path = os.path.join(output_subdir, filename)
cv2.imwrite(output_path, result)
原文地址: http://www.cveoy.top/t/topic/nI71 著作权归作者所有。请勿转载和采集!