1

I recently installed opencv on Ubuntu 12.04.5 from the repository using this command.

sudo apt-get install libopencv-dev python-opencv

When I try to run the following code to confirm that it works properly I get an Illegal instruction (it compiled fine).

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

using namespace std;

int main(){
    cv::Mat img;
    img = cv::imread("RO.ppm");
    cout << img.size() << endl;
    return 0;
 }

I compiled using this command (due to undefined reference errors).

g++ -o test test.cpp $(pkg-config opencv --cflags --libs)

Update: Commenting out the cout line does not change the result and I've triple checked that RO.ppm exists in this directory (even if it didn't imread doesn't throw an error with illegal or not found input in my experience). I guess my question is two-fold what causes illegal instruction errors and how do I fix it?

tkausl
  • 13,686
  • 2
  • 33
  • 50
Jarl Zarl
  • 55
  • 1
  • 9

1 Answers1

1

You can't cout cv::Size directly without overloading '<<' operator for cv::Size. Instead you can get rows and columns from cv::Size and multiply them in order to get total size of the image:

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

using namespace std;

int main(){
    cv::Mat img;
    img = cv::imread("RO.ppm");
    cv::Size img_size = img.size();

    int cols =  img_size.width;
    int rows =  img_size.height;
    cout << "image size: " << rows*cols << endl;


    return 0;
 }

See this similar post for usage of cv::Size.

Community
  • 1
  • 1
  • Commenting out the cout line still results in the Illegal Instruction, also based on my experience in c++ wouldn't I have gotten a more indepth error message explaining the type error and where it occurred? – Jarl Zarl Jul 02 '15 at 12:45
  • It compiles safely on my PC when commenting cout line. Also your code gives a compile error when i tried: test.cpp:12:22: error: no match for ‘operator<<’ in ‘std::cout << cv::Mat::MSize::operator()() const()’ – Yasin Yildirim Jul 02 '15 at 13:29
  • If you get error at runtime, it might be related to input 'ppm' image format. Did you try it with more common formats like jpeg or png? – Yasin Yildirim Jul 02 '15 at 14:14