Check surrounding elements without zero

This is the next part below:

2) Additional question:

Having received the average value of nonzero neighbors, I also want to check whether the neighboring elements are equal, smaller or larger than the average value of nonzero values. If it is greater than or equal to, then "1" or "0".

Note: if the neighbors are within a radius of two or more centers, take the lowest average center value for testing.

    0    12     9     
    4  **9**    15     
   11    19     0 

"9" in the middle is within a radius of 12, 15 and 19 centers, so take the minimum average value min [9.000, 9.000, 8.000] = 8.000

For example, when the radius = 1 m or 1 element.

new_x =

     0         0          0          0         0
     0         0     **9.0000**    9.0000      0
     0      4.0000     9.0000    **9.0000**    0
     0    **8.3333** **8.0000**      0         0
     0      2.0000     4.0000      8.0000      0
     0      4.0000     5.0000      8.0000      0
     0         0          0          0         0

Test_x =

     0         0           0           0         0
     0         0       **9.0000**      1         0
     0         0           1       **9.0000**    0
     0    **8.3333**   **8.0000**      0         0
     0         0           0           0         0
     0         0           0           0         0
     0         0           0           0         0

==================================================== =================================

1) Let's say if I have the matrix shown below,

X =

 0     0     0     0     0
 0     0    12     9     0
 0     4     9    15     0
 0    11    19     0     0
 0     2     4     8     0
 0     4     5     8     0
 0     0     0     0     0

, 10. , < 10.

, , ,

new_x =

     0         0         0         0         0
     0         0    9.0000    9.0000         0
     0    4.0000    9.0000    9.0000         0
     0    8.3333    8.0000         0         0
     0    2.0000    4.0000    8.0000         0
     0    4.0000    5.0000    8.0000         0
     0         0         0         0         0

: , , , - (.. 10 ).

, , 10, "", avearge , , 1 m. 1 = 1 .

. 1 , .. 2 . , , .

**** . , = 2 , . **

,

= 1 = 1 , new_x = [(i + 1, j), (i-1, j), (i, j + 1) (i, j-1)] - , , .

= 2 = 2 , new_x = [(i + 1, j), (i + 2, j), (i-1, j), (i-2, j), (i, j + 1), (i, j + 2), (i, j-1), (i, j-2), (i + 1, j + 1), (i + 1, j-1), (i-1, j-1) (I-1, J + 1)].

=============================================== ===================

, .

, , .

.

+5
4

, , , . :

  • 10, .
  • 10, 4- .

( , ), NLFILTER ( ) :

fcn = @(x) [x(5) sum(x(2:2:8))/max(sum(x(2:2:8) > 0),1)]*[x(5) < 10; x(5) >= 10];
new_x = nlfilter(X,[3 3],fcn);


EDIT: Image Processing Toolbox, , -in CONV2, :

kernel = [0 1 0; ...                      %# Convolution kernel
          1 0 1; ...
          0 1 0];
sumX = conv2(X,kernel,'same');            %# Compute the sum of neighbors
                                          %#   for each pixel
nX = conv2(double(X > 0),kernel,'same');  %# Compute the number of non-zero
                                          %#   neighbors for each pixel
index = (X >= 10);                        %# Find logical index of pixels >= 10
new_x = X;                                %# Initialize new_x
new_x(index) = sumX(index)./max(nX(index),1);  %# Replace the pixels in index
                                               %#   with the average of their
                                               %#   non-zero neighbors

= 1 . = 2, ​​ :

kernel = [0 0 1 0 0; ...
          0 1 1 1 0; ...
          1 1 0 1 1; ...
          0 1 1 1 0; ...
          0 0 1 0 0];
+3

: , Image Processing Toolbox, : COLFILT STREL

r = 1;                       %# radius
t = 10;                      %# threshold value
mid = round((2*r+1)^2/2);    %# mid point
nhood = getnhood(strel('diamond', r));
nhood(mid) = false;
fcn = @(M)sum(M(nhood(:),:),1)./(sum(M(nhood(:),:)~=0)+all(M(nhood(:),:)==0)).*(M(mid,:)>=t)+M(mid,:).*(M(mid,:)<t);
new_x = colfilt(x, 2*[r r]+1, 'sliding',fcn)

r = 1:

new_x =
            0            0            0            0            0
            0            0            9            9            0
            0            4            9            9            0
            0       8.3333            8            0            0
            0            2            4            8            0
            0            4            5            8            0
            0            0            0            0            0

r = 2:

new_x =
            0            0            0            0            0
            0            0         11.2            9            0
            0            4            9       10.167            0
            0            7       7.7778            0            0
            0            2            4            8            0
            0            4            5            8            0
            0            0            0            0            0

, radius >= 1

, :

nhood =
     0     1     0
     1     0     1
     0     1     0

nhood =
     0     0     1     0     0
     0     1     1     1     0
     1     1     0     1     1
     0     1     1     1     0
     0     0     1     0     0

and so on..

:

COLFILT, NxN .

() fcn, (COLFILT IM2COL COL2IM .)

:

  • 10, : M(mid,:)

  • >= 10, sum(M(nhood(:),:),1) ./ (sum(M(nhood(:),:)~=0) + all(M(nhood(:),:)==0)). ,

, 1 2 R1.*(M(mid,:)<t) + R2.*(M(mid,:)>=t) if/else.

+4

- : ( Octave, matlab)

octave-3.2.3:17> toohigh = (x>=10)
toohigh =

   0   0   0   0   0
   0   0   1   0   0
   0   0   0   1   0
   0   1   1   0   0
   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0

octave-3.2.3:18> nbr_avg = filter2(ones(3,3)/9,x)
nbr_avg =

   0.00000   1.33333   2.33333   2.33333   1.00000
   0.44444   2.77778   5.44444   5.00000   2.66667
   1.66667   6.11111   8.77778   7.11111   2.66667
   1.88889   5.44444   8.00000   6.11111   2.55556
   1.88889   5.00000   6.77778   4.88889   1.77778
   0.66667   1.66667   3.44444   2.77778   1.77778
   0.44444   1.00000   1.88889   1.44444   0.88889

octave-3.2.3:19> y=x; y(toohigh) = nbr_avg(toohigh)

y =

   0.00000   0.00000   0.00000   0.00000   0.00000
   0.00000   0.00000   5.44444   9.00000   0.00000
   0.00000   4.00000   9.00000   7.11111   0.00000
   0.00000   5.44444   8.00000   0.00000   0.00000
   0.00000   2.00000   4.00000   8.00000   0.00000
   0.00000   4.00000   5.00000   8.00000   0.00000
   0.00000   0.00000   0.00000   0.00000   0.00000

filter2 ( , ...), (toohigh ), , , .

, filter2 . .


note: . , ( , ), , filter2(ones(3,3),x) ./ M, M = filter2(ones(3,3),(x ~= 0)) - .

+1

. , . conv2nan.m, NaN.

/: , , , . , . :

% set up starting point matrix with some zeros
X = magic(4);
X(X < 5) = 0;

X(X == 0) = NaN; % convert zeros to NaNs to work with conv2nan
countmat = double(X > 0);

cmat = [0 1 0;
        1 0 1;
        0 1 0]; % consider surrounding elements only

[m1,c] = conv2nan(X,cmat); % sum of surrounding elements
[m2,c] = conv2nan(countmat,cmat); % number of surrounding elements > 0

x_new = m1./m2; % compute average we want

x_new = x_new(2:end-1,2:end-1); % trim edges created by conv2
x_new(~countmat) = 0; % restore zero elements
x_new(X < 10) = X(X < 10) % restore < 10 elements

, , , >= 10. , .

+1

All Articles