在应用bwlabel之前如何处理图像?

时间:2018-12-28 05:06:18

标签: matlab image-processing medical

I = imread('Sub1.png');
figure, imshow(I);
I = imcomplement(I);
I = double(I)/255;
I = adapthisteq(I,'clipLimit',0.0003,'Distribution','exponential');
k = 12;
beta = 2;
maxIter = 100;
for i=1:length(beta)
   [seg,prob,mu,sigma,it(i)] = ICM(I, k, beta(i), maxIter,5);
   pr(i) = prob(end);
   hold on;
end
figure, imshow(seg,[]);

,并将ICM功能定义为

function [segmented_image,prob,mu,sigma,iter] = ICM(image, k, beta, max_iterations, neigh)
    [width, height, bands] = size(image);
    image = imstack2vectors(image);
    segmented_image = init(image,k,1);
    clear c;
    iter = 0;
    seg_old = segmented_image;

    while(iter < max_iterations)
        [mu, sigma] = stats(image, segmented_image, k);

        E1 = energy1(image,mu,sigma,k);
        E2 = energy2(segmented_image, beta, width, height, k);
        E = E1 + E2;
        [p2,~] = min(E2,[],2);
        [p1,~] = min(E1,[],2);

        [p,segmented_image] = min(E,[],2);
        prob(iter+1) = sum(p);

        %find mismatch with previous step
        [c,~] = find(seg_old~=segmented_image);
        mismatch = (numel(c)/numel(segmented_image))*100;
        if mismatch<0.1
            iter
            break;
        end
        iter = iter + 1;
        seg_old = segmented_image;
    end
    segmented_image = reshape(segmented_image,[width height]);
end

我的算法的输出是大小为305 x 305的逻辑矩阵(seg)。当我使用

imshow(seg,[]);

我能够显示图像。它显示具有不同灰度值的不同成分。但是bwlabel返回1。我想显示连接的组件。我认为bwlabel将图片的阈值设置为1。unique(seg)返回的值是1到10,因为在k均值中使用的类数是10。我用过

[label n] = bwlabel(seg);
RGB = label2rgb(label);
figure, imshow(RGB);

我需要所有类似于图像中间的两个正方形之间的椭圆形结构。我不知道其中存在的类数。

输入图像:

Input Image

地面真相:

Groundtruth

我的输出:

Output

2 个答案:

答案 0 :(得分:0)

如果要将标签图像分解为不同的连接组件,则需要使用循环为每个类提取标签,并对标签图像求和以得到out标签图像。

u = unique(seg(:));
out = zeros(size(seg));
num_objs = 0;
for k = 1: numel(u)
    mask = seg==u(k);
    [L,N] = bwlabel(mask);
    L(mask) = L(mask) + num_objs;
    out = out + L;
    num_objs = num_objs + N ;
end
mp = jet(num_objs);
figure,imshow(out,mp)

会产生这样的事情

connected components

答案 1 :(得分:0)

我试图从头开始做所有事情。希望对您有所帮助。

我承认,我有一条治疗链,该链首先具有经过反复试验调整的参数的轮廓。最后一个“图像”在底部给出;有了它,您可以轻松选择连接的组件,并使用“ imreconstruct”运算符通过标记进行重建。

clear all;close all;
I = imread('C:\Users\jean-marie.becker\Desktop\imagesJPG10\spinalchord.jpg');
figure,imshow(I);
J = I(:,:,1);% select the blue channel because jpg image
J=double(J<50);% I haven't inverted the image
figure, imshow(J);
se = strel('disk',5);
J=J-imopen(J,se);
figure, imshow(J);
J=imopen(J,ones(1,15));% privilegizes long horizontal strokes
figure, imshow(J);
K=imdilate(J,ones(20,1),'same');
% connects verticaly not-to-far horizontal "segments"
figure, imshow(K);

enter image description here