Can this be vectorised? (Adding 3D pattern to 3D matrix at specific indices)

I currently have a problem which requires adding a 3D pattern (stored as a 3D matrix) to another 3D array at a particular X,Y point, specified in a different 2D matrix of 1s and 0s. At a '1' I need to add the pattern on top of whats there, and do nothing at a '0'.
Here is the code:
for i=1:sizei
for j=1:sizej
if edge_image(i,j)==1
range_i = i-shapesize/2:i+shapesize/2;
range_j = j-shapesize/2:j+shapesize/2 ;
voting_area(range_i,range_j,:)= voting_shape + voting_area(range_i,range_j,:) ;
end
end
end
Edge_image is the binary 2d array, voting_shape is the 3d pattern matrix and voting_area is the bigger 3d matrix. (This is a Hough transform loop looking for rotations of a square in an edge image).
Currently this is extremely slow, but the fact that the 2d image is just a binary matrix makes me suspect there could be a way to speed this loop up. Is this possible at all?
Cheers

 Réponse acceptée

edge_vol=repmat(edge_image,1,1,size(voting_shape,3));
voting_area= voting_area + convn(edge_vol,voting_shape,'valid');

2 commentaires

Perfect! thankyou
I think maybe that should have been
convn(edge_vol,voting_shape,'same') %not 'valid'

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Sparse Matrices dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by