matlab - 在 matlab 中更改过滤器(B,A,X)并出现内存不足错误

标签 matlab image-processing filter signal-processing

这篇文章与我之前的问题相关:image processing in matlab 因为我已经在那里上传了我的算法。 我的想法是我正在尝试更改代码的过滤部分。 在 matlab filter.m 函数中可以接受过滤器(B,A,我的像素随时间的演变)并返回过滤后的值。 但目前我必须将整个时间序列一起传递。

但问题是,现在我想以某种方式更改代码,而不是将整个时间序列传递到过滤器中,我想一次传递一个值,但我希望过滤器函数将该值视为第 n 个值不是第一个值。 我创建了一个 sudo 代码,因为我将一张图片注入(inject)到代码中,但我不知道如何更改过滤部分。任何人都有任何想法吗?

clear all
j=1;
for i=0:3000
  str = num2str(i);
  str1 = strcat(str,'.mat');
  load(str1);
  D{j}=A(20:200,130:230);
  j=j+1;
end
N=5;
w = [0.00000002 0.05;0.05 0.1;0.1 0.15;0.15 0.20;
     0.20 0.25;0.25 0.30;0.30 0.35;0.35 0.40;
     0.40 0.45;0.45 0.50;0.50 0.55;0.55 0.60;
     0.60 0.65;0.65 0.70;0.70 0.75;0.75 0.80;
     0.80 0.85;0.85 0.90;0.90 0.95;0.95 0.99999999];
for ind=1:20
  wn = w(ind,:);
  [b,a] = butter(N,wn);
  bCoeff(ind,:)=b;
  aCoeff(ind,:)=a;
end

ts=[];
sumLastImages=[];
for k=1:10 %number of images
  for bands=1:20 %number of bands
    for i=1:10 %image h
      for j=1:10 %image w
        pixelValue = D{k}(i,j);          
        % reflectivity elimination        
        % for the current pixel, have the summation of the same position from before 
        % images and create a mean value base on the temporal values
        sumLastImages(i,j)=pixelValue+sumLastImages(i,j);
        meanValue = sumLastImages(i,j)/k;

        if(meanValue==0)            
          filteredimages{bands}(i,j)=0; 
          continue;
        else
          pixel_calculated_meanvalue = pixelValue/meanValue; 
        end                      
        % filter part that need to be changed, and because we are adding 
        % one value then the reutrn of the filter is one too         
        ts_f = filter(bCoeff(bands,:), aCoeff(bands,:), ...
                      pixel_calculated_meanvalue);                         
        filteredimages{bands}(i,j)=ts_f;            
      end     
    end       

    finalImagesSummation{bands}(:,:) = ...
      (filteredimages{bands}(:,:)^2)+finalImagesSummation{bands}(:,:);
    finalImages{bands}(:,:)=finalImagesSummation/k;
  end
end

编辑 我设法像这样更改代码,现在我加载了前 200 张图像,之后我能够将图像一张一张地注入(inject)到过滤器中,但现在的问题是我得到了 "Out of内存。键入 HELP MEMORY 作为您的选项。" 错误为大 图片。 这是我的代码任何提高代码效率的想法:

%%
cd('D:\MatlabTest\06-06-Lentils');
clear all

%%

N=5;
W = [0.0 0.10;0.10 0.20;0.20 0.30;0.30 0.40;
     0.40 0.50;0.50 0.60 ;0.60 0.70;0.70 0.80 ;
     0.80 0.90;0.90 1.0];

[bCoeff{1},aCoeff{1}] = butter(N,0.1,'Low');
for ind=2:9
  wn = W(ind,:);
  [b,a] = butter(N,wn);
  bCoeff{ind}=b;
  aCoeff{ind}=a;
end
[bCoeff{10},aCoeff{10}] = butter(N,0.9,'high');

%%
j=1;

D = zeros(200,380,320);

T = 200;
K = 0:399;
l = T+1;
Yout = cell(1,10);
figure;

for i = 1:length(K)-200
  disp(i)

  if i == 1
    for j = 1:T
      str = int2str(K(1)+j-1);
      str1 = strcat(str,'.mat');
      load(str1);
      D(j,1:380,1:320) = A;
    end

  else

    str = int2str(l);
    str1 = strcat(str,'.mat');
    load(str1);

    temp = D(2:200,1:380,1:320) ;
    temp(200,1:380,1:320) = A;
    D = temp;
    clear temp
    l = l +1;

  end


  for p = 1:380
    for q = 1:320
      x = D(:,p,q) - mean(D(:,p,q));

      for k = 1:10
        temp = filter(bCoeff{k},aCoeff{k},x);
        if i == 1
          Yout{k}(p,q) = mean(temp);
        else
          Yout{k}(p,q) = (Yout{k}(p,q)  + mean(temp))/2;
        end
      end
    end
  end

  for k = 1:10
    subplot(5,2,k)
    subimage(Yout{k}*1000,[0 255]);
    color bar
    colormap jet
  end
  pause(0.01);
end

disp('Done Loading...')

最佳答案

无需重写过滤函数,有一个简单的解决方案!

如果你想喂filter一次处理一个样本,您还需要传递状态参数,以便根据其前一个样本处理每个输入样本。过滤后,新状态实际上作为第二个参数返回,因此 MATLAB 已经为您完成了大部分工作。 这是个好消息!

为了可读性,请允许我暂时用简单的字母替换您的变量名称:

a = aCoeff(bands, :);    
b = bCoeff(bands, :);
x = pixel_calculated_meanvalue;

ts_fy 表示。

所以,这个:

y = filter(b, a, x);

实际上相当于:

N = numel(x);
y = zeros(N, 1);                             %# Preallocate memory for output
z = zeros(max(length(a), length(b)) - 1, 1); %# This is the initial filter state
for i = 1:N
    [y(i), z] = filter(b, a, x(i), z);
end

你可以自己检查一下结果是一样的!

对于您的示例,代码为:

N = numel(pixel_calculated_meanvalue);
ts_f = zeros(N, 1);
z = zeros(max(length(aCoeff(bands, :)), length(bCoeff(bands, :))) - 1, 1); 
for i = 1:N
    [ts_f(i), z] = filter(bCoeff(bands, :), aCoeff(bands, :), ...
        pixel_calculated_meanvalue(i), z);
end

使用此方法,您可以一次处理一个输入样本,只需确保在每次 filter 调用后存储最后的过滤器状态即可。如果您计划使用多个过滤器,则必须为每个过滤器存储一个状态向量!

关于matlab - 在 matlab 中更改过滤器(B,A,X)并出现内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583523/

相关文章:

python - 如何识别不同颜色的条纹

java - 有没有办法过滤/搜索 HTMLEditorKit 中的内容?

用于读取写入数组的 csv 的 C++ 程序;然后操作并打印到文本文件中(已经用 matlab 编写)

matlab - 正态分布的峰度

image-processing - 用激光射击毛毛虫的神经网络和图像处理

python - 获取图像中的所有 R-G-B

api - 过滤用户轨道时的 Soundcloud API 错误

python - 如何只保留 pandas DataFrame 中具有多个值的行?

Matlab:查找矩阵每一列首次出现的行索引(不使用循环)

oop - 如何获取 MATLAB 句柄对象的 ID?