使用开放式 CV 和 python 提取或获取图像中几个点的平均 RGB 颜色
Extracting or getting mean RGB color of few points in an image using open CV and python
我正在尝试使用媒体管道库提取面部几个点的平均颜色。下面的代码是在脸上打印所需的点。但不确定如何添加这些点中的每一个并获得点的平均 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)
在您的代码中 ptcor = (x,y)
是坐标。
可能你想要 ptcor = rgb_image[y][x]
那是那些坐标的 rgb 颜色
我正在尝试使用媒体管道库提取面部几个点的平均颜色。下面的代码是在脸上打印所需的点。但不确定如何添加这些点中的每一个并获得点的平均 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)
在您的代码中 ptcor = (x,y)
是坐标。
可能你想要 ptcor = rgb_image[y][x]
那是那些坐标的 rgb 颜色