optimization - How to compare all patches in two images in matlab? -
i have 2 images, let's a , b, sizes of need not same. a of dimension 255×255 , b of dimension 100×100. , dimension of patch 5×5 problem. need compare patches in a patches in b.
every patch overlapping neighboring patches. clear point, first patch in a a(1:5,1:5) (matlab notation). second patch a(2:6,1:5) , on, way till a(251:255,1:5) @ end of first row, , way a(251:255,251:255) last patch in a.
i have compare each of these patches against all patches in b. can see, there 251*251 patches in a , 96*96 patches in b. there lot comparisons made. , comparison euclidean distance, i.e, i'll take difference of 2 patches , sum squares. each patch comparison i'll scalar value result.
i implemented in matlab it's taking several minutes execute. please suggest me fastest way implement this. section bottlenecking entire project. code wrote given below. i'm not expert please forgive mistakes.
row = size(a,1); col = size(a,2); row2 = size(b,1); col2 = size(b,2); patch_long = zeros(5,5,(row2-4)*(col2-4)); idx = 1; = 1:row2-4 j = 1:col2-4 patch_long(:,:,idx) = b(i:i+4,j:j+4); idx = idx+1; end end %// rearranged 'b' matrix 3d matrix each patch arranged %// slide behind 1 one parfor m = 1:row-4 n = 1:col-4 temp1 = bsxfun(@minus,(a(m:m+4,n:n+4)),patch_long); temp2 = sum(sum(temp1.^2)); count = sum(temp2 <=threshold); if count > 1 % xyzend end end end %// count counts how many patches in 'b' close particular patch in 'a'.
you can use im2col extract patches
pa = im2col( a, [5 5], 'sliding' ); pb = im2bol( b, [5 5], 'sliding' ); % compute squared difference d = sum( bsxfun( @minus, permute( pa, [2 3 1] ), permute( pb, [3 2 1] ) ).^2, 3 ); by way, if not need distances , willing compromise approximation k nearest neighbors, might find patchmatch useful:
approximate k-nearest neighbors algorithm tailored image patches. efficient in terms of memory (space) usage , fast.
Comments
Post a Comment