我无法保存由DLIB人脸检测模型生成的图像芯片。以下代码详细介绍了我的工作流我试图保存下面的整个图像,d_image
,这工作得很好。但是,当我尝试保存每个芯片时,我会得到失真的输出(请参见下面的示例)。我在Ubuntu 16.04上使用dlib 19.4。如何正确保存DLIB物体检测芯片?
// object to store raw image data
cv::Mat rawImage;
// initialize the detector
dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
// using shape predictor object to create dull_object_detections
dlib::shape_predictor sp;
dlib::deserialize(argv[1]) >> sp;
// for writing out images
int image_id = 1;
while (true){
// retrieve image size
sockt.getData(&image_size, 4, NULL);
if (image_size > 0) {
rawImage.create(1, image_size, CV_8UC1);
// load incoming data from a stream
sockt.getData(rawImage.data, image_size, MSG_WAITALL);
// reshape and correct orientation
dlib::cv_image<dlib::bgr_pixel> d_image = utils::process_frame(rawImage);
// find the daces!
std::vector<dlib::rectangle> detections = detector(d_image);
if (detections.size() > 0){
// generate additional detection data so we can use
// dlib's extract_image_chips function
std::vector<dlib::full_object_detection> shapes;
for (int idx = 0; idx < detections.size(); idx++){
dlib::full_object_detection shape = sp(d_image, detections[idx]);
shapes.push_back(shape);
}
// write each chip to disk
dlib::array<dlib::array2d<dlib::bgr_pixel>> face_chips;
dlib::extract_image_chips(d_image, dlib::get_face_chip_details(shapes), face_chips);
for (int idx = 0; idx < face_chips.size(); idx++){
std::string fname = argv[2] + std::to_string(image_id) + ".jpg";
dlib::save_jpeg(face_chips[idx], fname);
image_id++;
}
}
实例保存芯片:
编辑:添加注释utils::process_frame
。此函数接受的1×N阵列和使用的OpenCV
谢谢为了您的评论,但这不是问题。函数'process_frame'使用OpenCV函数将1xN数组解码为JPEG格式。我也可以将'd_image'成功保存为JPEG格式。我也尝试使用':: toMat'进行转换,但保存时仍然获得相同的输出。 – Andrew
@Andrew,看起来像现在我看到了真正的问题,我已经更新了我的回答 – Evgeniy
那就是诀窍!我应该在阅读后排队阅读'dlib :: cv_image'不会复制数据。 – Andrew