
我不知道如何提取被綠線包圍的不規則區域。即,一張臉的左臉頰和右臉頰。
from collections import OrderedDict
import numpy as np
import cv2
import dlib
import imutils
CHEEK_IDXS = OrderedDict([("left_cheek", (1, 2, 3, 4, 5, 48, 31)),
("right_cheek", (11, 12, 13, 14, 15, 35, 54))
])
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
img = cv2.imread('Tom_Cruise.jpg')
img = imutils.resize(img, width=600)
overlay = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detections = detector(gray, 0)
for k, d in enumerate(detections):
shape = predictor(gray, d)
for (_, name) in enumerate(CHEEK_IDXS.keys()):
pts = np.zeros((len(CHEEK_IDXS[name]), 2), np.int32)
for i, j in enumerate(CHEEK_IDXS[name]):
pts[i] = [shape.part(j).x, shape.part(j).y]
pts = pts.reshape((-1, 1, 2))
cv2.polylines(overlay, [pts], True, (0, 255, 0), thickness=2)
cv2.imshow("Image", overlay)
cv2.waitKey(0)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
我知道如果只是簡單地從臉上提取一個矩形區域作為臉頰,代碼可以是這樣的
ROI1 = img[shape[29][1]:shape[33][1], shape[54][0]:shape[12][0]] #right cheeks
ROI1 = img[shape[29][1]:shape[33][1], shape[4][0]:shape[48][0]] #left cheek

但是我想提取不規則區域進行后續處理,我該怎么做?
uj5u.com熱心網友回復:
您可以通過兩個簡單的步驟來完成此操作:
- 使用您擁有的點坐標創建蒙版
- 執行 bitwise_and 操作(裁剪)
代碼:
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
output = cv2.bitwise_and(img, img, mask=mask)
輸出:

此外,如果您想專注于裁剪的多邊形,您可以為多邊形創建一個邊界矩形,然后像 tihs 一樣從輸出幀裁剪:
# Create a bounding rects list at global level
bounding_rects = []
# Calculate Bounding Rects for each pts array inside the for loop
bounding_rects.append(cv2.boundingRect(pts))
# Assign geometrical values to variables to crop (Use a range(len(bounding_boxes)) for loop here)
enter code here
x1,y1,w1,h1 = bounding_rects[0]
x2,y2,w2,h2, = bounding_rects[1]
# At the end of the program, crop the bounding boxes from output
cropped1= output[y1:y1 h1, x1:x1 w1]
cropped2= output[y2:y2 h2, x2:x2 w2]
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453513.html
