Randomizing Color to 3D Objects - improving speed

2 visualizaciones (últimos 30 días)
Edwin
Edwin el 20 de Nov. de 2014
Comentada: Image Analyst el 21 de Nov. de 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

Respuesta aceptada

Image Analyst
Image Analyst el 20 de Nov. de 2014
Edwin:
Did you know there's a function called label2rgb(). I think this is what you need.
  3 comentarios
Edwin
Edwin el 21 de Nov. de 2014
Ah, yea that would be great! Would you mind elucidating on how to implement that with label2rgb, please? I really appreciate it!
Image Analyst
Image Analyst el 21 de Nov. de 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);

Iniciar sesión para comentar.

Más respuestas (1)

Edwin
Edwin el 20 de Nov. de 2014
Hi,
Thank you for the speedy response. I am aware of the function; my issue with that is that due to the way 'bwlabeln' labels the spheres, neighbors will have RGB values close to each other. Ideally, I would like to have completely random RGB values so neighboring spheres will have unique colors.
Also, I also realized an issue with my code. I would need to do the randomizing for all three RGB values. I.e. would need to be adjusted like:
% code
for i = 1:max(SphereLabel(:))
f1=round(rand(1)*255);
f1=round(rand(2)*255);
f1=round(rand(3)*255);
AddMatrix1(SphereLabel==i)=f1;
AddMatrix2(SphereLabel==i)=f2;
AddMatrix3(SphereLabel==i)=f3;
waitbar(i/max(X(:)))
end
delete(hWaitBar)
RGBMatrix(:,:,1,:) = AddMatrix1;
RGBMatrix(:,:,2,:) = AddMatrix2;
RGBMatrix(:,:,3,:) = AddMatrix3;
I realize I'm asking a pretty unique question here. I'm just eager to learn in hopes to increase my knowledge within matlab. Thank you for any and all help.

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by