在这段代码中,我有一个包含10个类的聚类图像,我想为每个级别提取10个不同的图像并保存为10个图像下面是代码,我使用
tic
numberOfClasses = 10;
segment_label_images = cell(1,numberOfClasses);
pixelCount = zeros(1,numberOfClasses);
[rs, cs] = size(classImage);
% classImage has intensity range from 1-numberOfClasses
for k = 1:numberOfClasses
for i = 1:rs
for j = 1:cs
if classImage(i,j) == k
segment_label_images{k}(i,j) = 1;
else
segment_label_images{k}(i,j) = 0;
end
end
end
pixelCount(k) = sum(segment_label_images{k}(:));
%figure, imshow(segment_label_images{k},[]);
end
toc
在这里,我有3个for循环,我认为这会影响计算时间。经过的时间是0.089413秒。
任何避免循环以改善补偿时间的建议。谢谢,Gopi
答案 0 :(得分:3)
假设for k = 1:numberOfClasses
segment_label_images{k} = classImage == k;
pixelCount(k) = sum(segment_label_images{k}(:));
end
是一个矩阵,你可以用
[{"eventId":"1","eventName":"INSERT","eventVersion":"1.0","eventSource":"aws:dynamodb","awsRegion":"us-west-2","image":{"Message":"New item!","Id":101}},{"eventId":"2","eventName":"MODIFY","eventVersion":"1.0","eventSource":"aws:dynamodb","awsRegion":"us-west-2","image":{"Message":"This item has changed","Id":101}},{"eventId":"3","eventName":"REMOVE","eventVersion":"1.0","eventSource":"aws:dynamodb","awsRegion":"us-west-2","image":{"Message":"This item has changed","Id":101}}]
答案 1 :(得分:0)
假设MATLAB 2016b(或Octave):
k = permute(1:numberOfClasses, [1,3,2]);
segment_label_images = (classImage == k);
pixelCount = squeeze(sum(sum(segment_label_images, 1), 2));
对于2016b之前的MATLAB,只需添加bsxfun
:
k = permute(1:numberOfClasses, [1,3,2]);
segment_label_images = bsxfun(@eq, classImage, k);
pixelCount = squeeze(sum(sum(segment_label_images, 1), 2));
当然,这两个都将segment_label_images
作为3D数组而不是单元格数组。鉴于所有阵列都是相同的大小,我更喜欢使用多维数组而不是单元数组,以提高速度和方便性。当然,如果需要,它可以转换为单元格数组。