Ihave got a matlab code for patch extraction of an image. please explain the algorithm behind that code

1 view (last 30 days)
Ihave got a matlab code for patch extraction of an image from internet. but i dont know the algorithm behind that code.I am new in matlab. Can anyone help me by explaining the code.. the code is given below.
patchsize = 11;
imsize = 11*24;
y = imresize(y,[imsize imsize]);
figure;imshow(y);
image_out = [];
for i = 1:patchsize:imsize
image_out_tmp = [];
for j = 1:patchsize:imsize
tmp_img = y((i+1-1):i+patchsize-1,(j+1-1):j+patchsize-1);
tmp_img = [ones(patchsize,1) tmp_img ones(patchsize,1)];
tmp_img = [ones(1,patchsize+2);tmp_img;ones(1,patchsize+2)];
image_out_tmp = [image_out_tmp tmp_img];
end
image_out = [image_out;image_out_tmp];
end
figure;imshow(image_out);

Answers (1)

DGM
DGM on 1 Nov 2023
Edited: DGM on 1 Nov 2023
To answer @R Sai Kiran's question:
If we assume the following:
  • the intent is to pad the tiles with white
  • aspect ratio should be preserved
  • data class should be preserved
... then the given code makes some presumptions:
  1. images are always square
  2. images are always unit-scale double
  3. images are always single-channel
We can do some basic cleanup and add comments to help explain it.
% do it the dumb way (~35ms)
inpict = imread('cameraman.tif');
% isolate the script parameters
patchsize = 11;
numpatches = 24;
% adjust the input image accordingly
imsize = patchsize*numpatches;
inpict = imresize(inpict,[imsize imsize]);
% construct the output image by growing (bad)
outpict = [];
for i = 1:patchsize:imsize
image_out_tmp = [];
for j = 1:patchsize:imsize
% extract a sample patch
tmp_img = inpict((i+1-1):i+patchsize-1,(j+1-1):j+patchsize-1);
% pad the patch with ones
% padding with ones probably won't make sense
% unless we presume that the input is always floating point
tmp_img = [ones(patchsize,1) tmp_img ones(patchsize,1)];
tmp_img = [ones(1,patchsize+2); tmp_img; ones(1,patchsize+2)];
% grow the array by concatenating the patch
image_out_tmp = [image_out_tmp tmp_img];
end
% grow the array by concatenating a row of patches
outpict = [outpict; image_out_tmp];
end
imshow(outpict);
The script works, but it's slow and inflexible. Because the input image is uint8, padding with ones doesn't do what's expected.
We could instead just directly construct the output from the input using loops. In this example, constraints #2 and #3 no longer apply. The input image can be any standard image class and can have more than one channel. Class won't be preserved for logical inputs, but that's easily fixed. The code is significantly faster. Since we're defining both tile size and count as before, we still have constraint #1 if we want aspect ratio to be preserved.
%% do it the hard way (~10ms)
inpict = imread('cameraman.tif');
% parameters
patchsize = 11;
numpatches = 24;
padwidth = 1;
% adjust the input image accordingly
imsize = patchsize*numpatches;
inpict = imresize(inpict,[imsize imsize]);
% preallocate the output array
outpatchsize = patchsize + 2*padwidth;
szout = [[1 1]*numpatches*outpatchsize size(inpict,3)];
valrange = getrangefromclass(inpict); % black,white values for this numeric class
outpict = valrange(2)*ones(szout,class(inpict)); % preallocate output with white pad color
% transfer patch content
for tr = 1:numpatches
yin = (tr-1)*patchsize + 1:tr*patchsize;
yout = (tr-1)*outpatchsize + padwidth + 1:tr*outpatchsize - padwidth;
for tc = 1:numpatches
xin = (tc-1)*patchsize + 1:tc*patchsize;
xout = (tc-1)*outpatchsize + padwidth + 1:tc*outpatchsize - padwidth;
outpict(yout,xout,:) = inpict(yin,xin,:);
end
end
imshow(outpict);
That's faster and does what's expected.
Would I do it that way? No. If I had these constraints, I'd do something like:
%% do it the MIMT way (~10ms)
inpict = imread('cameraman.tif');
% parameters
patchsize = [11 22]; % [y x]
numpatches = [24 12]; % [y x]
padwidth = 1;
padcolor = [0.9 0.3 1]; % any color
% adjust the input image accordingly
imsize = patchsize.*numpatches;
inpict = imresize(inpict,imsize);
% detile the input
outpict = imdetile(inpict,numpatches);
% add padding
outpict = addborder(outpict,padwidth,padcolor,'normalized');
% retile the output
outpict = imtile(outpict,numpatches); % MIMT imtile(), not IPT imtile()!
imshow(outpict);
That would allow me some extra flexibility to specify tile geometry and count. Padding color can be whatever I want. Class still isn't preserved for logical inputs, but considering the padding flexibility, it may not make sense to preserve logical class by default.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!