Jota
Jota

Reputation: 737

RGB average of circles

I'm using OpenCV and PIL in Python. I have 96 circles detected with their center coordinates and radios. I need the average RGB from each circles.

Each circle has 6000 pixels, so I thinks iterate one to one it is not efficient.

How can I extract the average RGB from each circle? I am ready to use any other library if it suits my use-case.

Upvotes: 3

Views: 4013

Answers (3)

user27221
user27221

Reputation: 344

Maybe too specific, but if you are using keypoints (by the way, this is just a "pretty" version from @Jota's answer):

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)

Just leaving it here in case someone else wants a function for this.

Upvotes: 0

Jota
Jota

Reputation: 737

Finally I get it, this is the solution:

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]

Upvotes: 3

Václav Struhár
Václav Struhár

Reputation: 1769

You can use openCV library

All of these steps is supported by openCV.

Upvotes: 1

Related Questions