How to save in a variable a portion of ndarry image?

41 views Asked by At

I'm facing problem in saving in a variable a portion of an image that is described in the following way:

def extract_face_region(image, landmarks):
    landmark_coords = np.array([(landmarks.part(i).x, landmarks.part(i).y) for i in [2, 14, 40, 41, 46, 47, 50, 52]])

    XLT = landmarks.part(14).x
    YLT = max(landmarks.part(40).y, landmarks.part(41).y, landmarks.part(46).y, landmarks.part(47).y)
    Wrect = landmarks.part(2).x - landmarks.part(14).x
    Hrect = min(landmarks.part(50).y, landmarks.part(52).y) - YLT

    if 0 <= XLT < image.shape[1] and 0 <= YLT < image.shape[0] and 0 <= XLT + Wrect <= image.shape[1] and 0 <= YLT + Hrect <= image.shape[0]:
        face_region = image[YLT:YLT+Hrect, XLT:XLT+Wrect, :]
    else:
        print("Region outside image limits.")
 
    cv2.rectangle(image, (int(XLT), int(YLT)), (int(XLT + Wrect), int(YLT + Hrect)), (255, 0, 0), 2)
    #cv2_imshow(image)
    cv2_imshow(face_region)  
    return face_region

image is a numpy.ndarray (640,480,3)

cv2.rectangle() works since the image with the rectangle is showed. I want that face region contains only the region tracked by the rectangle. The rest of image must be discarded. Any suggustion on how to do it?

Thanks.

0

There are 0 answers