My code doesn't work the second time
11 次查看(过去 30 天)
显示 更早的评论
I'm using the following code for nucleus subtraction and then median filtering. My code works fine the first time but the second time I run it, the subtraction doesn't work. Could anyone please tell me what I'm doing wrong?
close all;
he=imread('2.jpg');
% imshow(he), title('original');
% filter
h=[1 1 1; 1 1 1; 1 1 1]/9;
b=imfilter(he,h);
% figure, imshow(b), title('filtered');
% segmentation
cform = makecform('srgb2lab');
lab_he = applycform(he,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 3;
% repeat the clustering 3 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
% imshow(pixel_labels,[]), title('image labeled by cluster index');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = he;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
% imshow(segmented_images{1}), title('objects in cluster 1');
% imshow(segmented_images{2}), title('objects in cluster 2');
% figure, imshow(segmented_images{3}), title('objects in cluster 3');
mean_cluster_value = mean(cluster_center,2);
[tmp, idx] = sort(mean_cluster_value);
blue_cluster_num = idx(1);
L = lab_he(:,:,1);
blue_idx = find(pixel_labels == blue_cluster_num);
L_blue = L(blue_idx);
is_light_blue = im2bw(L_blue,graythresh(L_blue));
nuclei_labels = repmat(uint8(0),[nrows ncols]);
nuclei_labels(blue_idx(is_light_blue==false)) = 1;
nuclei_labels = repmat(nuclei_labels,[1 1 3]);
blue_nuclei = he;
blue_nuclei(nuclei_labels ~= 1) = 0;
% figure, imshow(blue_nuclei), title('blue nuclei');
% segmentation ends here
% erosion
se = strel('ball',9,9);
e=imerode(blue_nuclei,se);
% figure, imshow(e), title('erosion');
% reconstruction erosion as marker
IM = imreconstruct(e,segmented_images{2});
% figure, imshow(IM), title('reconstructed');
% In as Mask on original image
In=b.*IM;
% figure, imshow(In), title('masked');
% Subtraction
I=imsubtract(b,In);
figure, imshow(I), title('subtracted');
figure, imshow(he);
% median filter
g=rgb2gray(I);
J = imnoise(g,'salt & pepper',0.02);
K = medfilt2(g, [3 3]);
figure, imshow(J), figure, imshow(K);
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Filtering and Enhancement 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!