필터 지우기
필터 지우기

Index information for conv2(.,.)

조회 수: 2 (최근 30일)
820408
820408 2016년 10월 26일
편집: Andrei Bobrov 2016년 10월 27일
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