OpenCV: undefined reference to cv::imwrite - c++

Opencv cannot link cv::imwrite during linking. I am linking imgcodecs but still getting link error for imwrite. Interestingly no error for cv::imread only for cv::imwrite.
Opencv Version: 3.3.1
Compiler: gcc 4.4.7
Link line: -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -llibjasper -lIlmImf -llibjpeg -llibpng -llibtiff -llibwebp -lzlib -la_baslt -lbsc -lbsi -lbigparmcm -lparmsdata -lses -lbregacclib -lbregdb -lbbcacheapi -lbregutil -lcomdb2api -ldbutil -lpeutil -lparmsbase -le_ipc -lbbmsgbufs -lbbipc -lsysutil -lunwind -lz

As far as I know imwrite is a part of highgui module (opencv 2.4) and imgcodecs for opencv 3.x.x. But its important what compiler and opencv version you are using. For example for mingw you need -lopencv_imgcodecs320 (for opencv 3.2.0)

Related

Undefined reference to symbol 'gzclose'

I guess this is a linker error, but I've spent a lot of time and haven't find a working answer. I have an OpenCV C++ program, when I try to compile something of the style cv::Mat newMatObject; it gives me the error /usr/local/lib/libopencv_core.a(persistence.cpp.o): undefined reference to symbol 'gzclose'.
The .pro file contains the following lines:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtTest
TEMPLATE = app
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lz -ltbb -ltiff
SOURCES += main.cpp\
qttest.cpp
HEADERS += qttest.h
FORMS += qttest.ui
Am I forgetting something/doing something wrong?
Some Info:
Kubuntu 16.04 LTS 64 Bits
Qt Creator 4.0.2, based on Qt 5.7.0
OpenCV 2.4.13
Addendum:
I had the program working properly with OpenCV 2.4.9, no other changes. I decided to move on to 2.4.13 because of the extended gui, which didn't work on 2.4.9. I downloaded OpenCV 2.4.13 from the official website, and installed it with this script (except for the wget and unzip, which were made manually), initially didn't autoremove the previous library, but I did after the issue came up and rerun the previous script again, to ensure proper installation.
Edit 1:
Running
g++ -o opencvtest opencvtest.cpp `pkg-config opencv --cflags --libs`
with similar lines of code provides the desired result without a problem.
Add -lz to your LIBS:
LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lz

OpenCV undefined reference to `vtable for cv::HOGDescriptor'

I'm trying to compile my own object detector by using OpenCV 3 hog and svm over Eclipse CDT but when compiling the console returns the following:
Building target: HogDetection
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "HogDetection" ./main.o ./phog.o ./psvm.o -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_ml
./phog.o: In function `cv::HOGDescriptor::HOGDescriptor()':
/usr/local/include/opencv2/objdetect.hpp:348: undefined reference to `vtable for cv::HOGDescriptor'
./phog.o: In function `cv::HOGDescriptor::~HOGDescriptor()':
/usr/local/include/opencv2/objdetect.hpp:372: undefined reference to `vtable for cv::HOGDescriptor'
collect2: error: ld returned 1 exit status
make: *** [HogDetection] Error 1
Probably I'm doing something wrong with the libraries but since I'm new both to c++ and OpenCV I followed a tutorial to set up eclipse CDT with openCV and I was able to run other simple tutorials (those on openCV websites like display image and so on...).
I also read something on wikipedia about vtable and here but it was not sufficient for me to understand what's happening here.
Thanks in advance.
I solved the problem by adding to the gcc c++ linker libraries the library opencv_objdetect
Well if you are working with make files then just add
LIBS += -L/usr/local/libs \
-lopencv_objdetect \
or if you are not having makefile then just run code like
g++ -Wall main.cpp /usr/lib/libopencv_objdetect.a -o main

Opencv + Boost: Undefined references

I have a computer vision source code that is related to find copy and move portions in a forged image. I cannot compile this source code that uses openCV and Boost, some of the errors include undefined references to OpenCv stuff, for example:.
main.cpp:(.text+0x3cc1): undefined reference to `cvSetZero'
My makefile is the following. I think I correctly indicate the boost and opencv libraries. What could be wrong?
# Boost library is required
BOOST_LIBS=-L/usr/include/boost -lboost_system -lboost_random
BOOST_INCLUDE=-I/usr/include/boost
BOOST_VERSION=103500
OPENCV_INCLUDE=-I/usr/include/opencv2/
OPENCV_LIBS=-L/usr/include/opencv2/ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_flann -lopencv_calib3d
OPENCV_LIBS=-L/usr/lib/opencv2/ -lopencv_core ...
^^^
You might want to check if OpenCV is installed correctly and where this installation is located. Afterwards you can adjust your makefile. How to do this depends on your OS.
E.g. on Linux you might want to search for the opencv_core.so (e.g. via locate -b opencv_core).
In my case it would be
OPENCV_LIBS=-L/usr/lib/ -lopencv_core
if you have pkg-config installed, and if it knows where OpenCV is, you can try
OPENCV_INCLUDE=$$(pkg-config --cflags opencv)
OPENCV_LIBS=$$(pkg-config --libs opencv)

Compile issues with OpenCV and Juce on Xcode4.1 in 32bit on Lion

I'm trying to compile my VST Plugin with Xcode4.1 in 32Bit. I'm using the Juce Framework and integrated OpenCV 2.2 in it. I installed OpenCV with homebrew and this command:
sudo brew install opencv -build32
But when I'm compiling the project with Xcode4.1 in 32bit I will get this errors:
Undefined symbols for architecture i386: "_cvLoadImage", referenced from: ...
In my Build Settings I have defined the header and library search paths.
header search paths: /usr/local/Cellar/opencv/2.2/include
/usr/local/Cellar/opencv/2.2/include/opencv
/usr/local/Cellar/opencv/2.2/include/opencv2
library search paths: /usr/local/Cellar/opencv/2.2/lib
other linker flags: -L/usr/local/Cellar/opencv/2.2/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
But it doesn't compile successful. I hope someone can help me?
I have solved the problem with the help of the wiki article section Using the OpenCV libraries in an Xcode OS X project. Just needed to add the dylib files to my project. :-)

Runtime errors when coding OpenCV programs using codeBlocks in linux

When I try to compile a C++ program which uses the openCV library in CodeBlocks it gives me a runtime error :
error while loading shared libraries: libopencv_calib3d.so.2.2:
cannot open shared object file: no such file or directory
This is in spite of the fact that I have added all the required libraries using linker settings for the Code Blocks IDE (including the one named libopencv_calib3d.so.2.2 which is a symbolic link).
However I'm able to compile the program using the command line by issuing the command :
g++ hello-world.cpp -o hello-world \
-I /usr/local/include/opencv -L /usr/local/lib
\ -lm -lcv -lhighgui -lcvaux
Can somebody advise me on how I can get to run the same using Code Blocks.
Thanks!
The file libopencv_calib3d.so.2.2 is part of OpenCV 2.2. There is, however, no libcv, libhighgui or libcvaux part of OpenCV 2.2. This means that your g++ call links against another version of OpenCV.
To link against OpenCV 2.2, you would need flags like -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann.
You should also not put them in manually, but instead use
export PKG_CONFIG_PATH=/usr/local/share/opencv/:$PKG_CONFIG_PATH
g++ `pkg-config --cflags --libs opencv` hello-world.cpp -o hello-world
I would recommend installing a reasonably up-to-date version of OpenCV for Linux, 2.4.9 for example, at this time of writing, appears to build with the current version of gcc that I have (4.8.2). There are a few steps you need to complete in order to get a simple OpenCV example up and running in Code::Blocks. The OpenCV documentation tells you to create a build directory within your OpenCV root directory and run cmake, make, sudo make install etc in order to generate the libraries your project may need to link to, including the libopencv_calib3d.so you mention:
This blog posting gives explanations on how to install OpenCV and configure it's use for Code::Blocks in Ubuntu Linux environments:
http://www.technical-recipes.com/2014/using-opencv-in-codeblocks-in-linux/