更新:代码在另一台计算机上成功编译。所以问题不在于代码本身,而在于我安装了依赖项的方式。
如果我遗漏了任何必要的信息,请告诉我。完整代码可以找到here。
以下代码使用these instructions从SourceForge新安装的OpenCV 3.2.0在Ubuntu 14.04上编译并运行完全正常:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitkey(0);
return 0;
}
使用以下CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
以下代码无法编译。
lane_finder.h
#ifndef Lane_Finder_h
#define Lane_Finder_h
#include <stdio.h>
#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/image_encodings.h>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/opencv.hpp>
class LaneFinder
{ *snip* }
#endif
lane_finder.cpp:
#include "lane_finder.h"
sensor_msgs::CompressedImage LaneFinder::findLanes(const sensor_msgs::Image& msg) {
*snip*
frame = in_msg->image;
cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE );
cv::imshow("Display Image", frame);
*snip*
}
的CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(lane_finding)
find_package(catkin REQUIRED COMPONENTS roscpp cv_bridge sensor_msgs)
find_package(OpenCV REQUIRED)
catkin_package()
include_directories(include ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
add_executable(lane_finder src/lane_finder.cpp src/main.cpp)
target_link_libraries(lane_finder
${catkin_LIBRARIES}
${OpenCV_LIBS}
)
编译错误:
CMakeFiles/lane_finder.dir/src/lane_finder.cpp.o: In function `LaneFinder::findLanes(sensor_msgs::Image_<std::allocator<void> > const&)':
lane_finder.cpp:(.text+0x36c): undefined reference to `cv::namedWindow(cv::String const&, int)'
lane_finder.cpp:(.text+0x3c1): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
CMakeFiles/lane_finder.dir/src/lane_finder.cpp.o: In function `cv::String::String(char const*)':
lane_finder.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4f): undefined reference to `cv::String::allocate(unsigned long)'
CMakeFiles/lane_finder.dir/src/lane_finder.cpp.o: In function `cv::String::~String()':
lane_finder.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
CMakeFiles/lane_finder.dir/src/lane_finder.cpp.o: In function `cv::String::operator=(cv::String const&)':
lane_finder.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
我不能为我的生活弄清楚为什么一个人跑得很好而另一个人失败了。我怀疑我已经链接了一些不正确的东西,因为这是我第一次尝试使用我自己编写的头文件进行编译。如果你认为我在试图保持这种可读性方面省略了一些重要内容,那么我已经在本文开头包含了一个包含完整代码的github repo的链接。
非常感谢你们。
答案 0 :(得分:0)
You should add this option:
pkg-config opencv --cflags --libs
答案 1 :(得分:0)
我的平台:
安装opencv 3.30后,我编写代码:
//main.cc
#include <iostream>
#include <QDir>
#include <qdebug>
#include "opencv2/opencv.hpp"
using namespace std;
int main()
{
qDebug()<< QDir::currentPath();
cv::Mat image = cv::imread("1.jpg",1);
std::cout << image.cols << " " << image.rows << std::endl;
if(image.data == 0){
cout << "No Image" <<endl;
}
else{
cout << "Image" << endl;
}
cv::namedWindow("My Image");
cv::imshow("My Image", image)
system("pause");
return 0;
}
我得到这样的错误:
release/main.o:main.cpp:(.text.startup+0x170): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
collect2.exe: error: ld returned 1 exit status
我不知道为什么,但是其他函数运行良好,请期待imshow()。我确实添加了LIBS:
//qt.pro
INCLUDEPATH += D:\opencv\build\mingw\include
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_highgui330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_calib3d330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_dnn330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_features2d330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_flann330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_imgcodecs330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_imgproc330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_ml330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_objdetect330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_photo330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_shape330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_stitching330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_superres330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_video330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_videoio330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_videostab330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\opencv_ffmpeg330.dll
LIBS += D:\opencv\build\mingw\x86\mingw\bin\libopencv_core330.dll
答案 2 :(得分:-1)
cv_bridge依赖于OpenCV 2.4。更改CMakeLists.txt以指定:
find_package(OpenCV 2 REQUIRED)