使用开放CV和python提取或获取图像中几个点的平均RGB颜色
Posted
技术标签:
【中文标题】使用开放CV和python提取或获取图像中几个点的平均RGB颜色【英文标题】:Extracting or getting mean RGB color of few points in an image using open CV and python 【发布时间】:2022-01-09 03:18:00 【问题描述】:我正在尝试使用媒体管道库提取面部几个点的平均颜色。下面的代码是在脸上打印需要的点。但不确定如何添加这些点并获得这些点的平均 RGB 值
import cv2
import mediapipe as mp
from mediapipe.python.solutions import face_mesh
import numpy as np
#init main point array
color_array = []
#to access facemesh lib from mediapipe to identify facial landmarks
mpFaceMesh = mp.solutions.face_mesh
face_mesh = mpFaceMesh.FaceMesh()
#getting image
image = cv2.imread("E:\BeautBeta\IP_application/test2.jpg") #test: cannot add with multiple faces
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
height, width, _ = image.shape
#process image
result = face_mesh.process(rgb_image)
for facialLandmarks in result.multi_face_landmarks:
#specific landmarks are detected through the mediapipe library are considered here because not all landmarks are valid
landmarks_array = [3, 6, 8, 9, 32, 36, 69, 67, 109, 108, 101, 116, 117, 135, 16, 148, 151, 172, 169, 171, 152, 165, 170, 175, 199, 194, 192, 187, 200, 211, 204, 202, 207, 206, 210, 216, 228, 229, 266, 262, 277, 275, 273, 280, 281, 297, 299, 298, 330, 323, 343, 338, 337, 333, 343, 349, 348, 347, 350, 371, 357, 351, 396, 377, 391, 393, 423, 411, 420, 419, 421, 418, 422, 424, 429, 437, 425, 426, 432, 427, 436, 434, 430, 431, 428, 449, 448, 450, 451, 452]
for i in landmarks_array:
pt =facialLandmarks.landmark[i]
x = int(pt.x * width)
y = int(pt.y * height)
ptcor = (x,y)
cv2.circle(image, (x,y), 3, (255,255,255), -1)
color_array.append(ptcor)
mean_cor = np.mean(color_array, axis=0)
print(mean_cor)
cv2.imshow("Image", image)
cv2.waitKey(0)
【问题讨论】:
正如您所做的那样,您计算坐标 x 的平均值。也许您想计算这些坐标中颜色的平均值? 是的,没错@MarioAbbruscato 我想计算这些坐标的平均颜色。 【参考方案1】:在您的代码中,ptcor = (x,y)
是坐标。
你可能想要ptcor = rgb_image[y][x]
,即那些坐标的rgb颜色
【讨论】:
以上是关于使用开放CV和python提取或获取图像中几个点的平均RGB颜色的主要内容,如果未能解决你的问题,请参考以下文章