selecting neighborhood in 2D matrix

I have a 2D matrix A and a mask M with a pivot element at location (i,j). I need to select a neighborhood of an entry of A at (p,q) based on the shaped of mask. How can I place my pivot element of mask at location (p,q) in A. I can create an alias of mask with logical entries to select the neighborhood but I don't know how to first make sure that the pivot element is aligned with the (p,q) entry. Do I really need to first align the pivot element of mask with the (p,q) entry do select the neighborhood? I am not really looking for some stuff from image processing toolbox since my 2D matrix is not an image. I would appreciate if someone can suggest a general solution

Risposte (1)

Image Analyst
Image Analyst il 27 Mag 2013

0 voti

I don't know what you want to do. You're using non-standard lingo, at least as far as imaging goes. What is a pivot element? A mask is a 2D matrix of some extent, so what is it's pivot element? Which element of the mask? The center one? The upper left??? By the way, any 2D array can be considered as an image as far as the Image Processing Toolbox goes. It doesn't matter if you consider it an image, a mask, a sliding window, a kernel, or whatever you want to call it or how you interpret it.
Why don't you just say what you want to do? Like you want to mask an image, or you want to blur an image with a sliding box filter, or you want to do a hit or miss filter, or you want to do a morphological closing, or whatever.

7 Commenti

My 2D matrix is a random matrix whose elements belong to {+1,-1}. This is a Communication problem so that is why I avoid to use image processing terminology. The pivot element can be any element of the mask. It can be center one, corner left, or any other depending upon how I am defining a mask. It is because of this very reason that I am looking for a solution which first aligns my mask's pivot element with the (p,q) element of the image. Did I explain what you were looking for?
xplore29
xplore29 il 27 Mag 2013
Modificato: xplore29 il 27 Mag 2013
my mask only selects the relevant part of my 2D matrix for subsequent processing. I tend to use the term of pivot element because this element can be at any location.
e.g Mask = [1 1 0; 1 1 1;0 0 1]
Mask is not necessarily regular in shape that is why i am using zeros in declaring it. Now if the pivot element is (3,3) then the relevant part of my matrix would be different as compared to when the pivot element is (1,2)
For (3,3), Relevant matrix would be [(p-2,q-2) (p-2,q-1) 0; (p-1,q-2) (p-1,q-1) (p-1,q); 0 0 (p,q)]
For (1,2), Relevant matrix would be [(p,q-1) (p,q) 0; (p+1,q-1) (p+1,q) (p+1,q+1); 0 0 (p+2,q+1)]
If you have the Image Processing Toolbox, I think you could use bwhitmiss() or normxcorr2(). Can you give a small sample 2D matrix, and a sample mask, and the expected output?
xplore29
xplore29 il 27 Mag 2013
Modificato: Image Analyst il 27 Mag 2013
I dont have Image Processing Toolbox. I came up with one possible solution. Might not be a smart one but at least gives me an idea that I might not need to align the pivot element with (p,q)
%------------------------------START---------------------------------
function TestSlidingWindow
Image = [1 2 3 4 5 6 7 8 9;
9 0 1 2 3 4 5 6 7;
8 9 0 1 2 3 4 5 6;
7 8 9 0 1 2 3 4 5]
Mask = [0 0 1;1 1 0;1 1 1]
% Define an alias to perform sliding window
Alias = ones(size(Mask))
CellArray = cell(size(Image,1)-size(Mask,1)+1,...
size(Image,2)-size(Mask,2)+1);
for i=1:size(Image,1)-size(Mask,1)+1
for j=1:size(Image,2)-size(Mask,2)+1
CellArray{i,j}=Image(i:i+size(Mask,1)-1,j:j+size(Mask,2)-1);
CellArray{i,j}
CellArray{i,j}=CellArray{i,j}.*Mask;
CellArray{i,j}
end
end
%------------------------------END---------------------------------
Please comment on the weak points.
but still I think I will need to d the alignment.For the above example the pivot element seems to be Mask(2,2) which will not be the case always. or should I think about coming up with an Extended_Mask in which the pivot elements falls at the center, formulate the extended_alias and do the processing. I think I should try doing this
Don't use Image as the name of a variable since it's too close to the built in function image(). I don't see any need for "Alias". You might be able to get a little more speed if you reverse the loops over i and j. Put the loop over rows inside the loop over columns since MATLAB stores memory going down a column (incrementing rows first), before it jumps to the next column. So the loop over i (rows) should be inside. Finally I'd recommend using rows and columns rather than i and j because it's clearer and more descriptive.
but you do agree that bring the pivot element at the center of extended_mask will make sliding window more easy to handle?

Accedi per commentare.

Tag

Richiesto:

il 26 Mag 2013

Community Treasure Hunt

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

Start Hunting!

Translated by