小言_互联网的博客

【图像分割】基于分水岭算法实现细胞分割计数matlab源码

306人阅读  评论(0)

一、简介

分水岭算法是一种图像区域分割法,分割的过程中将图片转化为灰度图,然后我会将灰度值看作是海拔,然后向较低点注水,这种基于地形学的解释,我们着重考虑三种点:

极小值点,该点对应一个盆地的最低点,当我们在盆地里滴一滴水的时候,由于重力作用,水最终会汇聚到该点。注意:可能存在一个最小值面,该平面内的都是极小值点。
盆地的其它位置点,该位置滴的水滴会汇聚到局部最小点。
盆地的边缘点,是该盆地和其它盆地交接点,在该点滴一滴水,会等概率的流向任何一个盆地。

明白上述三种点之后,我们开始往盆地的极小值点注水,然后随着注水的深入,每一个极小值点慢慢的向外扩展,然后知道两个盆地的水汇合,汇合处就是我们需要的分水岭。

从下图可以直观理解一下,首先这三块区域都含有极小值点

然后逐渐填充就能获得分水岭(即分界线)

得到分界线就能完成图像分割:

二、源代码


  
  1. function susanseg
  2. clear all; close all; clc
  3. image= imread( 'cell.jpg');
  4. % 用SUSAN算法进行边缘检测
  5. image = susan(image, 4);
  6. figure, imshow(image,[]);
  7. %imwrite(image, './susanout/susanout.jpg');
  8. % 将image转为二值图像保存后,用图像处理工具
  9. % 把其背景的所有连通区域处理为黑色,即只有细
  10. % 胞体是白色,便于细胞数目的搜索
  11. BW = im2bw(image, graythresh(image));
  12. bounder_area = length(find(BW== 0));
  13. %imwrite(BW, './susanout/bw.jpg');
  14. figure, imshow(BW);
  15. % 申明全局变量
  16. global B Dir m n;
  17. B = imread( './blackbackground.jpg');
  18. B = im2bw(B, graythresh(B));
  19. [m,n] = size(B);
  20. figure, imshow(B);
  21. % 细胞的总面积,即细胞所占的像素数目,包括细胞的边界
  22. % 由于SUSAN提取出的边界已被增宽,所以将边界像素数除以2
  23. % 来作为细胞的边界像素数目
  24. total_area = length(find(B== 1)) + bounder_area/ 2;
  25. NUM = 5; % 细胞面积阈值
  26. count = 0; % 细胞总数
  27. % 搜索方向向量,4邻域搜索
  28. Dir = [- 1 0; 0 1; 1 0; 0 - 1;];
  29. % 搜索方向向量,8邻域搜索
  30. %Dir = [-1 0; -1 1; 0 1; 1 1; 1 0; 1 -1; 0 -1; -1 -1;];
  31. for i = 1:m
  32. for j = 1:n
  33. if B(i,j)== 1 % 是细胞像素
  34. num = search(i,j, 4) + 1; % 计算该细胞的像素数目
  35. if num>NUM
  36. count = count + 1;
  37. else
  38. total_area = total_area - num; % 减掉不是细胞的面积
  39. end
  40. end
  41. end
  42. end
  43. %fid = fopen('./susanout/results.txt', 'wt');
  44. fprintf( '图像尺寸: %d * %d, SUSAN阈值: 4, 细胞面积阈值: %d\n', ...
  45. n, m, NUM);
  46. fprintf( '细胞总数: %d, 细胞总面积: %.2f, 平均细胞面积: %.2f\n', ...
  47. count, total_area, total_area/count);
  48. %fprintf(fid,'图像尺寸: %d * %d, SUSAN阈值: 4, 细胞面积阈值: %d\n', ...
  49. % n, m, NUM);
  50. %fprintf(fid,'细胞总数: %d, 细胞总面积: %.2f, 平均细胞面积: %.2f\n', ...
  51. % count, total_area, total_area/count);
  52. %fclose(fid);
  53. end
  54. % -----------------------------------------------------------------------
  55. %
  56. % This function uses the SUSAN algorithm to find edges within an image
  57. %
  58. %
  59. % >>image_out = susan(image_in,threshold)
  60. %
  61. %
  62. % Input parameters ... The gray scale image, and the threshold
  63. % image_out .. (class: double) image indicating found edges
  64. % typical threshold values may be from 10 to 30
  65. %
  66. %
  67. %The following steps are performed at each image pixel:
  68. % ( from the SUSAN webpage, http://www.fmrib.ox.ac.uk/~steve/susan/susan/node4.html )
  69. %
  70. % Place a circular mask around the pixel in question.
  71. % Calculate the number of pixels within the circular mask which have similar brightness to
  72. % the nucleus. These define the USAN.
  73. % Subtract USAN size from geometric threshold to produce edge strength image.
  74. %
  75. % Estimating moments to find the edge direction has not been implemented .
  76. % Non-maximal suppresion to remove weak edges has not been implemented yet.
  77. %
  78. % example:
  79. %
  80. % >> image_in=imread('test_pattern.tif');
  81. % >> image = susan(image_in,27);
  82. % >> imshow(image,[])
  83. %
  84. %
  85. % Abhishek Ivaturi
  86. %
  87. % -------------------------------------------------------------------------
  88. function image_out = susan(im,threshold)
  89. % check to see if the image is a color image...
  90. %im= imread('test_pattern.tif')
  91. %threshold=27;
  92. d = length(size(im));
  93. if d== 3
  94. image=double(rgb2gray(im));
  95. elseif d== 2
  96. image=double(im);
  97. end
  98. % mask for selecting the pixels within the circular region (37 pixels, as
  99. % used in the SUSAN algorithm
  100. mask = ([ 0 0 1 1 1 0 0 ; 0 1 1 1 1 1 0; 1 1 1 1 1 1 1; 1 1 1 1 1 1 1; 1 1 1 1 1 1 1; 0 1 1 1 1 1 0; 0 0 1 1 1 0 0]);
  101. % the output image indicating found edges
  102. R=zeros(size(image));
  103. % define the USAN area
  104. nmax = 3* 37/ 4;
  105. % padding the image
  106. [a b]=size(image);
  107. new=zeros(a+ 7,b+ 7);
  108. [c d]=size(new);
  109. new( 4:c- 4, 4:d- 4)=image;
  110. for i= 4:c- 4
  111. for j= 4:d- 4
  112. current_image = new(i- 3:i+ 3,j- 3:j+ 3);
  113. current_masked_image = mask.*current_image;
  114. % Uncomment here to implement binary thresholding
  115. % current_masked_image(find(abs(current_masked_image-current_masked_image(4,4))>threshold))=0;
  116. % current_masked_image(find(abs(current_masked_image-current_masked_image(4,4))<=threshold))=1;
  117. % This thresholding is more stable
  118. current_thresholded = susan_threshold(current_masked_image,threshold);
  119. g=sum(current_thresholded(:));
  120. if nmax<g
  121. R(i,j) = g-nmax;
  122. else
  123. R(i,j) = 0;
  124. end
  125. end
  126. end

三、运行结果

四、备注

完整代码添加QQ1575304183


转载:https://blog.csdn.net/weixin_50197058/article/details/116429315
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场