0

I'm new to openCV and to C/C++ in general. I'm working on this tutorial and using CMake to generate my makefiles. I have no problem building the first program (Read and Display), but when I run the executable, nothing happens and I must close out of my terminal because it just hangs. This happens even when I just use the following code:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
     cout << "start\n";
     Mat img = imread("MyPic.jpg");
     return 0;
}

When I delete Mat img line, "start" is outputted, but when I include it even that does not happen. MyPic.jpg is definitely in the same directory as the ReadDisplay.cpp and CMakeLists.txt. I am running Ubuntu 14.04. Finally, here is CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project( ReadDisplay )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( ReadDisplay ReadDisplay.cpp )
target_link_libraries( ReadDisplay ${OpenCV_LIBS} )

Any insights as to why this is causing issues?

Edit

The above code was to explain how I was debugging and what was causing the problem; I've already tried catching the failure by checking image size and other methods to isolate it as a system/configuration issue. As per this post, I'm rebuilding openCV from source to see if that will fix anything.

Community
  • 1
  • 1
Chandler Squires
  • 397
  • 3
  • 7
  • 22
  • Have you tried it with another image? – David Marquant Jan 08 '16 at 23:05
  • can you change to cout << "start" << std::endl; ? you have to flush, otherwise you arent sure about when the output will be printed. – Micka Jan 09 '16 at 09:21
  • Possible duplicate of [OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code](http://stackoverflow.com/questions/31341845/opencv-error-assertion-failed-size-width0-size-height0-simple-code) – Miki Jan 09 '16 at 15:28
  • Tried it with another image, tried it with `<< endl` added to the `cout` line, and I'm almost positive it's not that the image is empty, but looking into attempting to display with a different library to see if it's just some sort of access or other system issue. – Chandler Squires Jan 11 '16 at 14:41

2 Answers2

3

First of all you need to make sure the problem is finding the image, so please do not skip this part of the code :

if (img.empty()) //check whether the image is loaded or not
  {
      cout << "Error : Image cannot be loaded..!!" << endl;
      //system("pause"); //wait for a key press
      return -1;
  }

so if the problem was with finding the image , to avoid confusion you can pass the absolute path like : /home/user/my_pic.jpg

Elmira
  • 296
  • 2
  • 5
  • Even when using the full path name, or even a path to an image that doesn't exist, it fails the same way. – Chandler Squires Jan 08 '16 at 21:49
  • Did you add the code I've suggested? Does it output the error? – Elmira Jan 08 '16 at 22:08
  • Yes, I've tried it with that code and no error is output – Chandler Squires Jan 08 '16 at 22:12
  • Even more important, the only visible thing that code does is outputting the "start\n". so please set a getchar() or system("pause") after Mat img = imread("MyPic.jpg"); and recompile your code. I think you'll see the start output but obviously you won't see any image. – Elmira Jan 08 '16 at 22:12
  • Good debugging tip, thanks! This is weird: not only is "start" not output if I put a `system("pause")` after `Mat img = imread("MyPic.jpg");`, it's not even output if I put it immediately after the `cout` line. However, if I comment everything except except the `cout` line, I do get the output. – Chandler Squires Jan 08 '16 at 22:20
  • I cannot reproduce your error on Mac. Going to test it on linux and get back to you soon. – Elmira Jan 11 '16 at 16:37
  • Ok , I couldn't reproduce this error on Linux as well. So please do following steps and get back to me : 1 - use `pkg-config` to make sure you have opencv correctly installed on your computer. 2 - Try to use g++ directly to make the binary instead of the make file. You can do it by using following cmd `g++ ReadDisplay.cpp -o app \`pkg-config --cflags --libs opencv\` ` – Elmira Jan 12 '16 at 19:27
  • That works great! For those who are interested, you can also build it from Sublime by adding this build file (if you need to edit it later, it will be located at ~/.config/sublime-text-3/Packages/User): ``{ "shell_cmd": "g++ -std=c++0x ${file} -o ${file_base_name} `pkg-config --cflags --libs opencv` && ./${file_base_name}" } `` – Chandler Squires Jan 12 '16 at 19:51
  • Then to address the original issue it is something related to your makefile, try to check if it can find Opencv you can add this condition to your makefile (after find_package(OpenCV))to check if it finds opencv `if (OpenCV_FOUND) include_directories(${OpenCV_INCLUDE_DIRS})`, try to fix the makefile anyway so it will be way easier using make file to compile larger projects, Good luck! – Elmira Jan 12 '16 at 20:36
0

Not a full answer, this is just a note.

"MyPic.jpg" needs to be in the same directory where your binary/program runs, has nothing to do with your implementation file (.cpp) at compile time.

To let cmake automatically copy the file from your source directory to the build directory add the following to your CMakeLists.txt:

configure_file(MyPic.jpg MyPic.jpg COPYONLY)
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Nacho
  • 1,104
  • 1
  • 13
  • 30
  • That's good to know for the future, as of now I had the binary in the same folder anyways but I probably would have missed that detail on a larger project. That being said, the problem is stil there. – Chandler Squires Jan 08 '16 at 20:51