0
votes

I installed Ubuntu 14.04 and Opencv using the steps mentioned here https://solarianprogrammer.com/2014/04/21/opencv-beaglebone-black-ubuntu/
I am trying to compile the following code (saved in text file named as test2.cpp. test2.cpp and lena.jpg were copied to beaglebone home folder) :

// Test to convert a color image to gray
// Build on Linux with:
// g++ test2.cpp -o test2 -lopencv_core -lopencv_imgproc -lopencv_highgui

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    // Load the image file and check for success
    cv::Mat input = cv::imread("lena.jpg", 1);
    if(!input.data) {
        std::cout << "Unable to open the image file" << std::endl;
        return -1;
    }

    // Convert the input file to gray
    cv::Mat gray_image;
    cvtColor(input, gray_image, cv::COLOR_BGR2GRAY);

    // Save the result
    cv::imwrite("lena_gray.jpg", gray_image);

    return 0;
}

using g++ test2.cpp -o test2 -lopencv_core -lopencv_imgproc -lopencv_highgui I also tried telling the compiler where the libraries are using -L /usr/local/lib . (the libopencv files were found there). But got the following error each time:

ubuntu@arm:~$ g++ test2.cpp -o test2 -lopencv_core -lopencv_imgproc -lopencv_highgui -L usr/local/lib
/tmp/cckXjOPd.o: In function `main':
test2.cpp:(.text+0x26): undefined reference to `cv::imread(cv::String const&, int)'
test2.cpp:(.text+0xf0): undefined reference to `cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int=""> > const&)'
collect2: error: ld returned 1 exit status

Can someone help me out here? Any help would be appreciated.

1
Typo: you are missing a leading / for the library path. You need -L /usr/local/lib. It may be easier to write a Makefile where this kind of thing is set properly.juanchopanza
I tried adding a leading / but got the same error.Clive

1 Answers

0
votes

I find it always convenient and safe to compile using:

g++ test2.cpp -o test2 `pkg-config --libs --cflags opencv`

Install pkg-config if not already present.