Refines the corner locations.
cv2.cornerSubPix(image, corners, winSize, zeroZone, criteria) → corners
np.array([[0, 5], [2, 6]], dtype=np.float32)
). May also come from another function such as findChessboardCorners
. winSize=(5,5)
, then a \((5 \times 2+1) \times (5 \times 2+1)=11\times 11\) search window is used. Must be positive integers.(-1,-1)
indicates that there is no such size. TermCriteria
): Criteria for termination of the iterative process of corner refinement. That is, the process of corner position refinement stops either after criteria.maxCount
iterations or when the corner position moves by less than criteria.epsilon
on some iteration. See note below.
The function iterates to find the sub-pixel accurate location of corners or radial saddle points, as shown on the figure below. The functions cornerHarris
or findContours
will return integer values for corners, and cornerSubPix
will refine those to more accurate locations (the subpixel location values). Please see this link for complete examples.
The sub-pixel accurate corner locator is based on the observation that every vector from the center \(q\) to a point \(p\) located within a neighborhood of q is orthogonal to the image gradient at p subject to image and measurement noise. Consider the expression:
$$\epsilon_i=DI_{p_i}^T\ast(q-p_i)$$
where \(DI_{p_i}\) is an image gradient at one of the points \(p_i\) in a neighborhood of q. The value of q is to be found so that \(\epsilon_i\) is minimized. A system of equations may be set up with \(\epsilon_i\) set to zero:
$$\sum_i(DI_{p_i}\ \cdot\ \ DI_{p_i}\ ^T)\cdot\ q\ -\ \sum_i(DI_{p_i}\ \ \cdot\ DI_{p_i}\ \ ^T\cdot\ p_i)$$
where the gradients are summed within a neighborhood ("search window") of q . Calling the first gradient term G and the second gradient term b gives:
$$q=\ G^{-1}\ \ \cdot\ b$$
The algorithm sets the center of the neighborhood window at this new center q and then iterates until the center stays within a set threshold.
criteria
of type TermCriteria
contains three pieces of important information:
criteria.type
: The type of termination criteria. One of iterations (cv2.TermCriteria_COUNT
), epsilon (cv2.TERM_CRITERIA_EPS
), or both (cv2.TERM_CRITERIA_EPS + cv2.TermCriteria_COUNT
; will stop when first is met).criteria.maxCount
(criteria.epsilon
(criteria
, even if only using one stopping condition.corners
is both an input and an output. This means that the returned corners
object and the parameter corners
will have the same id. In other words, the original input corners
object is modified, and an object that references it is returned.corners
is used as an input.