【发布时间】:2018-06-12 18:38:13
【问题描述】:
我有 100 个图像,每个图像大小为 512 x 512,存储在一个元胞数组中。
我想通过搜索所有图像来找到每个像素位置的最大值和索引。
我的代码:
imgs = cell(1,5);
imgs{1} = [2,3,2;3,2,2;3,1,1];
imgs{2} = [2,3,1;4,2,3;2,2,1];
imgs{3} = [3,2,1;5,3,5;3,2,3];
imgs{4} = [4,4,2;5,3,4;4,2,2];
imgs{5} = [4,5,2;4,2,5;3,3,1];
[nrows, ncols] = size(imgs{1});
maxVal_Mat = zeros(nrows,ncols);
maxIdx_Mat = zeros(nrows,ncols);
for nrow = 1:nrows
for ncol = 1:ncols
[maxVal_Mat(nrow, ncol), maxIdx_Mat(nrow, ncol)] = max(cellfun(@(x) x(nrow, ncol) , imgs));
end
end
maxVal_Mat =
4 5 2
5 3 5
4 3 3
maxIdx_Mat =
4 5 1
3 3 3
4 5 3
关于如何优化此代码以节省执行时间和内存的任何想法。
注意:这是问题的示例演示,原始单元格和矩阵都很大。
谢谢,
戈皮
【问题讨论】:
-
您对一个非常清晰的问题表示赞同!继续加油!
-
@Dev-iL 谢谢!