Calculate neighborhoods inside or outside the area

I have a problem in MATLAB as follows:

Suppose I have a matrix like below. I want to calculate the average pixel values ​​given in yellow. (ans - 108) enter image description here

This will be calculated if the specified option is out .

On the other hand, if the parameter is specified as inside , then the operation performed on the image. enter image description here

** I want to write an algorithm to find out the average value inside or outside the region with zeros depending on the option set manually. **

**** 4 surroundings ** or 8 surroundings **. I want to calculate this in MATLAB. Can you guys help me?

+4
source share
2 answers

% First, create images

FirstImage = [
108 113 121 129 128 124 117 101
114 76  60  110 98  74  121 109
114 62  52  105 85  59  121 116
110 59  54  104 0   0   0   115
104 55  54  104 0   0   0   113
96  48  51  105 0   0   0   113
94  60  69  115 0   0   0   110
99  108 122 130 135 0   0   109
];

SecondImage = [
0   0   0   0   0   0
138 137 137 137 0   0
138 127 129 135 138 0
132 97  99  133 135 0
134 108 110 137 137 0
141 140 140 140 139 0
138 138 138 140 0   0
0   0   0   0   0   0
];

% Make the image binary and invert it so that the zeros on the image are 1.
% This should make it compatible withbwtraceboundary

im = FirstImage == 0 ;

% Find the coordinates of the object in accordance with the requirements bwtraceboundary

objectCoord = find(im); 
[startRow,startCol]  = ind2sub(size(im),objectCoord(1) );  

% The function find()scans the matrix in the
% column , so we know that it will start in the upper left corner and will earn
% down in the column. Therefore, some part of the boundary should
% lie east of the first coordinate found. This is one way to find
% origin

contour = bwtraceboundary(im,[startRow startCol],'E' ); 

% Mark the outline

contourimage = zeros(size(im));
contourind = sub2ind(size(contourimage),contour(:,1),contour(:,2))
contourimage(contourind) = 1;



contourimage =

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


% .

% .
% ,
% countour
% .
%


%

mask = bwmorph(contourimage,'dilate')

mask =

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



% mask , .
% , .

A=mask.*FirstImage

A =

 0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0
 0     0     0   105    85    59   121   116
 0     0     0   104     0     0     0   115
 0     0     0   104     0     0     0   113
 0     0     0   105     0     0     0   113
 0     0     0   115     0     0     0   110
 0     0     0   130   135     0     0   109



%

mean(A(A>0))

ans =

108.6875
+5

:

:

BW = ~(FirstImage>0);

BW = SecondImage>0;

[B,L] = bwboundaries(BW,'noholes');
B=cell2mat(B);
m=zeros(size(BW));
m(sub2ind(size(BW),B(:,1),B(:,2)))=1
+2

All Articles