User:Hjian9333/sandbox

=Non-Maximum Suppression=

Introduction
Non-maximum suppression is a key post-processing step in many computer vision applications. It is an integral part of many proposed approaches in detection, such as edge, corner or object detection. Its necessity stems from the imperfect ability of detection algorithms to localize the concept of interest, resulting in groups of several detections near the real location.

In the context of edge detection, after applying gradient calculation, the edge extracted from the gradient value is still quite blurred. Thus, the image is scanned along the image gradient direction, and if pixels are not part of the local maxima they are set to zero. This has the effect of suppressing all image information that is not part of local maxima.

In the context of object detection, when searching for objects in an image several points are usually found as objects but some of them are not really objects, non-maximum suppression selects which of those maxima are really objects and suppress those that are not.

Edge Detection
For edge detection, non-maximum suppression is applied to "thin" the edge. After applying gradient calculation, the edge extracted from the gradient value is still quite blurred. However, there should only be one accurate response to the edge. Thus non-maximum suppression can help to suppress all the gradient values to 0 except the local maximal, which indicates location with the sharpest change of intensity value. The algorithm for each pixel in the image is :

Usually the neighborhood is taken to be 3 x 3 and the values of the magnitude are linearly interpolated between the closest points in the neighborhood. e.g. in Figure 1 the value at C is interpolated between the values at $$A_7$$ and $$A_8$$ and the values at B between those at $$A_3$$ and $$A_4$$.
 * For each point (x,y), where x and y are integers and I(x,y) the intensity of pixel (x, y).
 * Calculate the gradient of image intensity and its magnitude in (x, y).
 * Estimate the magnitude of the gradient along the direction of the gradient in some (2n+1) x (2n+1) neighborhood around (x, y).
 * If (x,y) is not a local maximum of the magnitude of the gradient along the direction of the gradient then it is not an edge point and thus is suppressed (set to 0).

Below is a concrete 3 x 3 example without linear interpolation:

In more accurate implementations, linear interpolation is used between the two neighboring pixels that straddle the gradient direction. For example, if the gradient angle is between 45° and 90°, interpolation between gradients at the north and north east pixels will give one interpolated value, and interpolation between the south and south west pixels will give the other. The gradient magnitude at the central pixel must be greater than both of these for it to be marked as an edge.
 * if the rounded gradient angle is 0° (i.e. the edge is in the north–south direction) the point will be considered to be on the edge if its gradient magnitude is greater than the magnitudes at pixels in the east and west directions,
 * if the rounded gradient angle is 90° (i.e. the edge is in the east–west direction) the point will be considered to be on the edge if its gradient magnitude is greater than the magnitudes at pixels in the north and south directions,
 * if the rounded gradient angle is 135° (i.e. the edge is in the northeast–southwest direction) the point will be considered to be on the edge if its gradient magnitude is greater than the magnitudes at pixels in the north west and south east directions,
 * if the rounded gradient angle is 45° (i.e. the edge is in the north west–south east direction) the point will be considered to be on the edge if its gradient magnitude is greater than the magnitudes at pixels in the north east and south west directions.

Note that the sign of the direction is irrelevant, i.e. north–south is the same as south–north and so on.

Object Detection
In the context of object detection, approaches based on sliding windows typically produce multiple windows with high scores close to the correct location of objects. The goal of non-maximum suppression is therefore to retain only one window per group, corresponding to the precise local maximum of the response function, ideally obtaining only one detection per object.

The most common approach for non-maximum suppression consists of a greedy iterative procedure. The procedure starts by selecting the best scoring window and assuming that it indeed covers an object. Then, the windows that are too close to the selected window are suppressed. Out of the remaining windows, the next top-scoring one is selected, and the procedure is repeated until no more windows remain.

Efficient Non-Maximum Suppression
There are several algorithms of non-maximum suppression ranging from easy-to-implement to highly-efficient. A commonly used efficient non-maximum suppression is (2n+1) x (2n+1) block algorithm described next.

Observe that two local maxima are at least n + 1 pixels in each direction apart. Vice versa, within each block of size (n + 1) × (n + 1) there can be at most one local maximum. Thus, the algorithm partitions the input image into such blocks and searches within each block for the greatest element, which is its only possible local maximum candidate. Then, the full neighborhood of this candidate is tested. Hereby, elements of the block itself can be skipped, because they are by construction already smaller than the candidate. The pseudocode is shown below.


 * for all $$(i, j) \in \{n, 2n+1, ... \} \times \{n, 2n+1, ...\} \cap [0, W - n] \times [0, H - n]$$ do
 * $$(m_i, m_j) \leftarrow (i, j)$$
 * for all $$ (i_2, j_2) \in [i, i+n] \times [j, j+n]$$ do
 * if $$I(i_2, j_2) > I(m_i, m_j)$$ then
 * $$(m_i, m_j) \leftarrow (i_2, j_2)$$
 * for all $$(i_2, j_2) \in [m_i - n, m_i +n] \times [m_j - n, m_j + n] - [i, i+n] \times [j, j+n] $$ do
 * if $$I(i_2, j_2) > I(m_i, m_j)$$ then
 * goto failed
 * MaximumAt(m_i, m_j)
 * failed

Codes
There are several code implementations of non-maximum suppression available online.

cpp

java