Can anyone please explain the meaning of below code?

1 view (last 30 days)
[rows, columns] = size(grayImage);
circleCenterX = 120;
circleCenterY = 120; % square area 0f 500*500
circleRadius = 110; % big circle radius
circleImage = false(rows, columns);
[x, y] = meshgrid(1:columns, 1:rows);
circleImage((x - circleCenterX).^2 + (y - circleCenterY).^2 <= circleRadius.^2) = true;
b = and(circleImage,b);
labeledImage = bwlabel(b);
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
matrix = zeros(4,length(measurements));
vector = zeros(1,length(measurements));
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
matrix(:,k) = thisBB(:);
vector(k) = thisBB(2);
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','g','LineWidth',2 )
end
vector = sort(vector);

Accepted Answer

Walter Roberson
Walter Roberson on 10 Sep 2017
The code creates a binary mask which is the inside of a circle of radius 110, centered at (120, 120). It then "ands" that mask with b, which has the effect of erasing everything in b that is outside the circle.
Components of the result are then labeled, and bounding boxes and area are found. The bounding boxes and area are recorded in the arrays "matrix" and "vector", and rectangles are plotted corresponding to each bounding box.
Finally, the areas are sorted -- but the original order is not kept track of, so it is not possible to associate the sorted areas with the corresponding bounding box. That final line
vector = sort(vector);
should be replaced by
[vector, sortorder] = sort(vector);
matrix = matrix(sortorder, :);
After that, the order of the bounding boxes would correspond to increasing area.

More Answers (0)

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!