0

I'm a newbie on openCV.
I just downloaded the source of openCV from Github and installed it on the desktop (Mac OS).

Now I need to do convert a binary file to an image file. Here is the code:

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

However I got an error as below:

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)

As I said, I installed the openCV on the desktop, here is how I compile:

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

Here is all lib files that I found in the openCV directory:
enter image description here

What should I do to solve this link error?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Yves
  • 11,597
  • 17
  • 83
  • 180
  • to use imread you'll need image headers in the file. If you want to read the binary manually and construct an image from it you must know the width, height and pixeltype of the raw data. – Micka Aug 09 '17 at 06:48
  • @Micka Could you give me an example? – Yves Aug 09 '17 at 08:26

1 Answers1

0

The error says it could not find imshow, which is in highgui. From what you provided, you need to link libopencv_highgui.dylib also. For example:

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

On the other note, I recommend using Cmake to automatically link all opencv libraries. You can find a CMakeList.txt sample here.

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74