圆圈的 RGB 平均值

RGB average of circles

我在 Python 中使用 OpenCV 和 PIL。我用 center coordinatesradios 检测到 96 个圆。我需要每个圆圈的平均 RGB。

每个圆有6000个像素,所以我认为一对一迭代效率不高。

如何从每个圆中提取平均 RGB? 如果适合我的用例,我准备使用任何其他库。

您可以使用 openCV 库

openCV 支持所有这些步骤。

终于明白了,这就是解决方案:

circle_img = np.zeros((color_img.shape[0],color_img.shape[1]), np.uint8) #Creamos mascara (matriz de ceros) del tamano de la imagen original
cv2.circle(circle_img,(x_center,y_center),radio,(255,255,255),-1) #Pintamos los circulos en la mascara
datos_rgb = cv2.mean(color_img, mask=circle_img)[::-1]

可能太具体了,但是如果你使用关键点(顺便说一下,这只是@Jota 回答的“漂亮”版本):

def average_keypoint_value(canvas,keypoints):
    average_value = []
    if canvas.ndim == 2:
        nchannels = 1
    elif canvas.ndim > 2:
        nchannels = canvas.shape[-1]
    for keypoint in keypoints:
        circle_x =      int(keypoint.pt[0])
        circle_y =      int(keypoint.pt[1])
        circle_radius=  int(keypoint.size/2)
        #copypasta from 
        circle_img = np.zeros((canvas.shape[:2]), np.uint8)
        cv2.circle(circle_img,(circle_x,circle_y),circle_radius,(255,255,255),-1)
        datos_rgb = cv2.mean(canvas, mask=circle_img)
        average_value.append(datos_rgb[:nchannels])
    return(average_value)

就把它留在这里,以防其他人需要这个功能。