Alex Rivera | Logout

Compiling a basic OpenCV program on Mac OS X 10.7

Asked 2011-12-06T02:33:21.847
11

I installed opencv from Macports and it is in /opt/local/include

I tried to compile basic OPENCV code from Terminal by giving the following commands, but it doesn't getting compiled:

g++ example.cpp -o example -I /usr/local/include/opencv/ -L /usr/local/lib/ -lopencv_highgui -lopencv_calib3d -lopencv_legacy
g++ example.cpp -o example -I /opt/local/include/opencv/ -L /opt/local/lib/ -lopencv_highgui -lopencv_calib3d -lopencv_legacy

Can anyone tell me the right Terminal command to compile given below opencv program for Mac OS X 10,7?

I was trying to compile the simple example given on this link: http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/

Edit:

Here is the code I am trying to compile:

////////////////////////////////////////////////////////////////////////
//
// hello-world.cpp
//
// This is a simple, introductory OpenCV program. The program reads an
// image from a file, inverts it, and displays the result. 
//
////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>


int main(int argc, char *argv[])
{
  IplImage* img = 0; 
  int height,width,step,channels;
  uchar *data;
  int i,j,k;

  if(argc<2){
    printf("Usage: main <image-file-name>\n\7");
    exit(0);
  }

  // load an image  
  img=cvLoadImage(argv[1]);
  if(!img){
    printf("Could not load image file: %s\n",argv[1]);
    exit(0);
  }

  // get the image data
  height    = img->height;
  width     = img->width;
  step      = img->widthStep;
  channels  = img->nChannels;
  data      = (uchar *)img->imageData;
  printf("Processing a %dx%d image with %d channels\n",height,width,channels); 

  // create a window
  cvNamedWindo
Edit
Report

1 Answer

7

Yay!!! This command worked:

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

I just needed to add the path of opencv.pc file into the .profile (or .bash_profile) which is:

export PKG_CONFIG_PATH=/opt/local/lib/pkgconfig

Now I am trying to compile file which uses

#include <opencv2/opencv.hpp> 

instead of

#include <cv.h>
#include <highgui.h>

I will reply soon if the same command works for this or not. Thanks a lot guys for all the comments.

answered 2011-12-06T16:04:14.063

Your Answer