圈子的RGB平均值

时间:2017-03-29 07:12:05

标签: python opencv python-imaging-library

我在Python中使用OpenCV和PIL。我检测到96个圆圈的中心 坐标无线电。我需要每个圆圈的平均RGB。

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

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

3 个答案:

答案 0 :(得分:1)

您可以使用openCV库

openCV支持所有这些步骤。

答案 1 :(得分:1)

最后我明白了,这就是解决方案:

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]

答案 2 :(得分:0)

也许太具体了,但是如果您使用的是关键点(顺便说一句,这只是@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 https://stackoverflow.com/a/43170927/2594947
        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)

只要其他人想要此功能,就把它留在这里。

相关问题