3

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.

3 Answers 3

3

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]
1

You can use openCV library

All of these steps is supported by openCV.

1
  • Thanks a lot but it does not work, I'm getting this error: error: (-215) (mask.type() == CV_8UC1 || mask.type() == CV_8SC1) in function binary_op And my code is: mask = np.zeros(color_img.shape, np.uint8) cv2.circle(mask,(key[0][0],key[0][1]),radio_medio,255,thickness=-1) masked_data = cv2.bitwise_and(color_img, color_img, mask=mask) mean = cv2.mean(color_img, mask=masked_data) print(mean)
    – Jota
    Commented Mar 29, 2017 at 15:08
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.