-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathim2patch.m
34 lines (29 loc) · 1.05 KB
/
im2patch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [patches, topleftOrigin] = im2patch(im, patchDim)
% [PATCHES, TOPLEFTORIGIN] = IM2PATCH(IM, PATCHDIM)
% extract overlapping square patches of size PATCHDIM from image IM.
% PATCHES is a matrix of size PATCHDIM^2 x num_patches, the patches are arranged columnwise
% TOPLEFTORIGIN is amatrix of num_patches x 2 of the column and row indices
% of the upperleft corner of each patch.
%
% Gal Mishne, original version of code by Israel Cohen
[imHeight, imWidth, chan] = size(im);
nInHeight = length(1:1:imHeight-patchDim+1);
nInWidth = length(1:1:imWidth-patchDim+1);
num_patches = nInHeight*nInWidth;
patches = NaN(patchDim^2*chan, num_patches);
yrange = 0 : (imHeight-patchDim);
xrange = 0 : (imWidth-patchDim);
k = 1;
for c = 1:chan
for m = 1 : patchDim
for n = 1 : patchDim
patches(k, : ) = reshape(im(n+yrange, m+xrange,c), 1, num_patches);
k = k+1;
end
end
end
cols = 1+xrange;
rows = 1+yrange;
[Cols,Rows] = meshgrid(cols,rows);
topleftOrigin = [Cols(:),Rows(:)];
return;