Clear Filters
Clear Filters

How to pass DICOM images to k mean clustering algorithm

1 view (last 30 days)
I have given my code below,I need to do the k-means segmentation using .dcm image,when i run the code I didnt get any errors,but my matlab got stuck.Is there any mistakes in my code,that will make the matlab stuck?
img = dicomread('C:\Users\Desktop\test\hii.dcm');
imshow(img, []);
info = dicominfo('C:\Users\Desktop\test\hii.dcm');
disp(info)
noise_img = imnoise(img,'salt & pepper',0.02);
imshow(noise_img, []);
denoise_img = medfilt2(noise_img);
figure;imshow(denoise_img, []);title('Denoised Image');
[out1, out2, out3, out4] = dwt2(denoise_img,'haar');
trans_img = [out1 out2;out3 out4];
figure;imshow(trans_img,[]);title('Trans_Image');
inv_trans_img = idwt2(out1,out2,out3,out4,'haar');
figure;imshow(inv_trans_img,[]);title('inv_trans_img');
k = 5;
[Centroid,new_cluster]=kmeans_algorithm(inv_trans_img,k);
for i_loop = 1:k
cluster = zeros(size(inv_trans_img));
pos = find(new_cluster==i_loop);
cluster(pos) = new_cluster(pos);
figure; imshow(cluster,[]);title('K-means');
data=cluster;
end
K means implementation code
function [Centroid,new_cluster]=kmeans_algorithm(input_image,k)
input_image=double(input_image);
new_image=input_image;
input_image=input_image(:);
min_val=min(input_image);
input_image=round(input_image-min_val+1);
length_input_image=length(input_image);
max_val=max(input_image)+1;
hist_gram=zeros(1,max_val);
hist_gram_count=zeros(1,max_val);
for i=1:length_input_image
if(input_image(i)>0)
hist_gram(input_image(i))=hist_gram(input_image(i))+1;
end;
end
IDX=find(hist_gram);
hist_length=length(IDX);
Centroid=(1:k)*max_val/(k+1);
while(true)
old_Centroid=Centroid;
for i=1:hist_length
new_val=abs(IDX(i)-Centroid);
hist_val=find(new_val==min(new_val));
hist_gram_count(IDX(i))=hist_val(1);
end
for i=1:k,
loop_count=find(hist_gram_count==i);
Centroid(i)=sum(loop_count.*hist_gram(loop_count))/sum(hist_gram(loop_count));
end
if(Centroid==old_Centroid) break;end;
end
length_input_image=size(new_image);
new_cluster=zeros(length_input_image);
for i=1:length_input_image(1),
for j=1:length_input_image(2),
new_val=abs(new_image(i,j)-Centroid);
loop_count=find(new_val==min(new_val));
new_cluster(i,j)=loop_count(1);
end
end
Centroid=Centroid+min_val-1;
  7 Comments
Image Analyst
Image Analyst on 20 Sep 2016
What are the features? It looks like you have only one feature and that is the gray level and you are specifying that there must be 5 clusters. I have a demo for that if you're interested. (On my other computer at home, not here.)
Zainab Mohamed
Zainab Mohamed on 21 Feb 2021
Hi, can you help me to segment a dicom medical type image using kmean?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Sep 2016
You have
while(true)
old_Centroid=Centroid;
for i=1:hist_length
new_val=abs(IDX(i)-Centroid);
hist_val=find(new_val==min(new_val));
hist_gram_count(IDX(i))=hist_val(1);
end
for i=1:k,
loop_count=find(hist_gram_count==i);
Centroid(i)=sum(loop_count.*hist_gram(loop_count))/sum(hist_gram(loop_count));
end
if(Centroid==old_Centroid) break;end;
end
This is an infinite loop that is terminated only if all of the newly computed centroid values are bit-for-bit identical with the old centroid values. That kind of programming is vulnerable to difficulties with numeric round-off (see http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F), and is vulnerable to overshoot/undershoot problems where the computed values keep cycling around the "true" solution, first one side of the solution then the other side...
You should be testing for equality within a tolerance, and you should have a "fail-safe" maximum iteration check.
(I did not check to see if that is generally correct code for kmeans clustering: the above are reasons why that implementation could fail even if the code is generally correct.)
  26 Comments
Suba Suba
Suba Suba on 9 Oct 2016
Edited: Suba Suba on 9 Oct 2016
@Walter Roberson, I already posted my full code here,the problem now I'm facing is when I try this code for the below image,it's extracting the tumor as well as the edge the image also there.I google it but failed at the end.Please show me a way to remove those edges.Please help me out this
P.S. I have attached both the source and the final output image,with this comment. Source image is the .zip file

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!