【发布时间】:2017-08-22 12:57:34
【问题描述】:
我在 Python 中使用 OpenCV 和 PIL。我检测到 96 个圆圈,它们的 center 坐标 和 radio。我需要每个圆圈的平均 RGB。
每个圆圈有6000像素,所以我认为一对一迭代效率不高。
如何从每个圆圈中提取平均 RGB? 如果适合我的用例,我准备使用任何其他库。
【问题讨论】:
标签: python opencv python-imaging-library
我在 Python 中使用 OpenCV 和 PIL。我检测到 96 个圆圈,它们的 center 坐标 和 radio。我需要每个圆圈的平均 RGB。
每个圆圈有6000像素,所以我认为一对一迭代效率不高。
如何从每个圆圈中提取平均 RGB? 如果适合我的用例,我准备使用任何其他库。
【问题讨论】:
标签: python opencv python-imaging-library
我终于明白了,这就是解决方案:
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]
【讨论】:
你可以使用openCV库
openCV 支持所有这些步骤。
【讨论】:
也许太具体了,但如果您使用的是关键点(顺便说一下,这只是@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)
把它留在这里,以防其他人想要这个功能。
【讨论】: