Some of the previous functions (the ones with the shift parameter) can work with sub-pixel accuracy in connection with the pixel coordinates. To cope with this, you should pass the coordinates as fixed-point numbers, which are encoded as integers.
Therefore, the shift parameter allows you to specify the number of the fractional bits (on the right of the decimal point). In the end, the real point coordinates are calculated as follows:
For example, this piece of code draws two circles with a radius of 300. One of them uses a value of shift = 2 to provide sub-pixel accuracy. In this case, you should multiply both the origin and the radius by a factor of 4 (2 shift= 2):
shift = 2
factor = 2 ** shift
print("factor: '{}'".format(factor))
cv2.circle(image, (int(round(299.99 * factor)), int(round(299.99 * factor))), 300 * factor, colors['red'], 1, shift=shift)
cv2.circle(image, (299, 299), 300, colors['green'], 1)
If shift = 3, the value for the factor will be 8 (2shift = 3), and so on. Multiplying by powers of 2 is the same as shifting the bits corresponding to the integer binary representations to the left by one. This way you can draw float coordinates. To summarize this point, we can also create a wrapper function for cv2.circle(), which can cope with float coordinates—draw_float_circle()—using the shift argument property. The key code for this example is shown next. The full code is defined in the shift_parameter.py script:
def draw_float_circle(img, center, radius, color, thickness=1, lineType=8, shift=4):
"""Wrapper function to draw float-coordinate circles
"""
factor = 2 ** shift
center = (int(round(center[0] * factor)), int(round(center[1] * factor)))
radius = int(round(radius * factor))
cv2.circle(img, center, radius, color, thickness, lineType, shift)
draw_float_circle(image, (299, 299), 300, colors['red'], 1, 8, 0)
draw_float_circle(image, (299.9, 299.9), 300, colors['green'], 1, 8, 1)
draw_float_circle(image, (299.99, 299.99), 300, colors['blue'], 1, 8, 2)
draw_float_circle(image, (299.999, 299.999), 300, colors['yellow'], 1, 8, 3)