小波变换背景预测matlab和python样例

news/2025/2/24 17:18:29

小波变换使用matlabpython

注意1d和2d的函数区别。注意默认参数问题。最终三个版本结果能够对齐。

matlab

load('wave_in.mat')

% res: image of 1536 x 1536
th=1;
dlevel=7;
wavename='db6';

[m,n] = wavedec2(res, dlevel, wavename);

vec = zeros(size(m));
vec(1:n(1)*n(1)*1) = m(1:n(1)*n(1)*1);

background =  waverec2(vec, n, wavename);
background(background<0.001)=0;

python version1

python">import time
import torch
from mat_utils import load_mat
import numpy as np
import pywt
import copy

data_dict = load_mat("test_data/wave_in.mat")
res = data_dict["res"]
# res: image of 1536 x 1536

data_torch = torch.from_numpy(res)

wavelet = pywt.Wavelet('db6')

wavename = 'db6'
dlevel = 7

coeffs_py = pywt.wavedec2(res, wavename, level=dlevel)

# keep fist one and zero all others
for i in range(1, len(coeffs_py)):
    sub_coefs = list(coeffs_py[i])
    sub_coefs = [np.zeros_like(tensor) for tensor in sub_coefs]
    coeffs_py[i] = sub_coefs

background_py = pywt.waverec2(coeffs_py, wavename)

python version2 with pytorch

使用库

https://github.com/v0lta/PyTorch-Wavelet-Toolbox

python">import time
import torch
from mat_utils import load_mat
import numpy as np
import pywt
import ptwt
# pip install ptwt

data_dict = load_mat("test_data/wave_in.mat")
res = data_dict["res"]
# res: image of 1536 x 1536

data_torch = torch.from_numpy(res)

wavename = 'db6'
dlevel = 7

wavelet = pywt.Wavelet(wavename)

coeffs_pt = ptwt.wavedec2(data_torch, wavelet, level=dlevel, mode='symmetric')
coeffs_pt = list(coeffs_pt)

# keep fist one and zero all others
for i in range(1, len(coeffs_pt)):
    sub_coefs = list(coeffs_pt[i])
    sub_coefs = [torch.zeros_like(tensor) for tensor in sub_coefs]
    coeffs_pt[i] = tuple(sub_coefs)

coeffs_pt = tuple(coeffs_pt)
background_pt = ptwt.waverec2(coeffs_pt, wavelet).squeeze()

background_pt_np = background_pt.numpy()


http://www.niftyadmin.cn/n/5864639.html

相关文章

C语言多人聊天室 ---chat(客户端聊天)

head.h #ifndef __HEAD_H #define __HEAD_H// 常用头文件 #include <stdio.h> #include <stdlib.h> #include <string.h>// 网络编程涉及的头文件 #include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h>#include <…

给小米/红米手机root(工具基本为官方工具)——KernelSU篇

目录 前言准备工作下载刷机包xiaomirom下载刷机包【适用于MIUI和hyperOS】“hyper更新”微信小程序【只适用于hyperOS】 下载KernelSU刷机所需程序和驱动文件 开始刷机设置手机第一种刷机方式【KMI】推荐提取boot或init_boot分区 第二种刷机方式【GKI】不推荐 结语 前言 刷机需…

【HeadFirst系列之HeadFirst设计模式】第10天之迭代器与组合模式:遍历与管理的艺术

迭代器与组合模式&#xff1a;遍历与管理的艺术 在《Head First 设计模式》中&#xff0c;**迭代器模式&#xff08;Iterator Pattern&#xff09;和组合模式&#xff08;Composite Pattern&#xff09;**是两个非常重要的设计模式。迭代器模式帮助我们遍历集合中的元素&#…

pikachu靶场搭建教程

需要的东西 phpStudy&#xff1a; 链接&#xff1a; https://pan.baidu.com/s/1fJ-5TNtdDZGUf5FhTm245g 提取码&#xff1a;0278 pikachu-master&#xff1a; Github链接&#xff1a;https://github.com/zhuifengshaonianhanlu/pikachu 链接&#xff1a; https://pan.baidu.c…

Java实现斗地主-做牌以及对牌排序

卡牌类 public class Card {private String size;//大小private String color;//花色private int value;//权值public Card() {}public Card(String size, String color, int value) {this.size size;this.color color;this.value value;}public String toString(){return …

vue2 和 vue3 中 computer 计算属性的用法

Vue 2 中的 computed 在 Vue 2 中&#xff0c;计算属性是响应式的&#xff0c;并且基于 getter 进行缓存&#xff0c;只有依赖的响应式数据发生变化时才会重新计算。 基本用法 <template><div><p>原始消息&#xff1a;{{ message }}</p><p>反…

22.回溯算法4

递增子序列 这里不能排序&#xff0c;因为数组的顺序是对结果有影响的&#xff0c;所以只能通过used数组来去重 class Solution { public:vector<int> path;vector<vector<int>> res;void backtracking(vector<int>& nums,int start){if(path.si…

“国补”带火手机换新,出售旧手机应如何保护个人信息安全

在“国补”政策的推动下,手机换新热潮正席卷而来。“国补”以其诱人的补贴力度,成功激发了消费者更换手机的热情。无论是渴望体验最新技术的科技爱好者,还是对旧手机性能不满的普通用户,都纷纷投身到这场手机换新的浪潮之中。 随着大量消费者参与手机换新,二手手机市场迎来…