Matlab滤波器矩阵

时间:2014-06-25 04:51:49

标签: matlab loops for-loop matrix filter

我的数据矩阵很大:smt像180:3000大小。 每个元素值介于0到255之间;

我必须在此矩阵中找到平均值高于某个阈值的区域(我们称之为'P')。并将这些区域中的每个元素重置为“0”。另一个词过滤我的矩阵。

我有过滤区域的宽度和高度。 所以我需要循环数据矩阵以找到适当的区域(尽可能多的存在)。

修改

请看一个例​​子:

4   6   7   5   6   6   7
10  8   9   8   9   10  9
10  8   9   8   9   10  9
7   4   6   9   7   8   7
4   5   5   5   5   5   5
4   5   5   5   5   5   5
10  12  12  12  13  10  11
14  15  15  16  14  15  15
13  15  15  15  14  14  13

这是给定的矩阵。让我们试着找到平均值> 1的区域(2,3)。 15。

结果将是:

4   6   7   5   6   6   7
10  8   9   8   9   10  9
10  8   9   8   9   10  9
7   4   6   9   7   8   7
4   5   5   5   5   5   5
4   5   5   5   5   5   5
10  12  12  12  13  10  11
14  0   0   0   14  15  15
13  0   0   0   14  14  13

请看矩阵的底部

请给我一些如何循环投掷的提示。

非常感谢。

2 个答案:

答案 0 :(得分:1)

这样做的一种方法如下:

% example A with more areas of mean greater than 15
% there are four such areas as shown here: http://i.imgur.com/V6m0NfL.jpg
A = [16   16   16   5   16   16   16
16  16   16   8   16   16  16
10  8   9   8   9   10  9
7   4   6   9   7   8   7
4   5  15.1   15   15   5   5
4   5   15   15   15   5   5
10  12  12  12  13  10  11
14  15  15  16  14  15  15
13  15  15  15  14  14  13];



% filter size
[n,m] = deal(2,3);

% filter center
center = floor(([n,m]+1)/2);

% find where we have areas greater than 15
B = nlfilter(A, [n,m], @(b) mean(b(:)) > 15);


% get coordinates of areas with mean > 15
[rows,cols] = find(B);


% zero out elements in all found areas
for i = 1:size(rows,1)

    % calculate starting coordinates for the area to be set to 0
    row = rows(i) - center(1) + 1;
    col = cols(i) - center(2) + 1;

    A(row:row+n-1 , col:col+m-1) = 0;
end

结果:

A =

     0     0     0     5     0     0     0
     0     0     0     8     0     0     0
    10     8     9     8     9    10     9
     7     4     6     9     7     8     7
     4     5     0     0     0     5     5
     4     5     0     0     0     5     5
    10    12    12    12    13    10    11
    14     0     0     0    14    15    15
    13     0     0     0    14    14    13

答案 1 :(得分:0)

试试这个

a = input_matrix;
ii = 2 ; jj = 3;
threshold = 15;
x = ones(ii,jj)/(ii*jj);
%\\create matrix temp2 with average value of block a(i:i+ii-1,j:j+jj-1) at temp2(i,j)
temp1 = conv2(a,x,'full'); 
temp2 = temp1(ii:end-ii+1,jj:end-jj+1);
%\\find row and column indices of temp2 with value > threshold
[row_ col_] = find(temp2>threshold);
out = a;
%\\assign zero value to the corresponding blocks
for iii = 1:length(row_)
    out(row_(iii):row_(iii)+ii-1,col_(iii):col_(iii)+jj-1) = 0;
end