Pergunta

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!

Foi útil?

Solução

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...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top