crop and Save ROI as new image in OpenCV 2.4.2 using cv::Mat

后端 未结 2 1309
后悔当初
后悔当初 2021-01-31 23:44

Working on Face Detection and Recognition, and after successfully detecting a face, I just want to crop the face and save it somewhere in the drive to give it for the recognitio

2条回答
  •  自闭症患者
    2021-02-01 00:05

    Using cv::Mat objects will make your code substantially simpler. Assuming the detected face lies in a rectangle called faceRect of type cv::Rect, all you have to type to get a cropped version is:

    cv::Mat originalImage;
    cv::Rect faceRect;
    cv::Mat croppedFaceImage;
    
    croppedFaceImage = originalImage(faceRect).clone();
    

    Or alternatively:

    originalImage(faceRect).copyTo(croppedImage);
    

    This creates a temporary cv::Matobject (without copying the data) from the rectangle that you provide. Then, the real data is copied to your new object via the clone or copy method.

提交回复
热议问题