Randomizing Color to 3D Objects - improving speed
2 views (last 30 days)
Show older comments
Hi all,
I have a 3D array with spheres embedded in the array, labeled as 1, and 0 elsewhere. I want to count the spheres and then randomize color with each sphere so when I look at an arbitrary 2D slice through the 3D array, I can verify that each sphere was uniquely identified. (i.e. if two spheres were close together and were considered the same sphere, they would be the same color). Anyways I've completed the task in a simple manner (see below) and my question pertains to how to make this operation faster. Any and all help will be greatly appreciated. Thank you.
if true
% code
SphereLabel=bwlabeln(SphereBW,26);
AddMatrix = zeros(512,512,256,'uint8');
hWaitBar=waitbar(0,'Randomizing');
for i = 1:max(SphereLabel(:))
f1=round(rand(1)*255);
AddMatrix(SphereLabel==i)=f1;
waitbar(i/max(X(:)))
end
delete(hWaitBar)
AddMatrix(:,:,:,2) = AddMatrix(:,:,:,1);
AddMatrix(:,:,:,3) = AddMatrix(:,:,:,1);
end
If there is a large number of spheres in the matrix, then this will take a long time. I'm not sure if there is a faster way, but I thought I'd ask if anyone had any suggestions to improve the speed of this. Always appreciate it.
Cheers, Edwin
0 Comments
Accepted Answer
Image Analyst
on 20 Nov 2014
Edwin:
Did you know there's a function called label2rgb(). I think this is what you need.
3 Comments
Image Analyst
on 21 Nov 2014
I've only done it with 2D labeled images, but here's a snippet from my Image Segmentation Tutorial:
labeledImage = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
subplot(3, 3, 4);
imshow(labeledImage, []);
title('Labeled Image, from bwlabel()');
subplot(3, 3, 5);
imshow(coloredLabels);
caption = sprintf('Pseudo colored labels, from label2rgb().\nBlobs are numbered from top to bottom, then from left to right.');
title(caption);
More Answers (1)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!