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)

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.

**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 ?
% Firstly, Create the images
% Make the image binary and invert it so that the zeros in the image are 1.
% This is to make it compatible with
bwtraceboundary% Find the object coordinates to match the requirements of
bwtraceboundary% 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
% Mark the contour
% 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
% Multiply the
maskwith the original image, element-wise.% The object itself will then be zeroed out and the non-zero boundary will remain.
% Then calculate the average