フィルターのクリア

Extracting image patches around each pixel

4 ビュー (過去 30 日間)
VIJENDRA
VIJENDRA 2016 年 10 月 6 日
コメント済み: Devkumar Das 2017 年 8 月 2 日
Suppose I have an image of size 256*256
Image = imread('cameraman.tif');
I have to extract patches of 7*7 around each pixel of image
patchSize = 7;
Around edges I padded image with zeros pixel
padcam = padarray(Image,[7 7],'both');
Total number of pixels is 256*256 therefore total patches would be 65536
[m,n] = size(padcam);
ImagePatches = zeros(patchSize,patchSize,256*256);
tic
for k = 1:65536
for i = 8:263
for j = 8:263
ImagePatches(:,:,k) = padcam(i-3:i+3, j-3:j+3);
end
end
end
toc
Due to 3 for-loops the operation is taking very long time. How can i optimize this code?
  2 件のコメント
Adam
Adam 2016 年 10 月 6 日
編集済み: Adam 2016 年 10 月 6 日
You should only need to loop twice. One loop is redundant. There are probably quicker methods that I don't have time to think about so someone else will likely suggest them, but instead of your outer k loop just keep a counter that you initialise and increment within your inner loop to give you your k index into the ImagePatches.
VIJENDRA
VIJENDRA 2016 年 10 月 7 日
Thanks Adam

サインインしてコメントする。

回答 (1 件)

Guillaume
Guillaume 2016 年 10 月 7 日
Since you have the imaging toolbox, a much simpler way to generate your output is by subverting nlfilter. You don't even need to pad your image as nlfilter does that for you:
ImagePatches = nlfilter(Image, [7 7], @(block) {block}); %store each block generated by nlfilter into scalar cell of cell array
ImagePatches = cat(3, ImagePatches{:}); %and concatenate in 3rd dimension
  1 件のコメント
Devkumar Das
Devkumar Das 2017 年 8 月 2 日
Hi Guillaume, If I want to extract patch for a color image then nlfilter will not work. Can you suggest something for that?

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by