Sliding neighborhood - how to vectorize?
古いコメントを表示
Dear all,
Can you please help me to vectorize (or speed-up somehow else) this code? Below is the original (parfor) version and the vectorized one, but its not working (the image is different). How to vectorize this, where is the error? The inner loop (two lines) is executed 47 bln times in my code, so any speed up is a good thing.
noised = imnoise(zeros(230,230), 'salt & pepper', 0.2);
imshow(noised, []); impixelinfo
%%Oryginal
myTempModel = zeros(1, 230);
signalInBlock = zeros(230, 230);
tic
parfor i = 1 : 199
myTemp = myTempModel;
ii=i+31;
for j = 1 : 199
block = noised( i:ii, j:j+31);
myTemp(j+15) = sum(block(:));
end
signalInBlock(i+15, :) = myTemp;
end
toc
imshow(signalInBlock,[]); impixelinfo
%%Vectorized, but not working
signalInBlock = zeros(230, 230);
tic
i = 1:1:199;
j = 1:1:199;
signalInBlock(i+15, j+15) = sum(sum(noised(i:i+31, j:j+31)));
toc
imshow(signalInBlock,[]); impixelinfo
Best regards, Alex
採用された回答
その他の回答 (1 件)
Alex Kurek
2015 年 9 月 25 日
2 件のコメント
Joseph Cheng
2015 年 9 月 25 日
編集済み: Joseph Cheng
2015 年 9 月 25 日
good catch, I stuck the figure portion towards the end to visually compare the parfor output and the conv2 output. forgot to copy the timing results without the figure when replying to you
Image Analyst
2015 年 9 月 25 日
conv2() is highly optimized, especially for separable kernels like you're using (just a flat box filter). You won't find anything faster. You could compare it with imfilter() if you want - it's similar.
カテゴリ
ヘルプ センター および File Exchange で Parallel Computing Toolbox についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!