Index information for conv2(.,.)

2 次查看(过去 30 天)
I have an image X (dimension N1xN2) and a filter Y(dimension 3x5). 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?

采纳的回答

Guillaume
Guillaume 2016-10-26
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.
  2 个评论
820408
820408 2016-10-26
编辑:820408 2016-10-26
Thank you for your reply. Can you please explain this part of your code: fun=@(idx) {idx(:)}. Actually I don't understand 'idx' part. This function is taking idx as input and returning a cell that contains a column vector of relevant indices but I don't get where idx is coming from? Is it something inbuilt?
Guillaume
Guillaume 2016-10-27
nlfilter works exactly the same as conv2 (with the 'full' option|) except that you specify the function that is to operate on the sliding block. In fact you could replicate conv2 with the function @(block) sum(sum(block .* Y)).
In this case, instead of filtering the original image, I filter the indices of the image pixel. nlfilter slides blocks of size size(Y) over the image and pass these pixels (in this case whose value is their indices) as a matrix to the filtering function I specify. That function is very simple, simply reshape that matrix of indices into a single column and pack it up into a scalar cell array. It needs to be put into a scalar cell array because nlfilter expect a scalar value back from the filtering function.
nlfilter combines all these scalar into a matrix but since I returned scalar cell arrays that combined matrix is actually a cell array containing column vectors. The next line simply concatenate all these column vectors into one matrix and transpose to get the orientation you wanted.
You could do the same with a loop which may be faster as nlfilter is not particularly fast. nlfilter just abstracts away the complexity of the loop.

请先登录,再进行评论。

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2016-10-27
编辑:Andrei Bobrov 2016-10-27
Without Image Processing ...
m = 3;
n = 5;
A = reshape(1:numel(X),size(X));
[k,l] = size(A);
s = floor([m,n]/2)
B = zeros(size(A)+s*2);
B(s(1)+1:end-s(1),s(2)+1:end-s(2)) = A;
C = reshape(1:numel(B),size(B))
D = C(1:end-m+1,1:end-n+1);
[x,y] = size(C);
M = bsxfun(@plus,(0:m-1)',(0:n-1)*x);
out = B(bsxfun(@plus,D(:),M(:)'));
with Image Processing
out = im2col(padarray(reshape(1:numel(X),size(X)),floor([m,n]/2),0),[m,n])';

Community Treasure Hunt

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

Start Hunting!

Translated by