【发布时间】:2020-08-27 21:24:34
【问题描述】:
我最近更新到 ubuntu 20.04。默认的 OpenCV 版本现在是 4.2,它给出了很多编译错误。
下面是一个示例程序
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, char const *argv[]) {
Mat src = imread(argv[1], 1);
namedWindow("src", 1);
imshow("src", src);
// Split the image into different channels
vector<Mat> rgbChannels(3);
split(src, rgbChannels);
// Show individual channels
Mat g;
g = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);
// Showing Red Channel
// G and B channels are kept as zero matrix for visual perception
{
Mat img_R;
vector<Mat> channels;
channels.push_back(g);
channels.push_back(g);
channels.push_back(rgbChannels[2]);
vector<Mat> R = channels;
/// Merge the three channels
merge(R, img_R);
namedWindow("R", 1);
imshow("R", img_R);
namedWindow("R2", 1);
imshow("R2", rgbChannels[2]);
}
// Showing Green Channel
{
Mat img_G;
vector<Mat> channels;
channels.push_back(g);
channels.push_back(rgbChannels[1]);
channels.push_back(g);
vector<Mat> G = channels;
merge(G, img_G);
namedWindow("G", 1);
imshow("G", img_G);
namedWindow("G2", 1);
imshow("G2", rgbChannels[1]);
}
// Showing Blue Channel
{
Mat img_B;
vector<Mat> channels;
channels.push_back(rgbChannels[0]);
channels.push_back(g);
channels.push_back(g);
vector<Mat> B = channels;
merge(channels, img_B);
namedWindow("B", 1);
imshow("B", img_B);
namedWindow("B2", 1);
imshow("B2", rgbChannels[0]);
}
// Showing Red Blue Channel
{
Mat img_RB;
vector<Mat> channels;
channels.push_back(rgbChannels[0]);
channels.push_back(g);
channels.push_back(rgbChannels[2]);
vector<Mat> RB = channels;
merge(RB, img_RB);
namedWindow("RB", 1);
imshow("RB", img_RB);
}
waitKey(0);
return 0;
}
现在当我使用
编译时g++ -g -I/usr/include/opencv4 `pkg-config --libs --cflags opencv4` rgb.cpp
然后我得到这些参考错误
/usr/bin/ld: /tmp/cc1tVO10.o: in function `main':
/home/gaurav-pant/lab/img_process/rgb.cpp:10: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:12: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:13: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:17: undefined reference to `cv::split(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:21: undefined reference to `cv::Mat::zeros(cv::Size_<int>, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:35: undefined reference to `cv::merge(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:36: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:37: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:38: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:39: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:52: undefined reference to `cv::merge(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:53: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:54: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:55: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:56: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:70: undefined reference to `cv::merge(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:71: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:72: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:73: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:74: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:87: undefined reference to `cv::merge(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:88: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:89: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: /home/gaurav-pant/lab/img_process/rgb.cpp:92: undefined reference to `cv::waitKey(int)'
/usr/bin/ld: /tmp/cc1tVO10.o: in function `cv::Mat::Mat(cv::Mat const&)':
/usr/include/opencv4/opencv2/core/mat.inl.hpp:538: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/usr/bin/ld: /tmp/cc1tVO10.o: in function `cv::Mat::~Mat()':
/usr/include/opencv4/opencv2/core/mat.inl.hpp:739: undefined reference to `cv::fastFree(void*)'
/usr/bin/ld: /tmp/cc1tVO10.o: in function `cv::Mat::release()':
/usr/include/opencv4/opencv2/core/mat.inl.hpp:851: undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
巧合的是,当我使用 CMake 包含库时,不存在此类错误。我该如何克服这个问题?
【问题讨论】:
-
您的代码中可能需要
pkg-config --libs --cflags opencv4并且可能应该使用#include<opencv2/ -
我已经完成了这个 pkg-config --libs --cflags opencv4。 #include
-
在干净的 ubuntu 20.04 上安装
pkg-config --cflags opencv4返回-I/usr/include/opencv4/opencv -I/usr/include/opencv4,/usr/include/opencv4/包含opencv2/core.hpp所以在我看来,一切正常 -
好吧,我在上面犯了一些错误,首先,包含路径必须用双引号括起来。不过,在编译时,我收到许多函数的未定义引用错误。尝试使用 CMake,效果很好
-
请发minimal reproducible example 并附上完整的错误信息
标签: c++ opencv ubuntu opencv4 ubuntu-20.04