Randomizing Color to 3D Objects - improving speed
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Edwin
am 20 Nov. 2014
Kommentiert: Image Analyst
am 21 Nov. 2014
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 Kommentare
Akzeptierte Antwort
Image Analyst
am 20 Nov. 2014
Edwin:
Did you know there's a function called label2rgb(). I think this is what you need.
3 Kommentare
Image Analyst
am 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);
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!