我在我的 ubuntu 16.04 机器上安装了两个版本的 openCV,一个是 openCV3,另一个是 openCV2.4,并且两者都需要。但是我现在无法编译用于 openCV3 的 C++ 代码。当尝试编译我的代码时,我遇到了大量的链接器错误,在安装 openvc2.4 之前,这些代码总是编译得很好
我得到的错误类型:
In file included from main.cpp:10:0:
/usr/local/include/opencv2/imgproc.hpp:4090:5: note: previous declaration ‘cv::ColormapTypes COLORMAP_PINK’
COLORMAP_PINK = 10, //!< ![pink](pics/colormaps/colorscale_pink.jpg)
^
In file included from /usr/local/include/opencv2/opencv.hpp:59:0,
from include/faceDetection.h:6,
from main.cpp:11:
/usr/local/include/opencv2/contrib/contrib.hpp:980:24: error: redeclaration of ‘COLORMAP_HOT’
COLORMAP_HOT = 11
^
In file included from main.cpp:10:0:
/usr/local/include/opencv2/imgproc.hpp:4091:5: note: previous declaration ‘cv::ColormapTypes COLORMAP_HOT’
COLORMAP_HOT = 11, //!< ![hot](pics/colormaps/colorscale_hot.jpg)
^
main.cpp: In function ‘int main(int, char**)’:
main.cpp:89:65: error: call of overloaded ‘createFisherFaceRecognizer()’ is ambiguous
Ptr<BasicFaceRecognizer> model = createFisherFaceRecognizer();
^
In file included from /usr/local/include/opencv2/face.hpp:373:0,
from main.cpp:8:
/usr/local/include/opencv2/face/facerec.hpp:99:39: note: candidate: cv::Ptr<cv::face::BasicFaceRecognizer> cv::face::createFisherFaceRecognizer(int, double)
CV_EXPORTS_W Ptr<BasicFaceRecognizer> createFisherFaceRecognizer(int num_components = 0, double threshold = DBL_MAX);
^
In file included from /usr/local/include/opencv2/opencv.hpp:59:0,
from include/faceDetection.h:6,
from main.cpp:11:
/usr/local/include/opencv2/contrib/contrib.hpp:963:38: note: candidate: cv::Ptr<cv::FaceRecognizer> cv::createFisherFaceRecognizer(int, double)
CV_EXPORTS_W Ptr<FaceRecognizer> createFisherFaceRecognizer(int num_components = 0, double threshold = DBL_MAX);
^
main.cpp:93:48: error: no matching function for call to ‘cv::face::BasicFaceRecognizer::train(std::vector<cv::Mat>&, std::vector<int>&)’
model->train(trainImages, labelsTrainImages);
^
In file included from main.cpp:8:0:
/usr/local/include/opencv2/face.hpp:207:26: note: candidate: virtual void cv::face::FaceRecognizer::train(int, int)
CV_WRAP virtual void train(InputArrayOfArrays src, InputArray labels) = 0;
^
/usr/local/include/opencv2/face.hpp:207:26: note: no known conversion for argument 1 from ‘std::vector<cv::Mat>’ to ‘int’
main.cpp:123:64: error: no matching function for call to ‘cv::face::BasicFaceRecognizer::predict(cv::Mat&, int&, double&)’
model->predict(face, predictedLabel, confidence);
^
In file included from main.cpp:8:0:
/usr/local/include/opencv2/face.hpp:259:35: note: candidate: int cv::face::FaceRecognizer::predict(int) const
CV_WRAP_AS(predict_label) int predict(InputArray src) const;
我编译的命令:
g++ src/faceDetection.cpp src/faceRecognition.cpp main.cpp -L/usr/local/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core -o main
这是 /usr/local/include 的样子:
$ ls /usr/local/include/
folly opencv opencv2 wangle
$ls /usr/local/include/opencv
cvaux.h cvaux.hpp cv.h cv.hpp cvwimage.h cxcore.h cxcore.hpp cxeigen.hpp cxmisc.h highgui.h ml.h
$ ls /usr/local/include/opencv2
aruco contrib dpm.hpp fuzzy.hpp imgproc.hpp ocl plot.hpp stereo.hpp text videostab
aruco.hpp core face gpu legacy opencv.hpp reg stitching text.hpp videostab.hpp
bgsegm.hpp core.hpp face.hpp hdf line_descriptor opencv_modules.hpp rgbd stitching.hpp tracking xfeatures2d
bioinspired cvconfig.h features2d hdf.hpp line_descriptor.hpp optflow rgbd.hpp structured_light tracking.hpp xfeatures2d.hpp
bioinspired.hpp cvv features2d.hpp highgui ml optflow.hpp saliency structured_light.hpp ts ximgproc
calib3d cvv.hpp flann highgui.hpp ml.hpp phase_unwrapping saliency.hpp superres video ximgproc.hpp
calib3d.hpp datasets flann.hpp imgcodecs nonfree phase_unwrapping.hpp shape superres.hpp video.hpp xobjdetect.hpp
ccalib dnn freetype.hpp imgcodecs.hpp objdetect photo shape.hpp surface_matching videoio xphoto
ccalib.hpp dnn.hpp fuzzy imgproc objdetect.hpp photo.hpp stereo surface_matching.hpp videoio.hpp xphoto.hpp
/usr/lib/x86_64-linux-gnu 的内容:http://paste.ubuntu.com/25261998/。
答案1
OpenCV3 将其标头安装到一个opencv
用于版本 3 内容的文件夹和一个opencv2
用于向后兼容内容的文件夹中。当您在 OpenCV3 之后构建并安装 OpenCV2.4 时,您可能确实覆盖了标头。库本身有版本,但标头没有。
我不确定安装这两个版本是否有意义,但如果您有理由,请卸载这两个版本以避免混乱,在默认位置重新安装 OpenCV3,然后为 OpenCV2 选择不同的路径前缀,例如/opt/opencv2
.
然后,当针对 OpenCV2 构建某些内容时,提供/opt/opencv2/include
包含路径和设置LD_LIBRARY_PATH=/opt/opencv2/lib
(用于构建和执行)。
如果您使用cmake
它来构建项目,则必须调整(或创建)脚本FindOpencv.cmake
。
答案2
在 Gentoo 中,我们支持插槽...这意味着我们可以在同一个系统中同时存在一个软件包的不同版本。
这是可能的,因为我们的包管理器(portage)使用包中的通用名称和版本的不同名称创建符号链接。
例如,如果您有 python 2 和 3...则只有一个被调用的符号链接python
指向特定版本(2 或 3),并且您有一个脚本来处理哪个版本是符号链接指向的。
我想您可以对标头执行相同的操作,创建一个opencv.h
并拥有两个不同的标头文件...然后您可以将原始文件链接到您的 .h 文件之一。
如果您不想自己做并创建与我们已有的逻辑相同的逻辑,您可以尝试一下 gentoo。
答案3
在您的共享库文件夹中,两个版本的 so 文件均已安装。当您指定 -l 时,gcc
默认为更高的 SONAME - 在您的情况下似乎是 2.4 。或许这Stack Overflow 上的答案可以帮助您(恕我直言,第二个答案是最好的)。