MATLAB: Index information for conv2(.,.)

digital signal processingimage processing

I have an image X (dimension N1xN2) and a filter Y(dimension 3×5). I can use matlab command conv2(X,Y) to get the filtered image Z. Now I want to get another matrix Z_Index of dimension (N1N2 x 15). The kth row of Z_index(k,:) should contain linear index information of those x's that contributed in kth element of Z. How can I get such a matrix?

Best Answer

One possible way:
indices = reshape(1:numel(X), size(X));
usedindices = nlfilter(indices, size(Y), @(idx) {idx(:)});
Z = [usedindices{:}]'
You get 0 for those k indices near the edges due to the zero padding.