Collect2 ld error when compiling program with openCV - c++

When compiling a program with opencv2, I'm getting a weird compiler error with ld. I'm unable to find anything online to do with the error.
I'm using this command to compile the program
g++ -Wall Tracker.cpp -o Tracker
and I'm getting this output from the compiler
Tracker.cpp: In function ‘int main(int, char**)’:
Tracker.cpp:60:10: warning: unused variable ‘ok’ [-Wunused-variable]
60 | bool ok = video.read(frame);
| ^~
/usr/bin/ld: /tmp/cc2Q6W8D.o: in function `main':
Tracker.cpp:(.text+0x29d): undefined reference to `cv::VideoCapture::VideoCapture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: Tracker.cpp:(.text+0x2ca): undefined reference to `cv::VideoCapture::isOpened() const'
/usr/bin/ld: Tracker.cpp:(.text+0x34a): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)'
/usr/bin/ld: Tracker.cpp:(.text+0x425): undefined reference to `cv::rectangle(cv::_InputOutputArray const&, cv::Rect_<int>, cv::Scalar_<double> const&, int, int, int)'
/usr/bin/ld: Tracker.cpp:(.text+0x492): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: Tracker.cpp:(.text+0x503): undefined reference to `cv::Tracker::init(cv::_InputArray const&, cv::Rect_<double> const&)'
/usr/bin/ld: Tracker.cpp:(.text+0x544): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)'
/usr/bin/ld: Tracker.cpp:(.text+0x562): undefined reference to `cv::getTickCount()'
/usr/bin/ld: Tracker.cpp:(.text+0x5b3): undefined reference to `cv::Tracker::update(cv::_InputArray const&, cv::Rect_<double>&)'
/usr/bin/ld: Tracker.cpp:(.text+0x5cd): undefined reference to `cv::getTickFrequency()'
/usr/bin/ld: Tracker.cpp:(.text+0x5da): undefined reference to `cv::getTickCount()'
/usr/bin/ld: Tracker.cpp:(.text+0x697): undefined reference to `cv::rectangle(cv::_InputOutputArray const&, cv::Rect_<int>, cv::Scalar_<double> const&, int, int, int)'
/usr/bin/ld: Tracker.cpp:(.text+0x772): undefined reference to `cv::putText(cv::_InputOutputArray const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'
/usr/bin/ld: Tracker.cpp:(.text+0x86e): undefined reference to `cv::putText(cv::_InputOutputArray const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'
/usr/bin/ld: Tracker.cpp:(.text+0x9ab): undefined reference to `cv::putText(cv::_InputOutputArray const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'
/usr/bin/ld: Tracker.cpp:(.text+0xa49): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: Tracker.cpp:(.text+0xa80): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: Tracker.cpp:(.text+0xab8): undefined reference to `cv::VideoCapture::~VideoCapture()'
/usr/bin/ld: Tracker.cpp:(.text+0xe35): undefined reference to `cv::VideoCapture::~VideoCapture()'
/usr/bin/ld: /tmp/cc2Q6W8D.o: in function `cv::Mat::~Mat()':
Tracker.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/usr/bin/ld: /tmp/cc2Q6W8D.o: in function `cv::Mat::release()':
Tracker.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
Tracker.cpp
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
// Convert to string
#define SSTR( x ) static_cast< std::ostringstream & >(( std::ostringstream() << std::dec << x )).str()
int main(int argc, char **argv)
{
// List of tracker types in OpenCV 3.4.1
string trackerTypes[8] = {"BOOSTING", "MIL", "KCF", "TLD","MEDIANFLOW", "GOTURN", "MOSSE", "CSRT"};
// vector <string> trackerTypes(types, std::end(types));
// Create a tracker
string trackerType = trackerTypes[2];
Ptr<Tracker> tracker;
#if (CV_MINOR_VERSION < 3)
{
// tracker = Tracker::create(trackerType);
}
#else
{
if (trackerType == "BOOSTING")
tracker = TrackerBoosting::create();
if (trackerType == "MIL")
tracker = TrackerMIL::create();
if (trackerType == "KCF")
tracker = TrackerKCF::create();
if (trackerType == "TLD")
tracker = TrackerTLD::create();
if (trackerType == "MEDIANFLOW")
tracker = TrackerMedianFlow::create();
if (trackerType == "GOTURN")
tracker = TrackerGOTURN::create();
if (trackerType == "MOSSE")
tracker = TrackerMOSSE::create();
if (trackerType == "CSRT")
tracker = TrackerCSRT::create();
}
#endif
// Read video
VideoCapture video("videos/test.mp4");
// Exit if video is not opened
if(!video.isOpened())
{
cout << "Could not read video file" << endl;
return 1;
}
// Read first frame
Mat frame;
bool ok = video.read(frame);
// Define initial bounding box
Rect2d bbox(287, 23, 86, 320);
// Uncomment the line below to select a different bounding box
//bbox = selectROI(frame, false);
// Display bounding box.
rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );
imshow("Tracking", frame);
tracker->init(frame, bbox);
while(video.read(frame))
{
// Start timer
double timer = (double)getTickCount();
// Update the tracking result
bool ok = tracker->update(frame, bbox);
// Calculate Frames per second (FPS)
float fps = getTickFrequency() / ((double)getTickCount() - timer);
if (ok)
{
// Tracking success : Draw the tracked object
rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );
}
else
{
// Tracking failure detected.
putText(frame, "Tracking failure detected", Point(100,80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,255),2);
}
// Display tracker type on frame
putText(frame, trackerType + " Tracker", Point(100,20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50,170,50),2);
// Display FPS on frame
putText(frame, "FPS : " + SSTR(int(fps)), Point(100,50), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50,170,50), 2);
// Display frame.
imshow("Tracking", frame);
// Exit if ESC pressed.
int k = waitKey(1);
if(k == 27)
{
break;
}
}
}
I've never seen this sort of error before and i'm not sure what's causing it, any help would be great.

Looks like you forgot to link, maybe something like:
g++ -Wall Tracker.cpp -o Tracker -lopencv_core -lopencv_imgproc -lopencv_dnn -lopencv_highgui
You might have to add -lopencv_videoio as well depending on version

Related

Cmake failed to link with Boost Regex in G++

Setup environment
1. Install CMake
cd ~
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
tar xf cmake-3.14.5.tar.gz
cd cmake-3.14.5
./bootstrap --parallel=10
make -j4
sudo make -j4 install
2. Install Boost
cd ~
wget https://boostorg.jfrog.io/artifactory/main/release/1.69.0/source/boost_1_69_0.tar.gz
tar xf boost_1_69_0.tar.gz
cd boost_1_69_0
./bootstrap.sh
./b2 ... cxxflags="-std=c++0x -stdlib=libc++" linkflags="-stdlib=libc++" ...
sudo ./b2 toolset=gcc -j4 install
CMakeLists.txt
# Defines AppBase library target.
project(recipe_01)
cmake_minimum_required(VERSION 3.5)
include(GNUInstallDirs)
set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS 14)
message(FATAL_ERROR "app requires c++14 or newer")
elseif(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
find_package(Boost 1.60 REQUIRED COMPONENTS regex)
add_executable(main main.cpp)
target_link_libraries(main Boost::regex)
main.cpp
#include <boost/regex.hpp>
#include <iostream>
int main() {
// output the available regex syntaxes
std::cout
<< "Available regex syntaxes:\n"
<< "\t[0] Perl\n"
<< "\t[1] Perl case insensitive\n"
<< "\t[2] POSIX extended\n"
<< "\t[3] POSIX extended case insensitive\n"
<< "\t[4] POSIX basic\n"
<< "\t[5] POSIX basic case insensitive\n"
<< "Choose regex syntax: ";
// correctly set up flags, according to the chosen syntax
boost::regex::flag_type flag;
switch (std::cin.get()) {
case '0': flag = boost::regex::perl;
break;
case '1': flag = boost::regex::perl|boost::regex::icase;
break;
case '2': flag = boost::regex::extended;
break;
case '3': flag = boost::regex::extended|boost::regex::icase;
break;
case '4': flag = boost::regex::basic;
break;
case '5': flag = boost::regex::basic|boost::regex::icase;
break;
default:
std::cout << "Incorrect number of regex syntax. Exiting... \n";
return -1;
}
// Disabling exceptions
flag |= boost::regex::no_except;
//requesting regex patterns in a loop
// Restoring std::cin
std::cin.ignore();
std::cin.clear();
//Getting a String to match: in a loop
std::string regex, str;
do {
std::cout << "Input regex: ";
if (!std::getline(std::cin, regex) || regex.empty()) {
return 0;
}
// Without `boost::regex::no_except`flag this
// constructor may throw
const boost::regex e(regex, flag);
if (e.status()) {
std::cout << "Incorrect regex pattern!\n";
continue;
}
std::cout << "String to match: ";
while (std::getline(std::cin, str) && !str.empty()) {
bool matched = boost::regex_match(str, e);
std::cout << (matched ? "MATCH\n" : "DOES NOT MATCH\n");
std::cout << "String to match: ";
} // end of `while (std::getline(std::cin, str))`
std::cout << '\n';
// Restoring std::cin
std::cin.ignore();
std::cin.clear();
} while (1);
return 0;
} // int main()
To build
mkdir build
cd build
cmake ..
cmake --build .
And I get this error:
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable bin/main
CMakeFiles/main.dir/main.cpp.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char,
boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
main.cpp:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISB_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISB_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x8f): undefined reference to `boost::re_detail_106900::perl_matcher<__gnu_cxx::__normal_iterator<char const*,
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()'
CMakeFiles/main.dir/main.cpp.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
main.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+0x2a): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*,
unsigned int)'
CMakeFiles/main.dir/main.cpp.o: In function `boost::re_detail_106900::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>
> >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)':
main.cpp:(.text._ZN5boost16re_detail_10690012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIcSJ_EENS_15regex_constants12_match_flagsESC_[_ZN5boost16re_detail_10690012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIcSJ_EENS_15regex_constants12_match_flagsESC_]+0x121): undefined reference to `boost::re_detail_106900::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:84: recipe for target 'bin/main' failed
make[2]: *** [bin/main] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I can not link to boost:regex with main function
Could you please tell me any solution for this issue?
I find some quote like
The boost 1.61 (and higher) are compiled with newer compilers, which use the _GLIBCXX_USE_CXX11_ABI=1 .. heh ABI. Meaning that for -std=c++11 std::basic_string... becomes std::__cxx11::basic_string... (and likewise for std::list; see the gcc Dual ABI documentation).
Since your linker error claims there is a missing symbol that contains std::string (which is short for std::basic_string<char,std::char_traits,std::allocator>), you are either compiling with a compiler that is too old and uses ABI 0, or you are even compiling explicitly with -D_GLIBCXX_USE_CXX11_ABI=0.
But how can I fix in my code?
Change the line
target_link_libraries(main Boost::regex)
to
target_link_libraries(main boost_regex)
And depending on your distro you may need to install some Boost devel packages.

Linking error while compiling sample of OpenCV

I tried to compile simple program using OpenCV 4.11 and current version of Mingw on Windows 7. I downloaded binaries for OpenCV from https://github.com/huihut/OpenCV-MinGW-Build. I tried to compile it using command:
"g++ main.cpp -std=c++11 -I F:\cpp_tools\libs\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\include -L F:\cpp_tools\libs\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\lib -lopencv_core411.dll -lopencv_imgcodecs411.dll -lopencv_imgproc411.dll -lopencv_highgui411.dll -o myprog.exe"
and i got linker error:
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text+0xa0): undefined re
ference to cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&, int)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text+0x13e): undefined r
eference tocv::namedWindow(std::__cxx11::basic_string, std::allocator > const&, int)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text+0x19f): undefined r
eference to cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&, cv::_InputArray const&)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text+0x1c9): undefined r
eference tocv::waitKey(int)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text$_ZN2cv3MatD1Ev[__ZN
2cv3MatD1Ev]+0x2d): undefined reference to cv::fastFree(void*)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text$_ZN2cv3Mat7releaseE
v[__ZN2cv3Mat7releaseEv]+0x40): undefined reference tocv::Mat::deallocate()'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text$ZN2cv3MataSEOS0[_
ZN2cv3MataSEOS0]+0xb4): undefined reference to `cv::fastFree(void*)'
collect2.exe: error: ld returned 1 exit status
I read alot of similar problems but any of it didn't solved my problem.
Source code:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
String imageName( "../data/HappyFish.jpg" ); // by default
if( argc > 1)
{
imageName = argv[1];
}
Mat image;
image = imread( imageName, IMREAD_COLOR ); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );
imshow( "Display window", image );
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Shouldn't it be
g++ main.cpp -std=c++11 -I F:\cpp_tools\libs\OpenCV-MinGW-
Build-OpenCV-4.1.1-x64\include -L F:\cpp_tools\libs\OpenCV-
MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\lib
-llibopencv_core411 -llibopencv_imgcodecs411
-llibopencv_imgproc411 -llibopencv_highgui411 -o
myprog.exe
with -llibopencv instead of -lopencv?
Have you tried the help Running a C++ program with OpenCV 3.4.1 using MinGW-w64 g++ in Visual Studio Code on Windows 10 x64 mentioned on GitHub?

Use opencv C++ on raspberry pi 2

I am successfully running opencv python code on raspberry pi 2 (raspbian).
Now I want to try to compile opencv C++ code on raspberry pi 2 by using this command:
g++ -std=c++0x test_colour_tracking_1.cpp -otest_colour
The C++ coding as below.
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap(0); //capture the video from web cam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
while (true)
{
Mat imgOriginal;
bool bSuccess = cap.read(imgOriginal); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("image",imgOriginal);
}
return 0;
}
But it show error as below.
/tmp/ccHcCqSm.o: In function `main':
test_colour_tracking_1.cpp:(.text+0x70): undefined reference to `cv::VideoCapture::VideoCapture(int)'
test_colour_tracking_1.cpp:(.text+0x7c): undefined reference to `cv::VideoCapture::isOpened() const'
test_colour_tracking_1.cpp:(.text+0xd8): undefined reference to `cv::VideoCapture::read(cv::Mat&)'
test_colour_tracking_1.cpp:(.text+0x150): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test_colour_tracking_1.cpp:(.text+0x164): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test_colour_tracking_1.cpp:(.text+0x1a4): undefined reference to `cv::VideoCapture::~VideoCapture()'
test_colour_tracking_1.cpp:(.text+0x1f0): undefined reference to `cv::VideoCapture::~VideoCapture()'
/tmp/ccHcCqSm.o: In function `cv::Mat::~Mat()':
test_colour_tracking_1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/tmp/ccHcCqSm.o: In function `cv::Mat::release()':
test_colour_tracking_1.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x58): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
And I want to ask how to check frame rate per second?
Try to use the following command:
g++ -std=c++0x test_colour_tracking_1.cpp -o test_colour `pkg-config --cflags --libs opencv`

"Function not declared in this scope" Error in compiling openCV code

I'm trying to write some code which uses openCV functions. I started by taking some of the example code available in the documentation:
#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]); // 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", CV_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;
}
When I try to build it in Eclipse-CDT, I get this:
**** Build of configuration Debug for project openCV1 ****
make all
Building target: openCV1
Invoking: Cross G++ Linker
g++ -L/usr/local/lib -o "openCV1" ./src/displayImage.o
./src/displayImage.o: In function `main':
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:25: undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:33: undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:36: undefined reference to `cv::waitKey(int)'
./src/displayImage.o: In function `~Mat':
/usr/local/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
./src/displayImage.o: In function `cv::Mat::operator=(cv::Mat const&)':
/usr/local/include/opencv2/core/mat.hpp:298: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
./src/displayImage.o: In function `cv::Mat::release()':
/usr/local/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
make: *** [openCV1] Error 1
**** Build Finished ****
The same code, when I build with g++ (g++ -o displayImageInput displayImageInput.cpppkg-config opencv --cflags --libs) works.
I then changed the code to make the image greyscale,
#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]); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
Mat grey;
cvtColor(image, grey, CV_BGR2GRAY);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", grey);
waitKey(); // Wait for a keystroke in the window
return 0;
}
It gave the following error when building with g++:
dispImgSobel.cpp: In function ‘int main(int, char**)’:
dispImgSobel.cpp:34:27: error: ‘CV_BGR2GRAY’ was not declared in this scope
dispImgSobel.cpp:34:38: error: ‘cvtColor’ was not declared in this scope
I need help with two things, How to get it working in Eclipse, and second, how to resolve this scope error, for this and future use cases.
the first err is a linker problem. you did not link against opencv_core.a and opencv_highgui.a
rule of thumb: for every module header you include, you'll need to link the appropriate library.
the 2nd is a compiler problem, you forgot a header, #include <opencv2/imgproc/imgproc.hpp>
and, ofc. need to link opencv_imgproc
Make sure that everything in the output of pkg-config opencv --cflags --libs is in your search path for the linker.
There's also a pkg-config add-on for Eclipse which will allow you to put the exact string above in directly rather than manually adding each item of the output.

Installing OpenCV2.4.1 on windows 7 x64 - Mingw & codeblocks

Installing OpenCV2.4.1 on windows 7 x64 - Mingw & codeblocks
Hi,
I followed this tutorial
Getting started with OpenCV 2.4 and MinGW on Windows 7
I configured the same except instead of x86, i put x64.
This is the example
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
when i compile main.cpp
C:\Users\rgap\Desktop>g++ -I"C:\opencv\build\include" -L"C:\opencv\build\x64\mingw\lib" main.cpp -lope
ncv_core241 -lopencv_highgui241 -o main
I get the following error
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0x62): undefined reference to `cv::imread(
std::string const&, int)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0xc7): undefined reference to `cv::_InputA
rray::_InputArray(cv::Mat const&)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0xfe): undefined reference to `cv::imshow(
std::string const&, cv::_InputArray const&)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0x120): undefined reference to `cv::waitKe
y(int)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text$_ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x2b): und
efined reference to `cv::fastFree(void*)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text$_ZN2cv3Mat7releaseEv[cv::Mat::release()]+0
x3c): undefined reference to `cv::Mat::deallocate()'
collect2: ld devolvió el estado de salida 1
Is this a bug? :(
thanks :)