Problem about the pixels

I just drew two red circles with matplotlib,the first one’s radius is 0.25,the second is 0.5.There is no doubt that the ratio of the area of ​​the former to the latter is 0.25.I have used the CV2 to count the red pixels of each circle in HSV(converted from RGB),However,the pixels of fomer is 8753,the latter is 29718,which means the ratio of the area is about 0.2945. Why this happened,and how do I solve this problem. Here is my code,sorry if my text is formatted incorrectly,I’m a fresh guy here.

from matplotlib.patches import Circle
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import cv2
import numpy as np
fig = plt.figure();
ax = fig.add_subplot(111)
cir2 = Circle(xy=(0, 0), radius=0.25, color='red', alpha=1.0)
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
img = cv2.imread(img_name)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#Gen lower mask (0-5) and upper mask (175-180) of RED
mask1 = cv2.inRange(img_hsv, (0,43,46), (10,255,255))
mask2 = cv2.inRange(img_hsv, (156,43,46), (180,255,255))
# Merge the mask and crop the red regions
mask = cv2.bitwise_or(mask1, mask2 )
output1 = cv2.bitwise_and(img, img, mask=mask)
# cv2.imshow("mask", mask)
# cv2.imshow("croped1", output1)
# cv2.waitKey()
tot_pixel = output1.size
red_pixel = np.count_nonzero(output1)
print("Red pixels: " + str(red_pixel))

Try setting Circle(..., lw=0, ...).

The edge of patches is stroked with a finite width path centered on the edge with effectively makes the area of the circles bigger. Because the width of the path is in absolute units (points) rather than data units it will add more relative area to the smaller circle and hence make the ratio of the sizes off.

It is helpful,thanks!! :smiley: