【问题标题】:How to read an image with PyWavelets?如何使用 PyWavelets 读取图像?
【发布时间】:2019-06-25 20:58:28
【问题描述】:

我需要使用 pyWavelet,即pywt 读取我的图像为它制作小波,下面的示例仅用于加载相机图像,如何使用我计算机路径中的另一个图像?

import pywt
import pywt.data

# Load image
original = pywt.data.camera()

【问题讨论】:

    标签: python wavelet pywavelets


    【解决方案1】:

    我不确定您是否可以仅使用 pywt 读取图像,但您可以使用 OpenCV 加载图像,然后将其转换为可用于 pywt 的可用格式

    import cv2
    import numpy as np
    import pywt
    
    image = cv2.imread('1.png')
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Convert to float for more resolution for use with pywt
    image = np.float32(image)
    image /= 255
    
    # ...
    # Do your processing
    # ...
    
    # Convert back to uint8 OpenCV format
    image *= 255
    image = np.uint8(image)
    
    cv2.imshow('image', image)
    cv2.waitKey(0)
    

    【讨论】:

      【解决方案2】:

      OpenCV 的替代方案是 scikit-image。

      import pywt
      from skimage import io, color
      
      data = io.imread(filename)
      
      # Process your image
      gray = color.rgb2gray(data)
      coeffs = pywt.dwt2(gray, 'haar')
      
      # Or... process each channel separately
      r, g, b = [c.T for c in data.T]
      cr = pywt.dwt2(r, 'haar')
      cg = pywt.dwt2(r, 'haar')
      cb = pywt.dwt2(r, 'haar')
      
      
      # output: PIL, matplotlib, dump to file...
      

      【讨论】:

        【解决方案3】:

        你可以使用matplotlib和numpy:

        from matplotlib.image import imread
        import numpy as np
        import pywt
           
        A = imread("1.jpg")
        original = np.mean(A, -1)
        #rest of your codes
        

        【讨论】:

          【解决方案4】:

          我使用pandas 读取图像,因为我使用hm3.6 数据集进行运动预测并应用小波变换作为预处理。

          我的代码简单如下;

          path = ".../your_path"
          img = pd.read_csv(path + "h3.6m/dataset/S1/directions_1.txt") #read the image
          
          #if you want to apply DWT you can continue with dataframe    
          coeffs2 = dwt(image,  'bior1.3')
          titles = ['Approximation', ' Horizontal detail',
                        'Vertical detail', 'Diagonal detail']
          
          LL, LH = coeffs2
          

          【讨论】:

            【解决方案5】:

            您可以尝试以下方法。

            import numpy as np
            import matplotlib.pyplot as plt
            import pywt
            import pywt.data
            # Load image
            original = pywt.data.camera()
            # Wavelet transform of image, and plot approximation and details
            titles = ['Approximation', ' Horizontal detail', 'Vertical detail', 'Diagonal detail']
            coeffs2 = pywt.dwt2(original, 'bior1.3')
            LL, (LH, HL, HH) = coeffs2
            fig = plt.figure(figsize=(12, 3))
            for i, a in enumerate([LL, LH, HL, HH]):
            ax = fig.add_subplot(1, 4, i + 1)
            ax.imshow(a, interpolation="nearest", cmap=plt.cm.gray)
            ax.set_title(titles[i], fontsize=10)
            ax.set_xticks([])
            ax.set_yticks([])
            fig.tight_layout()
            plt.show()
            

            参考:https://pywavelets.readthedocs.io/en/latest/

            【讨论】:

            • 你并没有真正回答所提出的问题
            猜你喜欢
            • 1970-01-01
            • 2021-12-03
            • 2012-01-11
            • 1970-01-01
            • 2021-03-29
            • 2022-01-10
            • 2017-10-17
            • 2020-02-12
            • 1970-01-01
            相关资源
            最近更新 更多