% Demo to mask an array with a circle.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
A=rand(31,51);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(A);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(A, []);
% Change imshow to image() if you don't have the Image Processing Toolbox.
title('Original Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Image Analysis Demo','numbertitle','off')
% Initialize parameters for the circle,
% such as it's location and radius.
circleCenterX = 26; % Column
circleCenterY = 15; % row
circleRadius = 5; % big circle radius
% Initialize an image to a logical image of the circle.
circleImage = false(rows, columns);
[x, y] = meshgrid(1:columns, 1:rows);
circleImage((x - circleCenterX).^2 + (y - circleCenterY).^2 <= circleRadius.^2) = true;
% Display it in the upper right plot.
subplot(2,2,2);
imshow(circleImage, []);
% Change imshow to image() if you don't have the Image Processing Toolbox.
title('Circle Mask', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
drawnow;
% Mask the image with the circle.
maskedImage = A; % Initialize with the entire image.
maskedImage(~circleImage) = 0; % Zero image outside the circle mask.
% Display it in the lower right plot.
subplot(2, 3, 5);
imshow(maskedImage, []);
% Change imshow to image() if you don't have the Image Processing Toolbox.
title('Image masked with the circle.', 'FontSize', fontSize);