I = imread('sample_circles.bmp');
Igray = rgb2gray(I);
BW = imbinarize(Igray);
BW = imclearborder(BW);
se = strel('disk',1);
BW = imclose(BW,se);
s = regionprops('table',BW,{'Centroid','BoundingBox','PixelIdxList'});
avLuminance = zeros(height(s),1);
for kk = 1:height(s)
avLuminance(kk) = mean(Igray(s.PixelIdxList{kk}));
end
figure
imshow(Igray)
hold on
for kk = 1:height(s)
rectangle('Position', s.BoundingBox(kk,:),'EdgeColor', 'c');
text(s.Centroid(kk,1),s.Centroid(kk,2),...
sprintf('%.1f',avLuminance(kk)),...
'FontWeight', 'bold',...
'HorizontalAlignment','center',...
'Color', 'c')
end