2

While I was working on a project, I noticed that I am not able to load images. Here is the simple code that I used to check:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std; 

int main( int argc, char** argv )
{ 
    if( argc != 2) 
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    } 

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    Mat gray_image;
    cv::cvtColor(image, gray_image, CV_RGB2GRAY);

    imwrite("Gray_Image.jpg",gray_image);

    return 0;
}

Here is its output when I execute it:

root@beaglebone:~# ./tryit lena.jpg 
Could not open or find the image

I tried to directly use the address of the image ("/home/root/lena.jpg") instead of argv[1] but nothing changed.

What can be the problem?

ps: I am cross-compiling this OpenCV program and then running it on my BeagleBone which has Angstrom Linux installed on it. Can this problem related to it?

Amadeus
  • 352
  • 2
  • 8
  • 16
  • It's possible that your Angstrom Linux installation has an OpenCV setup that doesn't support JPEG files. Can you use something like 'fopen' to ensure that you have access to the file, and also look for the JPEG libraries on your BeagleBone? – Rajesh J Advani Jul 26 '12 at 10:17
  • @RajeshJAdvani I opened it with fopen and checked if it is NULL, it is not. So I do not have any problems accessing to file. By the way, I do not have an OpenCV setup on my Angstrom Linux. I cross-compile the programs on my PC by using the OpenCV libraries which are also cross compiled for ARM Cortex A8, and then execute them on my beaglebone. – Amadeus Jul 26 '12 at 12:29
  • @RajeshJAdvani I started to think that the problem is related to my OpenCV libraries. Maybe I could not cross-compile them successfully. If it is the problem, I can figure it out by doing the following: I can send an array of bytes instead of an image, and let OpenCV process it. Do you have an idea how can I convert a picture to an array of bytes? – Amadeus Jul 26 '12 at 12:57
  • do a test like this, create a new image, allocate the memory and then save that image. – alinoz Jul 26 '12 at 14:57
  • Does your BeagleBone setup have the libjpeg* set of libraries? – Rajesh J Advani Jul 27 '12 at 06:12
  • @RajeshJAdvani I do not have any libjpeg libraries under "root@beaglebone:/lib" But under "root@beaglebone:/usr/lib#" I have libjpeg.la, libjpeg.so, libjpeg.so.8, libjpeg.so.8.0.2. Do you think that they might be corrupted? – Amadeus Jul 27 '12 at 11:54
  • @alinoz I could not understand your point. – Amadeus Jul 27 '12 at 11:55
  • On the BeagleBone, use ldd to check what libraries your executable is linked to. Also use ldconfig to check whether picking your libraries from /usr/lib are in the cache. – Rajesh J Advani Jul 30 '12 at 10:33

2 Answers2

1

I solved my problem by deleting existing OpenCV libraries on my angstrom image and replacing them with the working ones.

Amadeus
  • 352
  • 2
  • 8
  • 16
0

Try saving the image with a png extension and then opening it. For some reason, files with a png extension work better than other extensions like jpg/gif.

Prasanth Louis
  • 4,658
  • 2
  • 34
  • 47