clc;clear;close all;
img = imread(‘1.bmp’); I = rgb2gray(img); figure(1),imshow(I)
level =graythresh(I); BW = im2bw(I,level); se=strel(‘disk’,3’);%圆盘型结构元素 fo=imclose(BW,se);%直接开运算 BW2 = bwareaopen(1-fo, 200); %计算各个连通体的中心 [lalels,NumTotal] = bwlabel(BW2); centroid = regionprops(lalels,‘BoundingBox’,‘Area’,‘Centroid’);%重心 figure(2), imshow(img) hold on, for i=1:length(centroid) rectangle(‘Position’,centroid(i).BoundingBox, ‘LineWidth’, 2, ‘EdgeColor’,‘g’); text(centroid(i).Centroid(1),centroid(i).Centroid(2),num2str(i),‘color’,‘r’,‘FontSize’,14) end
% level2 =graythresh(I); % BW3 = im2bw(I,0.52); % figure(4),imshow(BW3)
% figure(3), for i=1:length(centroid) Irect = floor(centroid(i).BoundingBox); ITmp = I(Irect(2):(Irect(2)+Irect(4)+1), Irect(1):(Irect(1)+Irect(3)+1)); leveltmp =graythresh(ITmp); BWtmp = im2bw(ITmp,0.52); if(all(BWtmp(:)==1)) continue; end BWtmp2 = bwareaopen(1-BWtmp, 3); [lalelstmp,NumTotaltmp] = bwlabel(BWtmp2); centroidtmp = regionprops(lalelstmp,‘Area’);%重心 disp(strcat(‘第’,num2str(i),‘个细胞的各个细胞核对核仁面积的占比为:’,num2str(sum([centroidtmp.Area]/centroid(i).Area)))); % imshow(BWtmp,[]) end
|