0

I'm working with Opencv, I tried to run the following code:

 #include "opencv2/objdetect/objdetect.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"

    #include <iostream>
    #include <stdio.h>

    using namespace std;
    using namespace cv;

    /** Function Headers */
    void detectAndDisplay(Mat frame);

    /** Global variables */
    String face_cascade_name = "haarcascade_frontalface_alt.xml";
    String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
    CascadeClassifier face_cascade;
    CascadeClassifier eyes_cascade;
    string window_name = "Capture - Face detection";
    RNG rng(12345);

    /** @function main */
    int main(int argc, const char** argv)
    {
        CvCapture* capture;
        Mat frame;

        //-- 1. Load the cascades
        if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading\n"); return -1; };
        if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading\n"); return -1; };

        //-- 2. Read the video stream
        capture = cvCaptureFromCAM(-1);
        if (capture)
        {
            while (true)
            {
                frame = cvQueryFrame(capture);

                //-- 3. Apply the classifier to the frame
                if (!frame.empty())
                {
                    detectAndDisplay(frame);
                }
                else
                {
                    printf(" --(!) No captured frame -- Break!"); break;
                }

                int c = waitKey(10);
                if ((char)c == 'c') { break; }
            }
        }
        return 0;
    }

    /** @function detectAndDisplay */
    void detectAndDisplay(Mat frame)
    {
        std::vector<Rect> faces;
        Mat frame_gray;

        cvtColor(frame, frame_gray, CV_BGR2GRAY);
        equalizeHist(frame_gray, frame_gray);

        //-- Detect faces
        face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));

        for (size_t i = 0; i < faces.size(); i++)
        {
            Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
            ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);

            Mat faceROI = frame_gray(faces[i]);
            std::vector<Rect> eyes;

            //-- In each face, detect eyes
            eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));

            for (size_t j = 0; j < eyes.size(); j++)
            {
                Point center(faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5);
                int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
                circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);
            }
        }
        //-- Show what you got
        imshow(window_name, frame);
    }

I added all the necessary libraries to the link dependencies as described in this thread, although I still get the message:

  1>------ Build started: Project: openvc_program, Configuration: Debug x64 ------
1>  main.cpp
1>c:\users\dan\desktop\opencvmy\openvc_program\openvc_program\main.cpp(71): warning C4244: 'argument': conversion from 'double' to 'int', possible loss of data
1>c:\users\dan\desktop\opencvmy\openvc_program\openvc_program\main.cpp(72): warning C4244: 'argument': conversion from 'double' to 'int', possible loss of data
1>c:\users\dan\desktop\opencvmy\openvc_program\openvc_program\main.cpp(82): warning C4244: 'argument': conversion from 'double' to 'int', possible loss of data
1>main.obj : error LNK2019: unresolved external symbol "public: __cdecl cv::CascadeClassifier::CascadeClassifier(void)" (??0CascadeClassifier@cv@@QEAA@XZ) referenced in function "void __cdecl `dynamic initializer for 'eyes_cascade''(void)" (??__Eeyes_cascade@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl cv::CascadeClassifier::~CascadeClassifier(void)" (??1CascadeClassifier@cv@@UEAA@XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'eyes_cascade''(void)" (??__Feyes_cascade@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol "public: bool __cdecl cv::CascadeClassifier::load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?load@CascadeClassifier@cv@@QEAA_NAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __cdecl cv::CascadeClassifier::detectMultiScale(class cv::Mat const &,class std::vector<class cv::Rect_<int>,class std::allocator<class cv::Rect_<int> > > &,double,int,int,class cv::Size_<int>,class cv::Size_<int>)" (?detectMultiScale@CascadeClassifier@cv@@UEAAXAEBVMat@2@AEAV?$vector@V?$Rect_@H@cv@@V?$allocator@V?$Rect_@H@cv@@@std@@@std@@NHHV?$Size_@H@2@2@Z) referenced in function "void __cdecl detectAndDisplay(class cv::Mat)" (?detectAndDisplay@@YAXVMat@cv@@@Z)
1>C:\Users\Dan\Desktop\opencvMy\openvc_program\x64\Debug\openvc_program.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What should I do to fix this problem?

Community
  • 1
  • 1
Danaro
  • 61
  • 1
  • 2
  • 11

1 Answers1

0

It seems you are building against x86 library, however, your settings are set to x64, try to switch the Configuration to x86 (and set accordingly to your requirements - correct lib paths in Linker->General->Additional Library Directories and lib names Linker->Input->Additional Dependencies).

EDIT:

To sume the settings of the project for VS 2015:

Extract downloaded OpenCV https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.13/opencv-2.4.13.exe/download and copy the folders to your projects as follows:

C:\OpenCV\opencv\build\include -> D:\SRC\sandbox\OpenCV\include C:\OpenCV\opencv\build\x86\vc12\lib -> D:\SRC\sandbox\OpenCV\lib\Win32\v120

and set VS project Properties to Debug - Win32 Configuration (VS 2015 installed together with VS 2013 as the build of OpenCV is for latest vc120 only, unfortunately not vc140) and edit

General -> Platform Toolset -> Visual Studio 2013 (v120)

C/C++ -> Additional Include Directories -> $(ProjectDir)OpenCV\include

Linker -> General -> Additional Library Directories -> $(ProjectDir)OpenCV\lib\Win32\$(PlatformToolset)$(PlatformToolset)

Linker -> Input -> Additional Dependencies ->

opencv_calib3d2413d.lib
opencv_contrib2413d.lib
opencv_core2413d.lib
opencv_features2d2413d.lib
opencv_flann2413d.lib
opencv_gpu2413d.lib
opencv_highgui2413d.lib
opencv_imgproc2413d.lib
opencv_legacy2413d.lib
opencv_ml2413d.lib
opencv_nonfree2413d.lib
opencv_objdetect2413d.lib
opencv_ocl2413d.lib
opencv_photo2413d.lib
opencv_stitching2413d.lib
opencv_superres2413d.lib
opencv_ts2413d.lib
opencv_video2413d.lib
opencv_videostab2413d.lib

This must work :-)

For Release, you do the same just change the Additional Dependencies to "opencv_*.lib" - see the stripped d in the name of lib.

Dom
  • 532
  • 1
  • 9
  • 23
  • Thank you for helping! how do you know the library is x86? – Danaro Jun 18 '16 at 15:36
  • There is no x64 lib available until you build it on your own ;-) Did it help? VS should warn you: fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64', but you have different error... – Dom Jun 18 '16 at 15:41
  • So why is there x64 directory in the Opencv 2.4.13? – Danaro Jun 18 '16 at 16:02
  • I changed all the things you told me, Dom. Now I get the message: 1>------ Build started: Project: openvc_program, Configuration: Debug Win32 ------ 1> main.cpp 1>C:\opencv\build\x64\vc12\lib\haarcascade_frontalface_alt.xml : fatal error LNK1107: invalid or corrupt file: cannot read at 0xA5365 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== What should I do? – Danaro Jun 18 '16 at 16:17
  • Hmm... you are right, it is new for me. Anyway, you should link against the right version and use proper settings of compiler. What version of VS are you using? You should use Platform Toolset 120, as there is not build of 140. Still weird, "Configuration: Debug Win32" but "C:\opencv\build\x64\vc12", check all the settings again ;-) – Dom Jun 18 '16 at 16:20
  • This question is out of scope... (from the src, you should set the path of XML or copy to path, where you execute the program) – Dom Jun 18 '16 at 17:53
  • I think this is the source of the problem 1>------ Build started: Project: openvc_program, Configuration: Debug Win32 ------ 1>LINK : fatal error LNK1104: cannot open file 'haarcascade_frontalface_alt.xml' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== – Danaro Jun 19 '16 at 05:55
  • Can you explain me in more details how to add the .xml files to my project? – Danaro Jun 19 '16 at 06:36