Calculate the neighbourhood inside or outside of a region

907 views Asked by At

I have a problem in MATLAB as follows:

Suppose I have a matrix like given below. What I want to do is calculate the average values of the pixels given in yellow. (ans is 108) enter image description here

This will be calculated if the option given is outside.

On the other hand if the option is given as inside, then an operation as performed in the image. enter image description here

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

****4 neighbourhood** or 8 neighbourhood ** may be considered. I want to compute this in MATLAB. Can you guys help me ?

2

There are 2 answers

0
bla On

Another solution:

use either:

BW = ~(FirstImage>0);

or:

BW = SecondImage>0;

then:

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

% Firstly, Create the 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 in the image are 1.
% This is to make it compatible with bwtraceboundary

im = FirstImage == 0 ;

% Find the object coordinates to match the requirements of bwtraceboundary

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

% The find() function scans the matrix column-by-column
% so we know that it will start in the top left corner and work
% itself down, column-by-column. Therefore, some part of the boundary must
% lie east of the first coordinate found. This is one way of finding
% the starting coordinates

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

% Mark the contour

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


% Now, this will only detect the boundary of the object itself.
% We are looking for the nearest non-zero values to the boundary.
% To get the nearest non-zero values,
% apply dilution to the countour image and
% then multiply it element-wise with the original image.
% Calculate the mean of the non-zero values


% Perform dilution

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



% Multiply the mask with the original image, element-wise.
% The object itself will then be zeroed out and the non-zero boundary will remain.

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



% Then calculate the average

mean(A(A>0))

ans =

108.6875