Mathematica中文论坛-非官方

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 8284|回复: 0
打印 上一主题 下一主题

豆粑粑 matlab find circle hist

[复制链接]

532

主题

602

帖子

3031

积分

论坛元老

Rank: 8Rank: 8

积分
3031
跳转到指定楼层
楼主
发表于 2017-7-10 19:56:33 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 meatball1982 于 2017-7-10 20:00 编辑

XP的媳妇儿的问题,想想挺好玩的。试试。
有一个什么图,里面是一堆堆的圆。
原来的思路是手动找圆,然后测量半径。然后手动统计分布。注意,是手动,手动。我的天啊!
matlab里有一个imfindcircles函数,老好老好了。
把图中的圆找出来,这之前,要把圆的边界收缩一下,更加容易实现找到圆的过程。
  1. clear all
  2. clc
  3. clf

  4. %% outline
  5. % find circle 4 Xiaopeng
  6. % it costs at least 500 RMB

  7. %% main %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  8. % read image -------------------------------------
  9. % ima=imread('../file_imgs/img_cir_orig.jpg');
  10. ima=imread('../file_imgs/p1-1.png');
  11. se = strel('ball',10,3);            
  12. eroded = imerode(ima,se);            % splot the circles
  13. im=rgb2gray(eroded);                 % change to gray
  14. im(im<=82)=0;
  15. im(im>82)=500;                       % 2 values


  16. [cen,rad,met]=imfindcircles(im,[9 14],'ObjectPolarity','bright',...
  17.     'Sensitivity',0.973,'EdgeThreshold',0.02); % find circles

  18. [hi,bi]=hist(rad,40);                          % hist radius
  19. hi = hi./sum(hi);                              % percents

  20. save Mat_tm.mat


  21. %% fit distribution %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22. load Mat_tm.mat
  23. % fit gaussian      -----------------------
  24. x_bin=bi;
  25. x_plot= linspace(8,16,200);
  26. x1=linspace(min(x_bin),max(x_bin),200);
  27. y1 = hi;

  28. fx=@(b,x)1/(b(1)*sqrt(2*pi*b(2))).*exp(-(x-b(3)).^2/(2*b(2)^2));
  29. b0=[2,11,0.5];
  30. b=b0;
  31. for l=1:20
  32.     % Solve nonlinear curve-fitting problems in least-squares sense.
  33.     % just a guess.
  34.     b=lsqcurvefit(fx,b,x_bin,y1);
  35.     % Nonlinear regression,
  36.     b=nlinfit(x_bin,y1,fx,b);
  37.    
  38. end
  39. b1=b;           
  40. y_hi1 = fx(b1,x_plot);          % for plot gaussian distribution

  41. % fit log gaussian --------------------------
  42. fx_log =@(b,x) 1./x ./(b(1)*sqrt(2*pi*b(2))) .* exp(-(log(x)-b(3)).^2/(2*b(2)^2));
  43. b0_log0 = [ 2 11 0.5  ];

  44. b=b0;
  45. for l=1:20
  46.     % Solve nonlinear curve-fitting problems in least-squares sense.
  47.     % just a guess.
  48.     b=lsqcurvefit(fx_log,b,x_bin,y1);
  49.     % Nonlinear regression,
  50.     b=nlinfit(x_bin,y1,fx_log,b);
  51. end
  52. b1_log=b;

  53. y_hi1_log = fx_log(b1_log,x_plot); % for plot gaussian log distribution

  54. save Mat_fit.mat


  55. %% plot results %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  56. load Mat_fit.mat
  57. sbp_width=0.85;
  58. sbp_heig=0.8;
  59. n_row = 2;
  60. n_col = 3;
  61. [out_pos]=fun_mm_subplot_pos(n_row,n_col,sbp_width,sbp_heig);
  62. h=figure(1)
  63. set(h, 'Position', [100, 100, 1000, 550]);
  64. % subplot(2,3,1)
  65. ax=axes('position',out_pos(1,:));
  66. imshow(ima)
  67. ax=axes('position',out_pos(2,:));
  68. imshow(eroded)
  69. ax=axes('position',out_pos(3,:));
  70. imshow(im)
  71. hBright = viscircles(cen, rad,'EdgeColor','r','linewidth',0.5);
  72. ax=axes('position',out_pos(4,:));
  73. imshow(ima)
  74. hBright = viscircles(cen, rad+10,'EdgeColor','r','linewidth',0.5);
  75. ax=axes('position',out_pos(5,:));
  76. plot(rad,'ko','markerfacecolor','k','markersize',3)
  77. axis([ 0 length(rad) 9 14])
  78. ax=axes('position',out_pos(6,:));
  79. barh(bi,hi,'c','edgecolor','none')
  80. hold on
  81. plot(y_hi1,x_plot,'b-','linewidth',2)
  82. plot(y_hi1_log,x_plot,'m-','linewidth',2)
  83. legend('hist','gau','log')
  84. axis([0 1.1*max(hi) 9 14])
复制代码




res4subplot.png (370.49 KB, 下载次数: 796)

res4subplot.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|Mathematica中文论坛-非官方 ( 辽ICP备16001491号-1

GMT+8, 2024-4-28 00:24 , Processed in 0.119892 second(s), 26 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表