Advertisement
Guest User

Untitled

a guest
Feb 27th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.58 KB | None | 0 0
  1. %   Made by Gábor Vecsei
  2. %   https://gaborvecsei.wordpress.com/
  3. %   https://twitter.com/GAwesomeBE
  4.  
  5. clc;
  6. close all;
  7. clear;
  8. workspace;
  9. [filename, pathname] = ...
  10.      uigetfile({'*.jpg';'*.png';'*.tif';'*.*'},'Select an Image file');
  11. fullFileName = fullfile(pathname, filename);
  12. I = imread(fullFileName);
  13. BW = im2bw(I);
  14. BW = ~BW;
  15. st = regionprops(BW, 'BoundingBox', 'Area', 'Centroid',...
  16.     'MajorAxisLength','MinorAxisLength');
  17.  
  18.  
  19.  
  20.  
  21. [labeledImage, numberOfCircles] = bwlabel(BW);
  22.  
  23. subplot(1,2,1);
  24. imshow(BW);
  25. title('Original Picture', 'Color', 'black', 'FontSize', 14);
  26.  
  27. for k = 1 : length(st)
  28.   thisBB = st(k).BoundingBox;
  29.   rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
  30.       'EdgeColor','r','LineWidth',1 );
  31. end
  32.  
  33. for k = 1 : length(st)
  34.     center = st(k).Centroid;
  35.     diameter = mean([st(k).MajorAxisLength st(k).MinorAxisLength],2);
  36.     radii = diameter/2;
  37.     hold on
  38.     plot(center(1),center(2),'r*','MarkerSize',5)
  39.     hold off
  40.     text(center(1),center(2),strcat('radian:',num2str(radii)),'Color','green','FontSize', 8);
  41. end
  42.  
  43. allAreas = [st.Area];
  44. [sortedAreas, sortingIndexes] = sort(allAreas, 'ascend');
  45. smallIndex = sortingIndexes(1);
  46. smallImage = ismember(labeledImage, smallIndex);
  47. st_small = regionprops(smallImage, 'BoundingBox', 'Area');
  48.  
  49. subplot(1,2,2);
  50. imshow(smallImage);
  51. title('The Smallest Object', 'Color', 'black', 'FontSize', 14);
  52.  
  53. for k = 1 : length(st_small)
  54.   thisBB = st_small(k).BoundingBox;
  55.   rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
  56.   'EdgeColor','r','LineWidth',1 );
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement