Clear Filters
Clear Filters

reshaping error after clustering pixels > 0 in image

2 views (last 30 days)
Updated: I have an image in which I have isolated the tumor region using thresholding (img 1) . Now im trying to use fuzzy clustering to cluster the pixels in just the tumor ROI but not the black background (basically ignore any pixels = 0 in clustering).When I try to segment the clusters into images it s not outputting any clustered images, because its throwing an error that "to reshape the number of elements must not change." I've been trying to edit my code but the results are the same.
fuzzyT2 = mriAdjust(:,:,15);
[y, x] = find(fuzzyT2)
%pre-allocating a new variable re_fuzzyT2 that is the row numbers of ab that are not all zero;
re_fuzzyT2 = fuzzyT2(111:182,69:123); %72 x 55 uint16
%create output = pixel_label as zeros of the proper shape
cluster_center = im2uint16(zeros(256,256));
%store results of clustering ab(idx,:) into pixel_label(idx)
idx = fuzzyT2 > 0 %creates a mask for true value for all nonzero pixels
z = fuzzyT2(idx);
%% fuzzy c-means
G = 4
z = double(z)
[cluster_idx, cluster_center, obj] = fcm(z, G)
% reshape into four 2-D array
cluster1 = cluster_center(1,:);
cluster2 = cluster_center(2,:);
cluster3 = cluster_center(3,:);
cluster4 = cluster_center(4,:)
imIDX1 = reshape(cluster1, size(idx));
imIDX2 = reshape(cluster2, size(idx));
imIDX3 = reshape(cluster3, size(idx));
imIDX4 = reshape(cluster4, size(idx));
subplot(2,2,1), imshow(img_1), title('cluster 1')
subplot(2,2,2), imshow(img_2), title('cluster 2')
subplot(2,2,3), imshow(img_3), title('cluster 3')
subplot(2,2,4), imshow(img_4), title('cluster 4')

Accepted Answer

Adam Danz
Adam Danz on 6 Nov 2019
I understand the problem but I don't have an entirely clear picture of what the imIDX variables should be.
I think all you need to do is replaced these reshape lines
imIDX1 = reshape(cluster1, size(idx));
with these pairs of lines
imIDX1 = zeros(size(idx));
imIDX1(idx) = cluster1;
so give that a try and it it's not what you're looking for, please describe how it differs from your goal.
  2 Comments
Raheema Al Karim Damani
Raheema Al Karim Damani on 6 Nov 2019
Thank you so much it worked! I'm still learning matlab can you explain your logic behind your code?
I really appreciate your help!
Adam Danz
Adam Danz on 6 Nov 2019
Edited: Adam Danz on 6 Nov 2019
It wasn't clear to me what the result of the code should be. I guessed that you want imIDX1 to be the same size as your original image.
imIDX1 = zeros(size(idx));
This line creates a matrix of 0s that is the same size as your fuzzyT2 data.
imIDX1(idx) = cluster1;
This line replaces some of the 0s identified by 'idx' with data from 'cluster1'.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!