clc;
close all;
imtool close all;
clear;
workspace;
fontSize = 14;
numberOfSmallCircles = 50;
smallCircleOutsideValue = 0.3;
smallCircleInsideValue = 0.8;
smallCircleRadius = 31;
bigImageWidth = 500;
bigImageHeight = 500;
bigCircleRadius = 250;
bigCircleImage = zeros(bigImageHeight, bigImageWidth);
[x, y] = meshgrid(1:bigImageWidth, 1:bigImageHeight);
bigCircleImage((x - bigImageWidth/2).^2 + (y - bigImageHeight/2).^2 <= bigCircleRadius.^2) = 1;
subplot(3,2,1);
imshow(bigCircleImage, []);
title('Big Circle Mask', 'FontSize', fontSize);
axis on;
set(gcf, 'Position', get(0,'Screensize'));
sigma = smallCircleRadius * .4;
singleCircleImage = fspecial('gaussian', 2*smallCircleRadius, sigma);
singleCircleImage=singleCircleImage/max(max(singleCircleImage));
subplot(3,2,2);
imshow(singleCircleImage, []);
title('Single Small Circle (scaled to fit)', 'FontSize', fontSize);
axis on;
singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
widthThatWillFit = bigImageWidth - 2 * smallCircleRadius;
heightThatWillFit = bigImageHeight - 2 * smallCircleRadius;
smallUpperLeftX = widthThatWillFit * rand(numberOfSmallCircles, 1);
smallUpperLeftY = heightThatWillFit * rand(numberOfSmallCircles, 1);
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
for k = 1 : numberOfSmallCircles
x1 = int16(smallUpperLeftX(k));
y1 = int16(smallUpperLeftY(k));
if x1 < 1
x1 = 1;
end
if y1 < 1
y1 = 1;
end
x2 = int16(x1 + singleWidth - 1);
y2 = int16(y1 + singleHeight - 1);
if x2 > bigImageWidth
x2 = bigImageWidth;
end
if y2 > bigImageHeight
y2 = bigImageHeight;
end
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
end
manySmallCircles(manySmallCircles == 0) = smallCircleOutsideValue;
subplot(3,2,3);
imshow(manySmallCircles, []);
title('Many Small Overlapping Circles', 'FontSize', fontSize);
axis on;
maskedByBigCircle = bigCircleImage .* manySmallCircles;
subplot(3,2,4);
imshow(maskedByBigCircle, []);
title('Many Small Circles Masked by Big Circle', 'FontSize', fontSize);
axis on;
subplot(3,2,5:6);
numberOfBins = 128;
[pixelCounts grayLevels] = hist(maskedByBigCircle(:), numberOfBins);
bar(pixelCounts);
xlim([1 numberOfBins]);
title('Intensity Histogram of Many Small Circles Masked by Big Circle', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlabel('Bin', 'FontSize', fontSize);
Best Answer