c++ 正在将cv::Mat对象转换为dlib::cv_image

b5lpy0ml  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(125)

我尝试了很多我认为是解决办法的东西,但在这里都不起作用。
我的配置:

  • 操作系统:Ubuntu 20.04
  • OpenCv版本:3.4.19
  • Dlib版本:19.6.0

我得到这个错误:

In file included from /usr/local/include/dlib/opencv.h:10,
from /home/zeobora/openCv/c++/facial_recognition/trainModel/trainModel.cpp:3: 
/usr/local/include/dlib/opencv/cv_image.h:
In instantiation of ‘dlib::cv_image<pixel_type>::cv_image(cv::Mat) 
[with pixel_type = dlib::bgr_pixel]’: 
/home/zeobora/openCv/c++/facial_recognition/trainModel/trainModel.cpp:45:75:   
required from here /usr/local/include/dlib/opencv/cv_image.h:37:29: 
error: conversion from ‘const cv::Mat’ to non-scalar type 
‘IplImage’ {aka ‘_IplImage’} requested 37 | IplImage temp = img; ^~~ 
make[2]: *** [CMakeFiles/trainModel.dir
/build.make:63: CMakeFiles/trainModel.dir/trainModel.cpp.o] Error 1 
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/trainModel.dir/all]
Error 2 make: *** [Makefile:130: all] Error 2

下面是我的CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.15)
set(CMAKE_CXX_STANDARD 17)
project( trainModel )
find_package( OpenCV REQUIRED )
include_directories( "/usr/local/include/opencv4/opencv2" )
find_package(dlib REQUIRED)
include_directories("/usr/local/include")
add_executable( trainModel trainModel.cpp )
target_link_libraries( trainModel ${OpenCV_LIBS} dlib::dlib )
install( TARGETS trainModel DESTINATION bin)

这是我的代码片段,这个错误就是从这里来的。

#include <iostream>
#include <opencv2/opencv.hpp>
#include <dlib/opencv.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>

int main(){
    //Some codes here which isn't related...
    dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
    dlib::shape_predictor pose_model;
    dlib::deserialize("/home/openCv/models/shape_predictor_68_face_landmarks.dat") >> pose_model;
    cv::Mat image, grayImage;
    for (int i = 0; i < personNumber; i++){
        image = cv::imread( pictures[i] );
        //My problem begins here.
        dlib::array2d<dlib::bgr_pixel> newImage;
        dlib::assign_image(newImage, dlib::cv_image<dlib::bgr_pixel>(image));
        //My problem ends here.
        std::vector<dlib::rectangle> dets = detector(newImage);
        std::cout << "Number of faces detected: " << dets.size() << std::endl;
        while(1){
cv::imshow("Image", image);
            char key=(cv::waitKey(1)&(0xFF)); 
            if (key=='q'){ 
                break;
            }
        }
    }
    cv::destroyAllWindows();
    return 0;
}

你能帮我转换一下吗?这样我就可以用dlib库了。

zc0qhyus

zc0qhyus1#

我卸载了dlib的旧版本,从dlib's github link安装了最新版本,修复了。但是我不明白19.22为什么比19.7更新。

相关问题