0
votes

My data matrix is large: smt like 180:3000 size. Each element value is between 0 to 255;

I have to find areas in this matrix where average value is higher than some threshold (lets call it 'P'). And reset each element in these areas to '0'. Another words filter my matrix.

I have width and heigth of filter area. So I need to loop over data matrix to find appropriate areas (As many as exist).

EDIT:

Please, see an example:

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

This is given matrix. Lets try to find areas (2, 3) of size where average value is > 15.

So the result will be:

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

Please, look at bottom of matrix

Please, give me some tips how it is possible to loop throw.

Thank you very much.

2
Some example code would be good. - Marcin
additionally put a small example, input and output matrices alongwith with your filter area - Nishant
Sure. Example is atop. - Artem
take a look at conv2 here mathworks.in/help/matlab/ref/conv2.html - Nishant

2 Answers

1
votes

One way of doint this is as follows:

% 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

Results in:

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
0
votes

try this

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