Advertisement
makispaiktis

2. Image - Circular objects

Apr 19th, 2022 (edited)
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.68 KB | None | 0 0
  1. clc
  2. clear all
  3.  
  4. % ******** Data ********
  5. rgb = imread('circularobjects.png');
  6. gray = rgb2gray(rgb);
  7. sens1 = 0.95;
  8. sens2 = 0.97;
  9. polarity1 = 'dark';                 % Red circles
  10. polarity2 = 'bright';               % Blue circles
  11. y1 = simulate(rgb, gray, polarity1, sens1);
  12. y2 = simulate(rgb, gray, polarity1, sens2);
  13. y3 = simulate(rgb, gray, polarity2, sens1);
  14. y4 = simulate(rgb, gray, polarity2, sens2);
  15.  
  16.  
  17.  
  18. function sensitivity = simulate(rgb, gray, pol, sens)
  19.    
  20.     display('*************************************');
  21.     if strcmp(pol, 'dark')
  22.         color = 'r';
  23.     elseif strcmp(pol, 'bright')
  24.         color = 'b';
  25.     end
  26.     polarity = pol
  27.     sensitivity = sens
  28.     % ******** Find circles ********
  29.     % 1. 'imfindcircles' finds circular objects that are brighter than the background.
  30.     % So, set the parameter 'ObjectPolarity' to 'dark' in imfindcircles to search for dark circles.
  31.     % 2. imfindcircles has a parameter 'Sensitivity' which can be used to control this internal threshold,
  32.     % and consequently, the sensitivity of the algorithm. A higher 'Sensitivity' value sets the detection
  33.     % threshold lower and leads to detecting more circles.
  34.     [centers1,radii1] = imfindcircles(rgb,[20 25],'ObjectPolarity', polarity, ...
  35.         'Sensitivity', sens)
  36.     [centers2,radii2] = imfindcircles(gray,[20 25],'ObjectPolarity', polarity, ...
  37.         'Sensitivity', sens)
  38.  
  39.  
  40.     % ******** Visualize circles ********
  41.     figure;
  42.     imshow(rgb);
  43.     h1 = viscircles(centers1, radii1, 'Color', color);
  44.     figure;
  45.     imshow(gray);
  46.     h2 = viscircles(centers2, radii2, 'Color', color);
  47.     disp("Found " + num2str(length(centers1)) + " circles.");
  48.    
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement