can not compile opencv programs in ubuntu - c++

I have wrote a simple program like this
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, const char* argv[])
{
Mat input = imread(argv[1], 0); //Load as grayscale
//~ FAST detector;
//~ vector<KeyPoint> keypoints;
//~ FAST(input, keypoints, 0.2);
// Add results to image and save.
//~ Mat output;
//~ drawKeypoints(input, keypoints, output);
namedWindow ("Image", CV_WINDOW_FREERATIO);
imshow("Image", input);
//~ imwrite(argv[2], output);
return 0;
}
Then compile program like this:
g++ `pkg-config --libs opencv` main.cpp
And here is output of g++:
/tmp/ccK1Sbrw.o: In function `main':
main.cpp:(.text+0x66): undefined reference to `cv::imread(std::string const&, int)'
main.cpp:(.text+0xc2): undefined reference to `cv::namedWindow(std::string const&, int)'
main.cpp:(.text+0xf6): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
main.cpp:(.text+0x139): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
/tmp/ccK1Sbrw.o: In function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccK1Sbrw.o: In function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
I have the library installed, I can see *.so and *.hpp files in their folders, and ld find them, but what does it complain about? there is nothing in .so files?!
Also, I do not have nonfree modules installed (I uset apt-get to install opencv), how can I get them? I need SIFT which is inside that module. Do I have to compile opencv myself?

It appears to me that you forgot to specify a module in
`pkg-config --libs MODULENAMEGOESHERE`

Related

Code with Opencv4 compiles but not links- not finding Mat and imshow

I am trying to create executable of following code taken from here:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
using namespace cv;
using namespace std;
// Driver code
int main(int argc, char** argv)
{
// Read the image file as // imread("default.jpg");
Mat image = imread("barchart.png", IMREAD_GRAYSCALE);
// Error Handling
if (image.empty()) {
cout << "Image File Not Found" << endl;
// wait for any key press
cin.get();
return -1;
}
// Show Image inside a window with the name provided
imshow("Window Name", image);
// Wait for any keystroke
waitKey(0);
return 0;
}
It compiles all right with command g++ showimage.cpp -c -I /usr/include/opencv4 and creates an showimage.o file
However, following command to link it gives error:
g++ showimage.o -l:libopencv_imgcodecs.a
...
... (lot of output)
/usr/bin/ld: showimage.o: in function `main':
showimage.cpp:(.text+0x83): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: showimage.cpp:(.text+0x10e): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: showimage.cpp:(.text+0x13c): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: showimage.cpp:(.text+0x150): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: showimage.cpp:(.text+0x1c2): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
I also tried following option:
g++ showimage.o -L /usr/lib/x86_64-linux-gnu
g++ showimage.o -L libopencv
g++ showimage.o -L opencv
But none is not working.
Where is the problem and how can it be solved?

Undefined reference errors for OpenCV Hello World in Ubuntu

I'm trying the following code:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char** argv) {
namedWindow("Output",1);
Mat output = Mat::zeros( 120, 350, CV_8UC3 );
putText(output,"Hello World",cvPoint(15,70),
FONT_HERSHEY_PLAIN,3,cvScalar(0,255,0),4);
imshow("Output", output);
waitKey(0);
return 0;
}
I then tried g++ -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui opencv_hello.cpp -o opencv_hello
and g++pkg-config opencv cvblob --cflags --libsopencv_hello.cpp -o opencv_hello
But they both give the same undefined reference errors:
opencv_hello.cpp:(.text+0x132): undefined reference to `cv::namedWindow(cv::String const&, int)'
opencv_hello.cpp:(.text+0x15f): undefined reference to `cv::Mat::zeros(int, int, int)'
opencv_hello.cpp:(.text+0x26f): undefined reference to `cv::putText(cv::_InputOutputArray const&, cv::String const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'
opencv_hello.cpp:(.text+0x2d7): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
opencv_hello.cpp:(.text+0x2ff): undefined reference to `cv::waitKey(int)'
/tmp/cctt8VGQ.o: In function `cv::String::String(char const*)':
opencv_hello.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/cctt8VGQ.o: In function `cv::String::~String()':
opencv_hello.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/cctt8VGQ.o: In function `cv::Mat::~Mat()':
opencv_hello.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cctt8VGQ.o: In function `cv::Mat::release()':
opencv_hello.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
How can I fix this?
If pkg-config opencv --cflags --libs command finds OpenCV include files and libraries below compilation works wituout any errors.
g++ opencv_hello.cpp -o opencv_hello $(pkg-config opencv --cflags --libs)
or
g++ opencv_hello.cpp -o opencv_hello `pkg-config opencv --cflags --libs`

Compile opencv3.1 with mingw

I want to compile the following code with mingw-w64.
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread("lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
after following Getting started with OpenCV 2.4 and MinGW on Windows 7
I compile the code with g++ -I D:\opencv\build\include -L D:\opencv\build\x64\vc14\lib -lopencv_world310 .\loadimg.cpp
but it returns undefined reference
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text+0x40): undefined reference to `cv::imread(cv::String const&, int)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text+0xb7): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text+0xd9): undefined reference to `cv::waitKey(int)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv6StringC1EPKc[_ZN2cv6StringC1EPKc]+0x4a): undefined reference to `cv::String::allocate(unsigned long long)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv6StringD1Ev[_ZN2cv6StringD1Ev]+0x11): undefined reference to `cv::String::deallocate()'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv3MatD1Ev[_ZN2cv3MatD1Ev]+0x36): undefined reference to `cv::fastFree(void*)'
C:\Users\1409021\AppData\Local\Temp\cc8ZRPJP.o:loadimg.cpp:(.text$_ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x48): undefined reference to `cv::Mat::deallocate()'
the library is opencv3.1, and there is only one library opencv_world310
Any advise?
Thank you.
g++ -I D:\opencv\build\include -L D:\opencv\build\x64\vc14\lib -lopencv_world310 .\loadimg.cpp
is wrong.
g++ -I D:\opencv\build\include -L D:\opencv\build\x64\vc14\lib .\loadimg.cpp -lopencv_world310
is right. Explained here

undefined reference error in g++

i have the following code:
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timestamp.h"
#include "Poco/DateTimeFormatter.h"
int main(int argc, char** argv)
{
Poco::Net::SocketAddress sa("127.0.0.1", 8080);
Poco::Net::DatagramSocket dgs(sa);
std::string syslogMsg;
Poco::Timestamp now;
syslogMsg = Poco::DateTimeFormatter::format(now,
"<14>%w %f %H:%M:%S Hello, world!");
return 0;
}
and after compiling i get the following errors:
/tmp/cc38RdWw.o: In function `main':
pocoSender.cpp:(.text+0x4d): undefined reference to `Poco::Net::SocketAddress::SocketAddress(std::string const&, unsigned short)'
pocoSender.cpp:(.text+0x7d): undefined reference to `Poco::Net::DatagramSocket::DatagramSocket(Poco::Net::SocketAddress const&, bool)'
pocoSender.cpp:(.text+0x93): undefined reference to `Poco::Timestamp::Timestamp()'
pocoSender.cpp:(.text+0x11f): undefined reference to `Poco::Timestamp::~Timestamp()'
pocoSender.cpp:(.text+0x135): undefined reference to `Poco::Net::DatagramSocket::~DatagramSocket()'
pocoSender.cpp:(.text+0x140): undefined reference to `Poco::Net::SocketAddress::~SocketAddress()'
pocoSender.cpp:(.text+0x163): undefined reference to `Poco::Net::SocketAddress::~SocketAddress()'
pocoSender.cpp:(.text+0x1ac): undefined reference to `Poco::Timestamp::~Timestamp()'
pocoSender.cpp:(.text+0x1ca): undefined reference to `Poco::Net::DatagramSocket::~DatagramSocket()'
pocoSender.cpp:(.text+0x1d9): undefined reference to `Poco::Net::SocketAddress::~SocketAddress()'
/tmp/cc38RdWw.o: In function `Poco::DateTimeFormatter::format(Poco::Timestamp const&, std::string const&, int)':
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi]+0x15): undefined reference to `Poco::DateTime::DateTime(Poco::Timestamp const&)'
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi]+0x43): undefined reference to `Poco::DateTime::~DateTime()'
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi]+0x52): undefined reference to `Poco::DateTime::~DateTime()'
/tmp/cc38RdWw.o: In function `Poco::DateTimeFormatter::format(Poco::DateTime const&, std::string const&, int)':
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_8DateTimeERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_8DateTimeERKSsi]+0x41): undefined reference to `Poco::DateTimeFormatter::append(std::string&, Poco::DateTime const&, std::string const&, int)'
collect2: error: ld returned 1 exit status
i use the following commandline to compile:
g++ -L/usr/local/lib -lPocoUtil -lPocoFoundation -lPocoNet pocoSender.cpp -o hello
please do not mark the question as duplicate as i have included the required libraries using g++ flags, but i am still getting these errors.
It means that the compiler or linker did not find implementations of these functions. It seems that the corresponding libraries or object modules were not included in building of the project.

Simple SIFT Detector ISSUE on OpenCV

I'm implementing this simple example just to get familiar with OpenCv and SIFT.
1 #include <opencv2/core/core.hpp>
2 #include <opencv2/features2d/features2d.hpp>
3 #include <opencv2/highgui/highgui.hpp>
4
5 using namespace std;
6
7 int main(int argc, char** argv) {
8
9 const cv::Mat input = cv::imread("/tmp/image.jpg", 0); //Load as grayscale
10
11 cv::SiftFeatureDetector detector;
12 std::vector<cv::KeyPoint> keypoints;
13 detector.detect(input, keypoints);
14
15 // Add results to image and save.
16 cv::Mat output;
17 cv::drawKeypoints(input, keypoints, output);
18 cv::imwrite("/tmp/SIFT_RESULT.jpg", output);
19
20 return 0;
21
22 }
I was expecting it to be quite straightforward, but it's throwing the following errors:
undefined reference to 'cv::SIFT::CommonParams::CommonParams()' at line 11
undefined reference to 'cv::FeatureDetector::detect(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::Mat const&) const' at line 13
undefined reference to 'cv::drawKeypoints(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> > const&, cv::Mat&, cv::Scalar_<double> const&, int)' at line 17
Can you please tell me what's going wrong?
Is it a problem in the code, or am I missing some headers?
Complete Build Output:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/include/c++/4.5.2 -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: BioInfo
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "BioInfo" ./imageProcessingClass.o ./main.o -lopencv_core -lopencv_highgui
./main.o: In function `main':
/.../Debug/../main.cpp:38: undefined reference to `cv::SIFT::CommonParams::CommonParams()'
/.../Debug/../main.cpp:38: undefined reference to `cv::SIFT::DetectorParams::DetectorParams()'
/.../Debug/../main.cpp:38: undefined reference to `cv::SiftFeatureDetector::SiftFeatureDetector(cv::SIFT::DetectorParams const&, cv::SIFT::CommonParams const&)'
/.../Debug/../main.cpp:40: undefined reference to `cv::FeatureDetector::detect(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::Mat const&) const'
/.../Debug/../main.cpp:44: undefined reference to `cv::drawKeypoints(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> > const&, cv::Mat&, cv::Scalar_<double> const&, int)'
./main.o: In function `~SiftFeatureDetector':
/usr/local/include/opencv2/features2d/features2d.hpp:1502: undefined reference to `vtable for cv::SiftFeatureDetector'
/usr/local/include/opencv2/features2d/features2d.hpp:1502: undefined reference to `cv::FeatureDetector::~FeatureDetector()'
collect2: ld returned 1 exit status
make: *** [BioInfo] Error 1
Build Finished
It seems you have forgotten to link with the OpenCV libraries. Also remember if you're linking against OpenCV 2.4 there is a new library for "non-free" algorithms named opencv_nonfree that you need to link against for SIFT.