Getting the OpenCV library to work with C++ [closed]

asked 2018-03-16 07:37:16 -0600

quina gravatar image

updated 2018-03-16 09:13:00 -0600

Hello everyone,

just a short heads-up: I have never used OpenCV and barely programmed outside of university classes (that means I know all the basic concepts like loops, data types, object orientation etc but I am having a hard time dealing with all the stuff that surrounds the coding like getting libraries to work, setting up frameworks, ...)

I have been trying to get OpenCV to work for hours now, but I just cannot do it. The first thing I did is downloading and extracting the current OCV - 3.4.1. I wanted to get it to work in Code Blocks and after some research I learned that there are 'source' files from which I can build 'binaries' (correct me if I got that wrong) to use in my IDE. And that I need cmake to generate those binaries. So I downloaded it and tinkered around with it for hours - it just wouldn't work. I always got an error message telling me that some DLL (libintl-8?) was missing - even though it was exactly where it needed to be, inside the MinGW folder. Creating an OpenCV-project with Code Block did not work at all for me, I got stuck with some error messages I can't quite remember after linking the OpenCV-folder.

After that I read that I would not need to create the binaries myself if I used Visual C++, so I decided to download it and give it a try. I set my project's propertysheet (I hope that's the english name for it, I'm using it in a different language) "Linker" -> "Additional Library directories" to C:\opencv\build and the "C/C++" -> "Additional Include directories" to C:\opencv\build\include. Lastly, under "Linker" -> "Input" -> "Additional dependencies" I entered "opencv_world341.lib", because the full list (as described here: https://docs.opencv.org/2.4/doc/tutor...) wouldn't work but it seems that this 'world' thing seems to contain all I need.

After typing #include, the IDE automatically suggests the opencv and opencv2 directories, so that seemed to work. However, after trying out the test-code from the link I posted above:

#include "stdafx.h"
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char** argv)
{
    if (argc != 2)
    {
        cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
        return -1;
    }

    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file

    if (!image.data) // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

    namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
    imshow("Display window", image); // Show our image inside it.

    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

and run it, I get the following error:

1>------ Erstellen gestartet: Projekt: OpenCV_Test_Console, Konfiguration: Debug Win32 ------ 1>opencv_world341.lib(opencv_world341.dll) : fatal error LNK1112: Modul-Computertyp "x64" steht in Konflikt mit dem Zielcomputertyp "x86". 1>Die Erstellung des Projekts "OpenCV_Test_Console.vcxproj" ist abgeschlossen -- FEHLER. ========== Erstellen: 0 ... (more)

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by quina
close date 2018-03-22 07:02:21.631952

Comments

if you're using VS now, can we remove the codeblocks story ? it's irrelevant & confusing.

the error says, you built 64 bit libraries, but your project is 32bit. there's a dropbox in the toolbar to change this, look it up

berak gravatar imageberak ( 2018-03-16 08:43:37 -0600 )edit

I just posted it because I thought that maybe there is an easy way to get it to work in Code Blocks and that I'm just too much of a noob to figure it out (especially since there's this OpenCV project-type that just refuses to work).

quina gravatar imagequina ( 2018-03-16 08:45:19 -0600 )edit

After setting this to x64, I get the error

1>------ Erstellen gestartet: Projekt: OpenCV_Test_Console, Konfiguration: Debug x64 ------ 1>stdafx.cpp 1>OpenCV_Test_Console.cpp 1>OpenCV_Test_Console.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl cv::imshow(class cv::String const &,class cv::debug_build_guard::_InputArray const &)" (?imshow@cv@@YAXAEBVString@1@AEBV_InputArray@debug_build_guard@1@@Z)" in Funktion "main". 1>C:.......+\Projekte\OpenCV_Test_Console\x64\Debug\OpenCV_Test_Console.exe : fatal error LNK1120: 1 nicht aufgelöste Externe 1>Die Erstellung des Projekts "OpenCV_Test_Console.vcxproj" ist abgeschlossen -- FEHLER. ========== Erstellen: 0 erfolgreich, 1 fehlerhaft, 0 aktuell, 0 übersprungen ==========

quina gravatar imagequina ( 2018-03-16 08:51:50 -0600 )edit

you have to add opencv_world341.lib to the linker library list, not the dll

berak gravatar imageberak ( 2018-03-16 09:12:11 -0600 )edit

I thought that's what I did: https://pasteboard.co/HcaV15x.png

For some reason though, I get another error now: it simply says it cannot open the file opencv_world341.lib:

1>LINK : fatal error LNK1104: Datei "opencv_world341.lib" kann nicht geöffnet werden.

So I tried to link the exact directory the lib is located in the Linker -> Additional Library Directories (speaking of that, what does "vc14" and "vc15" mean and what's the difference?). But then I'll get yet another error:

1>OpenCV_Test_Console.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl cv::imshow(class cv::String const &,class cv::debug_build_guard::_InputArray const &)" (?imshow@cv@@YAXAEBVString@1@AEBV_InputArray@debug_build_guard@1@@Z)" in Funktion "main". [...]

quina gravatar imagequina ( 2018-03-16 09:32:30 -0600 )edit

i know, it's painful to be a noob.

opencv_world341d.lib for the debug version

berak gravatar imageberak ( 2018-03-16 09:36:54 -0600 )edit

hi berak,

thanks a lot for you continued support. sorry i couldn't answser earlier, i was out of town over the weekend. after specifying the debug-dll in den linker, the program seems to compile, but i now get yet another error (which is a runtime error this time, i suppose? https://pasteboard.co/HcC3sLb.png

the error says that the code execution cannot be continued because opencv_world341d.dll could not be found. this is so strange since it is another missing dll error, already got one very similar sounding in cmake. and the dll IS in the folder, i just double checked it.

https://stackoverflow.com/questions/3... here it says i should copy the dll into my VS\x64\release directory. doesnt work for me tho

quina gravatar imagequina ( 2018-03-19 06:32:48 -0600 )edit

yea, you hit every pothole there is.

the folder , where the dll is, has to go on the PATH env variable. do NOT try to copy dlls around, that's moronic. (you'll have more than 1 opencv program, and you wold have to do that every time, for both release and debug)

berak gravatar imageberak ( 2018-03-19 06:43:10 -0600 )edit

hi, i already tried to add it to my env variables (since cmake threw the same error, i'm already a little used to that) - didnt work for me either, though. i am not sure however, if i did this correctly. is it really just adding the folder to the path like so? https://pasteboard.co/HcCbi1A.png i did the same in the system variables - still does not work. might a reboot be necessary? takes my computer forever, that's why i haven't already tried it :)

quina gravatar imagequina ( 2018-03-19 06:49:29 -0600 )edit

you'll need to restart the console(and VS, too), so it starts with the changed environment

type PATH in a cmd window, to see, what you have

berak gravatar imageberak ( 2018-03-19 07:04:38 -0600 )edit