结核杆菌分割

image=imread('000089.jpg');
maxPixel=max(image);
minPixel=min(image);
imageResize=imresize(image,0.5);
hsvImage=rgb2hsv(imageResize);
vI=hsvImage(:,:,3);
figure(1);
subplot(131);imshow(imageResize,[]);
subplot(132);imshow(vI,[]);
%% 彩色图像增强
%中值滤波
filterIma=medfilt2(vI,[3 3]);
subplot(133);imshow(filterIma,[]);
figure(2);
se=strel('disk',3);
t1=imerode(filterIma,se);
t1=imdilate(t1,se);
f1=vI-t1; %顶帽变换,原图-顶帽变换
subplot(131);imshow(f1,[]);
t2=imdilate(filterIma,se);
t2=imerode(t2,se); %底帽变换
subplot(132);imshow(t2,[]);
f2=vI-t2;
subplot(132);imshow(f2,[]);
ft=(vI+f1)-f2;
subplot(133);imshow(ft,[]);
hsvImage(:,:,3)=ft;
trgbImage=hsv2rgb(hsvImage);
rgbImage=trgbImage;
figure(3);
subplot(131);imshow(image,[]);
subplot(132);imshow(trgbImage,[]);
% grayImage=rgb2gray(rgbImage);
% subplot(133); imshow(grayImage,[]);
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(rgbImage), hy, 'replicate');
Ix = imfilter(double(rgbImage), hx, 'replicate');
gradmag = sqrt(Ix.^2 + Iy.^2);%求梯度
subplot(133); imshow(gradmag,[]); %显示梯度
%2. 直接使用梯度模值进行分水岭算法:(往往会存在过分割的情况,效果不好)
L = watershed(gradmag);%直接应用分水岭算法
subplot(133);imshow(double(L),[]);
figure;imshow(double(L));
g2 = imclose(imopen(gradmag, ones(2,2)), ones(2,2));%梯度闭操作
L2 = watershed(g2);
figure;imshow(L2);
wr2 = L2 == 0;
f2 = rgbImage;
f2(wr2) = 255; %画出白色边界线
figure;imshow(f2);
%%
%分水岭变换
% f=trgbImage;
% figure;imshow(trgbImage);title('原图像');
% h=fspecial('sobel');    %sobel边缘检测
% figure;imshow(h);title('sobel');
% fd=trgbImage;          %图像转化为浮点型
% g=sqrt(imfilter(fd, h, 'replicate') .^ 2 + imfilter(fd, h', 'replicate') .^ 2);     %计算梯度
% figure;imshow(g);title('梯度');
% L = watershed(g);   %分水岭线
% figure;imshow(double(L));title('分水岭线');
% wr = L == 0;
% g2 = imclose(imopen(g, ones(3,3)), ones(3,3));  %对梯度图像形态学闭操作
% figure;imshow(g2);title('梯度闭操作后');
% L2 = watershed(g2);
% figure;imshow(double(L2));title('分水岭线2');
% wr2 = L2 == 0;
% f2 = f;
% f2(wr2) = 255;
% figure;imshow(f2);title('分割结果');
%% 相邻区域最大相似度准则合并
[m,n,l]=size(imageResize);
regionIndex=ones(m,n);
r=L2(:,:,1);
g=L2(:,:,2);
b=L2(:,:,3);
for i=1:m
    for j=1:n
        if((r(i,j)==0)&&(g(i,j)==0)&&(b(i,j)==0))
            regionIndex(i,j)=0;
        end
    end
end
figure;imshow(regionIndex,[]);
timage= drawContour( imageResize,regionIndex,1 );
figure;imshow(timage,[]);
[imageLabel,num]=bwlabeln(regionIndex);
regionArea=regionprops(imageLabel,'Area');
numArea_segment=[regionArea.Area];
index1=find(numArea_segment==max(numArea_segment));%找到最大连通区域
segmentLargeRegion=ismember(imageLabel,index1);
segment=imfill(segmentLargeRegion,'holes');

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值