% 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