【问题标题】:Calculating Energy of image frame using DWT in python shows wrong value在python中使用DWT计算图像帧的能量显示错误值
【发布时间】:2016-06-22 14:43:03
【问题描述】:

我想找到图像帧的能量。 这就是我在 Matlab 中的计算方式。

[~,LH,HL,HH] = dwt2(rgb2gray(maskedImage),'db1'); % applying dwt 
E = HL.^2 + LH.^2 + HH.^2;    % Calculating the energy of each pixel.
Eframe = sum(sum(E))/(m*n); % m,n row and columns of image.

当我在 python 中为相同的图像编程时,Energy 的值显示为 170,预期为 0.7 我的程序哪里出错了请指教

#!usr/bin/python
import numpy as np
import cv2
import pywt
im = cv2.cvtColor(maskedimage,cv2.COLOR_BGR2GRAY)
m,n = im.shape
cA, (cH, cV, cD) = pywt.dwt2(im,'db1')
# a - LL, h - LH, v - HL, d - HH as in matlab
cHsq = [[elem * elem for elem in inner] for inner in cH]
cVsq = [[elem * elem for elem in inner] for inner in cV]
cDsq = [[elem * elem for elem in inner] for inner in cD]
Energy = (np.sum(cHsq) + np.sum(cVsq) + np.sum(cDsq))/(m*n)
print Energy

【问题讨论】:

    标签: python matlab dwt


    【解决方案1】:

    您分析的问题在于 numpy 数组和 MATLAB 矩阵的顺序不同(默认情况下)。二维 numpy 数组的第一维是行,而二维 MATLAB 矩阵的第一维是列。 dwt2 函数取决于此顺序。所以为了得到与dwt2相同的输出,需要在使用前对numpy数组进行转置。

    此外,dwt2 输出 numpy 数组,而不是列表,因此您可以像在 MATLAB 中那样直接对它们进行数学运算。

    此外,您可以使用size 获得图像的总大小,从而不必将mn 相乘。

    因此,假设您的颜色通道顺序正确(BGR 与 RGB),这应该会为 MATLAB 提供等效的结果:

    #!usr/bin/python
    import cv2
    from pywt import dwt2
    
    im = cv2.cvtColor(maskedimage, cv2.COLOR_BGR2GRAY)
    _, (cH, cV, cD) = dwt2(im.T, 'db1')
    # a - LL, h - LH, v - HL, d - HH as in matlab
    Energy = (cH**2 + cV**2 + cD**2).sum()/im.size
    
    print Energy
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多