Amogh
Amogh

Reputation: 4573

cv::imread() fails in c++ project when we mix opencv and dlib

I have a c++ project created in visual studio 2019. I compiled and build openCV version 4.2.0 and dlib version 19.19.0 and used in my c++ project. My motto using opencv and dlib in single project is I want to do face detection using opencv's DNN (caffe) and dlib's hog + svm based face detector (get_frontal_face_detector()).

My both functions are separate, i.e detectFaceByOpenCVDNN() for opencv based face detection and detectFaceBydlibHOG() for dlib based HOG + SVM based detector.

I added include directory of both projects, lib directories (additional library directories) and mentioned additional dependencies with .lib files.

Build of this project is successful and generates .lib file. By using this file another c++ console application calls for detectFaceByOpenCVDNN() (opencv face detector).

Code of detectFaceByOpenCVDNN():

#include <opencv2/imgcodecs.hpp>
#include <opencv2/dnn/dnn.hpp>

#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>

using namespace dlib;
using namespace std;
using namespace cv::dnn;

void namespace_name::FaceDetection::detectFaceByOpenCVDNN(std::string filename)
{    

Net net;        
cv::Mat frame = cv::imread(filename);

if (frame.empty())
    throw std::exception("provided image file is not found or unable to open.");

int frameHeight = frame.rows;
int frameWidth = frame.cols;
    .... // code continues
}

When I this function, I receive exception as "provided image file is not found or unable to open." which is throw if frame.empty() return true. No other exception is displayed on console.

While just digging what makes this fail, I got to know that, If I remove dlib19.19.0_debug_32bit_msvc1924.lib entry from Properties->Linker->Input->Additional Dependencies then frame.empty() returns false and program continues.

But I still not get, why cv::imread() fails when I use opencv and dlib in single c++ project?

Upvotes: 2

Views: 494

Answers (1)

Davis King
Davis King

Reputation: 4791

Did you build dlib with libjpeg and libpng build statically into it? It’s probably conflicting with a similar copy of those libraries statically build into your opencv .lib file. Just rebuild dlib without that if you aren’t using those libraries.

Or link both opencv and dlib to the same libjpeg and libpng libraries.

Dlib's cmake files will try to link with libjpeg and libpng, and if they can't find a system copy will build and statically link the copy in dlib/external. You can control if cmake will attempt to link to these things by setting DLIB_JPEG_SUPPORT and DLIB_PNG_SUPPORT

Upvotes: 2

Related Questions