What exactly does ordfilt2 do?

Can you please explain in simple terms what do the input parameters indicate in the ordfilt2 function in matlab?

B=ordfilt2(A,Order,Domain)

I have seen people use this function as J = ordfilt2(I, 9, true(5)) or J=ordfilt2(I,25,ones(3,3))

But I do not understand what each input does to the image I to give J as output..

Thanks a lot in advance

• RTFM? Perhaps if you could explain what you do not understand about the manual entry? – Peter K. Nov 10 '13 at 1:30
• In this example you used a [1 1; 1 1] matrix as your domain. What if you had a matrix that's either [0 1 0; 1 0 1; 0 1 0] or [1 1 1; 1 1 1; 1 1 1]. Actually, my question is: how do the zero elements affect the filter output? – Rafael Sep 20 '15 at 11:54

I'm pretty new to this myself, so please correct me if I get this wrong.

Using your example, J = ordfilt2(I, 9, true(5)). ordfilt2 will move over the 2d array I in blocks of the same size as true(5). For each of these 5x5 blocks, sort all the elements from smallest to largest. Now fill in the corresponding block in J with a bunch of copies of the 9th smallest element.

To use a smaller example, so I have room to type it:

I = [ 1 2 4 5 ;
5 3 5 1 ;
0 3 5 2 ;
2 1 7 7 ];
J = ordfilt2(I,3, ones(2,2));

Now, let's go through a few blocks one at a time. The first ones(2,2) block is [1 2; 5 3] in the top left corner. If we sort these elements, we get [1 2 3 5], and since we're looking for the 3rd smallest, we receive a value of 3 for the (1,1) position of J.

J = [ 3 ? ? ? ;
? ? ? ? ;
? ? ? ? ;
? ? ? ? ];

next up is the I(1:2,2:3) block. Ordering those elements gives [2 3 4 5], so the third smallest is 4. Now we replace J(1,2) with a 4.

J = [ 3 4 ? ? ;
? ? ? ? ;
? ? ? ? ;
? ? ? ? ];

Go ahead and run this command and compare I, J to understand what's happening. One other thing to know is that the input matrix is padded by default with zeros at the lower and right sides. So the I(4:5,4:5) block is [ 7 0; 0 0].

• Looks like you can RTFM! :-) +1. – Peter K. Nov 13 '13 at 17:32

protected by jojek♦Sep 20 '15 at 19:37

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?