Randomizing Color to 3D Objects - improving speed
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Image Analyst
2014년 11월 20일
Edwin:
Did you know there's a function called label2rgb(). I think this is what you need.
댓글 수: 3
Image Analyst
2014년 11월 21일
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);
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!