Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Made by Gábor Vecsei
- % https://gaborvecsei.wordpress.com/
- % https://twitter.com/GAwesomeBE
- clc;
- close all;
- clear;
- workspace;
- [filename, pathname] = ...
- uigetfile({'*.jpg';'*.png';'*.tif';'*.*'},'Select an Image file');
- fullFileName = fullfile(pathname, filename);
- I = imread(fullFileName);
- BW = im2bw(I);
- BW = ~BW;
- st = regionprops(BW, 'BoundingBox', 'Area', 'Centroid',...
- 'MajorAxisLength','MinorAxisLength');
- [labeledImage, numberOfCircles] = bwlabel(BW);
- subplot(1,2,1);
- imshow(BW);
- title('Original Picture', 'Color', 'black', 'FontSize', 14);
- for k = 1 : length(st)
- thisBB = st(k).BoundingBox;
- rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
- 'EdgeColor','r','LineWidth',1 );
- end
- for k = 1 : length(st)
- center = st(k).Centroid;
- diameter = mean([st(k).MajorAxisLength st(k).MinorAxisLength],2);
- radii = diameter/2;
- hold on
- plot(center(1),center(2),'r*','MarkerSize',5)
- hold off
- text(center(1),center(2),strcat('radian:',num2str(radii)),'Color','green','FontSize', 8);
- end
- allAreas = [st.Area];
- [sortedAreas, sortingIndexes] = sort(allAreas, 'ascend');
- smallIndex = sortingIndexes(1);
- smallImage = ismember(labeledImage, smallIndex);
- st_small = regionprops(smallImage, 'BoundingBox', 'Area');
- subplot(1,2,2);
- imshow(smallImage);
- title('The Smallest Object', 'Color', 'black', 'FontSize', 14);
- for k = 1 : length(st_small)
- thisBB = st_small(k).BoundingBox;
- rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
- 'EdgeColor','r','LineWidth',1 );
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement