我利用subject.ct(256x256x63)分割出了5种脑部的组织,期望给他们上色并在同一个图里面显示出来;但运行这段代码时候,花费近30分钟,十分缓慢,效果也不好。
for j=1:z%该循环是比较subject.ct数据和其他数据
for m=1:x
for n=1:y
if subject.ct(m,n,j)==ctbone(m,n,j)
labelct(m,n,j)=1;
elseif subject.ct==ctair
labelct(m,n,j)=2;
elseif subject.ct==ctwater
labelct(m,n,j)=3;
elseif subject.ct==ctfat
labelct(m,n,j)=4;
elseif subject.ct==ctbrain
labelct(m,n,j)=5;
else label(m,n,j)=6;
end
end
end
colorimg(:,:,j) = drawlabel2image(subject.ct(:,:,j),label(:,:,j),color,alpha);%drawlabel2img是一段自动上色的代码,通过它给每一层上色
end
代码慢的原因是循环层数太多,首先subject.ct,ctbone和labelct等变量的长度是一致的。
可以直接用 == 运算,给个示例:
X = [1 2 3 4 5 6 7 8 9 0];
Y = [1 2 2 2 3 6 7 8 9 0];
B = zeros(size(X));
B(X == Y) = 1
输出结果:
B =
1 1 0 0 0 1 1 1 1 1
楼主的问题也可以用这种方法,示意代码如下:
labelct(:) = 6;
labelct(subject.ct == ctbone) = 1;
labelct(subject.ct == ctair) = 2;
labelct(subject.ct == ctwater) = 3;
labelct(subject.ct == ctfat) = 4;
labelct(subject.ct == ctbrain) = 5;
for j=1:z
%drawlabel2img是一段自动上色的代码,通过它给每一层上色
colorimg(:, :, j) = drawlabel2image(subject.ct(:, :, j),label(:, :, j), color, alpha);
end
可以用parfor,并行计算
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!