Sum of Absolute differences between images in Matlab

时间秒杀一切 提交于 2019-12-08 03:23:58

问题


I want to implement sum of absolute difference in Matlab to establish a similarity metric between between one video frame and 5 frames either side of this frame (i.e. past and future frames). I only need the SAD value for the co-located pixel in each frame, rather than a full search routine, such as full search.

Obviously I could implement this as nested loops such as:

bs = 2; % block size
for (z_i = -bs:1:bs)
    for (z_j = -bs:1:bs) 

        I1(1+bs:end-bs,1+bs:end-bs) = F1(1+bs+z_i:end-bs+z_i, 1+bs+z_j:end-bs+z_j);
        I2(1+bs:end-bs,1+bs:end-bs) = F2(1+bs+z_i:end-bs+z_i, 1+bs+z_j:end-bs+z_j);

        sad(:,:) = sad(:,:) + abs( I1(:,:) - I2(:,:));

    end
end

However I'm wondering is there a more efficient way of doing it than this? At the very least I guess I should define the above code snippet as a function?

Any recommendations would be grateful accepted!


回答1:


You should use the command im2col in MATLAB you will be able to do so in Vectorized manner.
Just arrange each neighborhood in columns (For each frame).
Put them in 3D Matrix and apply you operation on the 3rd dimension.

Code Snippet

I used Wikipedia's definition of "Sum of Absolute Differences".

The demo script:

```

% Sum of Absolute Differences Demo

numRows = 10;
numCols = 10;

refBlockRadius = 1;
refBlockLength = (2 * refBlockRadius) + 1;

mImgSrc     = randi([0, 255], [numRows, numCols]);
mRefBlock   = randi([0, 255], [refBlockLength, refBlockLength]);

mSumAbsDiff = SumAbsoluteDifferences(mImgSrc, mRefBlock);

```

The Function SumAbsoluteDifferences:

```

function [ mSumAbsDiff ] = SumAbsoluteDifferences( mInputImage, mRefBlock )
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here

numRows = size(mInputImage, 1);
numCols = size(mInputImage, 2);

blockLength = size(mRefBlock, 1);
blockRadius = (blockLength - 1) / 2;

mInputImagePadded = padarray(mInputImage, [blockRadius, blockRadius], 'replicate', 'both');

mBlockCol = im2col(mInputImagePadded, [blockLength, blockLength], 'sliding');

mSumAbsDiff = sum(abs(bsxfun(@minus, mBlockCol, mRefBlock(:))));

mSumAbsDiff = col2im(mSumAbsDiff, [blockLength, blockLength], [(numRows + blockLength - 1), (numCols + blockLength - 1)]);


end

```

Enjoy...



来源:https://stackoverflow.com/questions/23663437/sum-of-absolute-differences-between-images-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!