我如何从某个ROI中提取所有像素值,然后将其存储为CSV文件,然后再次使用该csv文件来获取该ROI

问题描述 投票:0回答:1

[After applying mask original image

import cv2
import dlib
import numpy as np
img = cv2.imread("Aayush.jpg")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
msk = np.zeros_like(img_gray)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
faces = detector(img_gray)
for face in faces:
  landmarks = predictor(img_gray, face)
  lp = []
  for n in range(0,68):
    x = landmarks.part(n).x
    y = landmarks.part(n).y
    lp.append((x,y))
    p = np.array(lp, np.int32)
    #cv2.circle(img, (x,y), 3, (0, 0, 255), -1)
convexhull = cv2.convexHull(p)
#cv2.polylines(img, [convexhull], True, (255,0,0), 3)
cv2.fillConvexPoly(msk, convexhull, 255)
img1 = cv2.bitwise_and(img, img, mask = msk)

img1包含一个完整的黑色图像,从img上切下了脸,我只需要脸部的像素值而不是完整的图像

python-3.x opencv image-processing face-detection dlib
1个回答
0
投票
由于问题本身未提供原始图像和遮罩。我假设一个简单的输入图像和一个带有圆形空腔的蒙版图像为:

mask image enter image description here

此处的遮罩是中央腔中值为255的单通道矩阵。要获取腔体内的像素信息,只能使用以下numpy操作:

pixel_info = original_image[mask == 255] # You may need to convert the numpy array to Python list. pixel_info_list = pixel_info.tolist()

现在您可以将list序列化为所需的任何格式(在这种情况下为csv。]

完整代码:

import cv2 import numpy as np original_image = cv2.imread("/path/to/lena.png") mask = np.zeros(original_image.shape[:2], dtype=original_image.dtype) mask = cv2.circle(mask, (256, 256), 100, [255], -1) pixel_info = original_image[mask == 255] pixel_info_list = pixel_info.tolist()

© www.soinside.com 2019 - 2024. All rights reserved.