Is there any way to make this code faster?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi, I have this code that tosses out pixels from a cc list
for i = 1:cc.NumObjects
for j = length(cc.PixelIdxList{i}):-1:1
ind = cc.PixelIdxList{i}(j);
if ~imdiff(ind) || ~imcombined(ind)
cc.PixelIdxList{i}(j) = [];
end
end
end
imdiff and imcombined are both images. The code at the moment takes quite a while to churn through a decent sized image. Is there any way to make this code run faster?
0 件のコメント
採用された回答
Daniel Shub
2012 年 7 月 17 日
I think this is a memory allocation issue. Every time you set cc.PixelIdxList{i}(j) = [], MATLAB needs to allocate new memory. You might be better off building an array that holds the indices to remove, and then removing them at the end.
その他の回答 (1 件)
Sean de Wolski
2012 年 7 月 17 日
編集済み: Sean de Wolski
2012 年 7 月 17 日
That whole thing should be vectorizable assuming imdiff and imcombined (whatever those are) can handle it:
cellfun(@(c)c(imdiff(c)&imcombined(c)),CC.PixelIdxList,'uni',false)
3 件のコメント
Sean de Wolski
2012 年 7 月 17 日
No. I just like it :)
You could keep the outer for-loop just as easily. The point is that the inner for-loop does not need to check each value if imdiff and imcombined are either images or functions that can be passed vector inputs.
参考
カテゴリ
Help Center および File Exchange で Encryption / Cryptography についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!