2017-08-09 14 views
0

私はopenCVの初心者です。
GithubからopenCVのソースをダウンロードし、それをデスクトップ(Mac OS)にインストールしました。
MacOSでOpenCVを使ってC++プログラムをリンクする方法

今、バイナリファイルをイメージファイルに変換する必要があります。ここでは、コードされています

// 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; 
} 

私は以下のようにエラーが発生しましたが:

Undefined symbols for architecture x86_64: "cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from: _main in bbb-bb77f0.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

私が言ったように、私はここでは、デスクトップ上の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ファイルは次のとおりです。
enter image description here

このリンクエラーを解決するにはどうすればよいですか?

+0

imreadを使用するには、ファイル内にイメージヘッダーが必要です。バイナリを手動で読み込んでイメージを構築する場合は、生データの幅、高さ、ピクセルタイプを知っている必要があります。 – Micka

+0

@Micka私に例を挙げてもらえますか? – Yves

答えて

0

highguiにあるimshowが見つかりませんでした。あなたが提供したものから、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を見つけることができます。

関連する問題