【问题标题】:How to link a C++ program using OpenCV on MacOS [duplicate]如何在 MacOS 上使用 OpenCV 链接 C++ 程序 [重复]
【发布时间】:2017-08-09 06:41:24
【问题描述】:

我是 openCV 的新手。
我刚刚从 Github 下载了 openCV 的源码,并安装在桌面(Mac OS)上。

现在我需要将二进制文件转换为图像文件。这是代码:

// The program is first to convert the image to the binary file then convert the binary file back to the image
#include <iostream>
#include </Users/Me/Desktop/opencv-master/include/opencv2/opencv.hpp>
#include </Users/Me/Desktop/opencv-master/modules/highgui/include/opencv2/highgui.hpp>

const std::string filename = "test.dat";
const std::string picname = "pano_b.jpg";

int main(int argc, char **argv)
{
    std::ofstream outfile;
    outfile.open(filename.c_str(), std::ios::binary);
    if (!outfile)
    {
        std::cerr << "failed to open the file : " << filename << std::endl;
        return -1;
    }
    cv::Mat srcImg = cv::imread(picname);
    if (srcImg.empty())
    {
        std::cerr << "failed to open the file : " << picname << std::endl;
        return -1;
    }
    for (int r = 0; r < srcImg.rows; r++)
        outfile.write(reinterpret_cast<const char*>(srcImg.ptr(r)), srcImg.cols*srcImg.elemSize());
    std::cout << "write to file ok!" << std::endl;

    ///// Here the file test.dat is generated. It looks like this:
    ///// 0069 a800 6caa 0074 af0c 80b9 2096 ca2a
    ///// 9fd2 1c8d bf03 71a1 0773 a300 6295 0064
    ///// 9907 73a9 0070 a900 6da8 0480 bc18 95d2
    ///// ...



    //////// Now I want to convert test.dat into an image file

    cv::Mat img = cv::imread(filename.c_str(), cv::IMREAD_ANYCOLOR);
    cv::imshow("img", img);
    cv::waitKey();

    return 0;
}

但是我收到如下错误:

架构 x86_64 的未定义符号:“cv::imshow(cv::String const&, cv::_InputArray const&)",引用自: bbb-bb77f0.o ld 中的 _main:未找到架构 x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 到 见调用)

正如我所说,我在桌面上安装了 openCV,这是我的编译方式:

g++ test.cpp /Users/Me/Desktop/opencv-master/build/lib/libopencv_core.dylib /Users/Me/Desktop/opencv-master/build/lib/libopencv_imgproc.dylib /Users/Me/Desktop/opencv-master/build/lib/libopencv_imgcodecs.dylib

这是我在 openCV 目录中找到的所有 lib 文件:

我应该怎么做才能解决这个链接错误?

【问题讨论】:

  • 要使用 imread,您需要文件中的图像标题。如果您想手动读取二进制文件并从中构建图像,您必须知道原始数据的宽度、高度和像素类型。
  • @Micka 你能给我举个例子吗?

标签: c++ macos opencv


【解决方案1】:

错误提示找不到imshow,它位于highgui。根据您提供的内容,您还需要链接libopencv_highgui.dylib。例如:

g++ test.cpp /Users/Me/Desktop/opencv-master/build/lib/libopencv_core.dylib \
             /Users/Me/Desktop/opencv-master/build/lib/libopencv_imgproc.dylib \
             /Users/Me/Desktop/opencv-master/build/lib/libopencv_imgcodecs.dylib \
             /Users/Me/Desktop/opencv-master/build/lib/libopencv_highgui.dylib

另一方面,我建议使用 Cmake 自动链接所有 opencv 库。您可以找到 CMakeList.txt 示例 here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 2021-05-30
    • 1970-01-01
    • 2021-09-10
    • 2021-06-10
    • 2021-09-04
    • 2012-10-16
    • 1970-01-01
    相关资源
    最近更新 更多