You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

计算单张图像Shifted Delta倒谱时触发IndexError错误求助

解决Shifted Delta倒谱计算中的IndexError问题

问题描述

编写图像Shifted Delta倒谱计算代码时,触发IndexError: index 1740 is out of bounds for axis 1 with size 1740错误,错误出现在compute_delta_cepstrum函数的标注行。

错误代码

import numpy as np
import cv2
from scipy.fftpack import dct, idct

def compute_cepstrum(image):
    # 计算二维离散余弦变换(DCT)
    dct_image = dct(dct(image.T, norm='ortho').T, norm='ortho')
    # 计算对数幅度
    log_magnitude = np.log(np.abs(dct_image) + 1e-8)
    # 逆DCT得到倒谱
    cepstrum = idct(idct(log_magnitude.T, norm='ortho').T, norm='ortho')
    return cepstrum

def compute_delta_cepstrum(cepstrum, delta=1):
    rows, cols = cepstrum.shape
    delta_cepstrum = np.zeros((rows, cols))

    for i in range(rows):
        for j in range(cols):
            if i - delta >= 0:
                delta_cepstrum[i, j] = cepstrum[i, j] - cepstrum[i - delta, j]
            if j - delta >= 0:
                # 错误发生在此行
                delta_cepstrum[i, j] = cepstrum[i, j] - cepstrum[i, j - delta]

    return delta_cepstrum

def compute_shifted_delta_cepstrum(cepstrum, window_size=3, delta=1):
    rows, cols = cepstrum.shape
    shifted_delta_cepstrum = np.zeros((rows, cols, window_size * 2 + 1))

    for shift in range(-window_size, window_size + 1):
        shifted_delta_cepstrum[:, :, shift + window_size] = compute_delta_cepstrum(cepstrum, delta + shift)

    return shifted_delta_cepstrum

# 加载并预处理图像
image_path = '/content/drive/My Drive/RMC2/Full Clean Run/sample.jpg'  # 替换为你的图像路径
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
image = image.astype(np.float32) / 255.0

# 计算倒谱
cepstrum = compute_cepstrum(image)

# 计算Shifted Delta倒谱
shifted_delta_cepstrum = compute_shifted_delta_cepstrum(cepstrum)

print("Shifted Delta Cepstrum计算完成。")

错误回溯信息

IndexError                                Traceback (most recent call last)
<ipython-input-4-f742ff8a661b> in <cell line: 45>()
     43 
     44 # Compute shifted delta cepstrum
---> 45 shifted_delta_cepstrum = compute_shifted_delta_cepstrum(cepstrum)
     46 
     47 # Save or process the shifted delta cepstrum as needed

1 frames
<ipython-input-4-f742ff8a661b> in compute_delta_cepstrum(cepstrum, delta)
     21                 delta_cepstrum[i, j] = cepstrum[i, j] - cepstrum[i - delta, j]
     22             if j - delta >= 0:
---> 23                 delta_cepstrum[i, j] = cepstrum[i, j] - cepstrum[i, j - delta]
     24 
     25     return delta_cepstrum

IndexError: index 1740 is out of bounds for axis 1 with size 1740

错误原因分析

  1. 负delta导致索引越界:在compute_shifted_delta_cepstrum的循环中,shift会取到负数(如-3、-2),当delta=1时,delta+shift会变成负数(如1-3=-2)。此时j - delta等价于j - (-2) = j+2,当j接近图像列数上限时,j+2会超过数组最大索引(数组索引从0开始,列数为1740时最大索引是1739),触发越界错误。
  2. 差分逻辑覆盖问题:原代码中,行方向差分结果会被列方向差分结果覆盖,无法同时保留两个方向的差分信息。

修正代码

import numpy as np
import cv2
from scipy.fftpack import dct, idct

def compute_cepstrum(image):
    dct_image = dct(dct(image.T, norm='ortho').T, norm='ortho')
    log_magnitude = np.log(np.abs(dct_image) + 1e-8)
    cepstrum = idct(idct(log_magnitude.T, norm='ortho').T, norm='ortho')
    return cepstrum

def compute_delta_cepstrum(cepstrum, delta=1):
    rows, cols = cepstrum.shape
    delta_cepstrum = np.zeros((rows, cols))
    abs_delta = abs(delta)
    # 判断差分方向:负delta为向前差分,正delta为向后差分
    is_forward = delta < 0

    for i in range(rows):
        for j in range(cols):
            # 处理行方向差分
            if not is_forward:
                if i - abs_delta >= 0:
                    delta_cepstrum[i, j] += cepstrum[i, j] - cepstrum[i - abs_delta, j]
            else:
                if i + abs_delta < rows:
                    delta_cepstrum[i, j] += cepstrum[i + abs_delta, j] - cepstrum[i, j]
            # 处理列方向差分
            if not is_forward:
                if j - abs_delta >= 0:
                    delta_cepstrum[i, j] += cepstrum[i, j] - cepstrum[i, j - abs_delta]
            else:
                if j + abs_delta < cols:
                    delta_cepstrum[i, j] += cepstrum[i, j + abs_delta] - cepstrum[i, j]
    return delta_cepstrum

def compute_shifted_delta_cepstrum(cepstrum, window_size=3, delta=1):
    rows, cols = cepstrum.shape
    shifted_delta_cepstrum = np.zeros((rows, cols, window_size * 2 + 1))

    for shift in range(-window_size, window_size + 1):
        current_delta = delta + shift
        shifted_delta_cepstrum[:, :, shift + window_size] = compute_delta_cepstrum(cepstrum, current_delta)

    return shifted_delta_cepstrum

# 加载并预处理图像
image_path = '/content/drive/My Drive/RMC2/Full Clean Run/sample.jpg'  # 替换为你的图像路径
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
image = image.astype(np.float32) / 255.0

# 计算倒谱
cepstrum = compute_cepstrum(image)

# 计算Shifted Delta倒谱
shifted_delta_cepstrum = compute_shifted_delta_cepstrum(cepstrum)

print("Shifted Delta Cepstrum计算完成。")

修正说明

  1. 处理负delta场景:通过判断delta正负,分别执行向前/向后差分,确保索引始终在数组范围内,避免越界。
  2. 合并差分结果:将行和列方向的差分结果相加,保留两个维度的差分信息(若需分开存储,可调整为返回多通道数组)。

内容的提问来源于stack exchange,提问作者Qasim0787

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 08:32:03