在Mac OS X 10.7上编译基本的OpenCV程序

时间:2011-12-06 02:33:22

标签: c macos opencv

我从Macports安装了opencv,它就在 的/ opt /本地/包括

我试图通过提供以下命令从Terminal编译基本的OPENCV代码,但它没有被编译:

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

任何人都可以告诉我在Mac OS X 10,7下面的opencv程序下编译正确的终端命令吗?

我正在尝试编译此链接上给出的简单示例: http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/

修改:

以下是我要编译的代码:

////////////////////////////////////////////////////////////////////////
//
// 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
  cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("mainWin", 100, 100);

  // invert the image
  for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
    data[i*step+j*channels+k]=255-data[i*step+j*channels+k];

  // show the image
  cvShowImage("mainWin", img );

  // wait for a key
  cvWaitKey(0);

  // release the image
  cvReleaseImage(&img );
  return 0;
}

以下是输出:

singhg@~/Programming/opencvTest $ g++ example.c -o example 'pkg-config --cflags --libs opencv'
i686-apple-darwin11-llvm-g++-4.2: pkg-config --cflags --libs opencv: No such file or directory
example.c:14:16: error: cv.h: No such file or directory
example.c:15:21: error: highgui.h: No such file or directory
example.c: In function ‘int main(int, char**)’:
example.c:21: error: ‘IplImage’ was not declared in this scope
example.c:21: error: ‘img’ was not declared in this scope
example.c:23: error: ‘uchar’ was not declared in this scope
example.c:23: error: ‘data’ was not declared in this scope
example.c:32: error: ‘cvLoadImage’ was not declared in this scope
example.c:43: error: expected primary-expression before ‘)’ token
example.c:43: error: expected `;' before ‘img’
example.c:47: error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope
example.c:47: error: ‘cvNamedWindow’ was not declared in this scope
example.c:48: error: ‘cvMoveWindow’ was not declared in this scope
example.c:55: error: ‘cvShowImage’ was not declared in this scope
example.c:58: error: ‘cvWaitKey’ was not declared in this scope
example.c:61: error: ‘cvReleaseImage’ was not declared in this scope

编辑[6Dec]:

singhg@~/Programming/opencvTest $ g++ example.cpp -o example `pkg-config --cflags --libs opencv`
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
i686-apple-darwin11-llvm-g++-4.2: example.cpp: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: no input files
singhg@~/Programming/opencvTest $ 

在我的/opt/local/lib我有:

singhg@/opt/local/lib $ ls *cv*
libopencv_calib3d.2.3.1.dylib       libopencv_highgui.dylib
libopencv_calib3d.2.3.dylib     libopencv_imgproc.2.3.1.dylib
libopencv_calib3d.dylib         libopencv_imgproc.2.3.dylib
libopencv_contrib.2.3.1.dylib       libopencv_imgproc.dylib
libopencv_contrib.2.3.dylib     libopencv_legacy.2.3.1.dylib
libopencv_contrib.dylib         libopencv_legacy.2.3.dylib
libopencv_core.2.3.1.dylib      libopencv_legacy.dylib
libopencv_core.2.3.dylib        libopencv_ml.2.3.1.dylib
libopencv_core.dylib            libopencv_ml.2.3.dylib
libopencv_features2d.2.3.1.dylib    libopencv_ml.dylib
libopencv_features2d.2.3.dylib      libopencv_objdetect.2.3.1.dylib
libopencv_features2d.dylib      libopencv_objdetect.2.3.dylib
libopencv_flann.2.3.1.dylib     libopencv_objdetect.dylib
libopencv_flann.2.3.dylib       libopencv_ts.2.3.1.dylib
libopencv_flann.dylib           libopencv_ts.2.3.dylib
libopencv_gpu.2.3.1.dylib       libopencv_ts.dylib
libopencv_gpu.2.3.dylib         libopencv_video.2.3.1.dylib
libopencv_gpu.dylib         libopencv_video.2.3.dylib
libopencv_highgui.2.3.1.dylib       libopencv_video.dylib
libopencv_highgui.2.3.dylib
singhg@/opt/local/lib $ 

现在我使用了这个命令:

   singhg@~/Programming/opencvTest $ g++ hello-world.cpp -o hello-world -I /opt/local/include/opencv/ -L /opt/local/lib  -lopencv_highgui -lopencv_core
In file included from hello-world.cpp:12:
/opt/local/include/opencv/cv.h:63:33: error: opencv2/core/core_c.h: No such file or directory
/opt/local/include/opencv/cv.h:64:33: error: opencv2/core/core.hpp: No such file or directory
/opt/local/include/opencv/cv.h:65:39: error: opencv2/imgproc/imgproc_c.h: No such file or directory
/opt/local/include/opencv/cv.h:66:39: error: opencv2/imgproc/imgproc.hpp: No such file or directory
/opt/local/include/opencv/cv.h:67:38: error: opencv2/video/tracking.hpp: No such file or directory
/opt/local/include/opencv/cv.h:68:45: error: opencv2/features2d/features2d.hpp: No such file or directory
/opt/local/include/opencv/cv.h:69:35: error: opencv2/flann/flann.hpp: No such file or directory
/opt/local/include/opencv/cv.h:70:39: error: opencv2/calib3d/calib3d.hpp: No such file or directory
/opt/local/include/opencv/cv.h:71:43: error: opencv2/objdetect/objdetect.hpp: No such file or directory
/opt/local/include/opencv/cv.h:72:37: error: opencv2/legacy/compat.hpp: No such file or directory
/opt/local/include/opencv/cv.h:79:37: error: opencv2/core/internal.hpp: No such file or directory
In file included from hello-world.cpp:13:
/opt/local/include/opencv/highgui.h:47:39: error: opencv2/highgui/highgui_c.h: No such file or directory
/opt/local/include/opencv/highgui.h:48:39: error: opencv2/highgui/highgui.hpp: No such file or directory
hello-world.cpp: In function ‘int main(int, char**)’:
hello-world.cpp:18: error: ‘IplImage’ was not declared in this scope
hello-world.cpp:18: error: ‘img’ was not declared in this scope
hello-world.cpp:20: error: ‘uchar’ was not declared in this scope
hello-world.cpp:20: error: ‘data’ was not declared in this scope
hello-world.cpp:29: error: ‘cvLoadImage’ was not declared in this scope
hello-world.cpp:40: error: expected primary-expression before ‘)’ token
hello-world.cpp:40: error: expected `;' before ‘img’
hello-world.cpp:44: error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope
hello-world.cpp:44: error: ‘cvNamedWindow’ was not declared in this scope
hello-world.cpp:45: error: ‘cvMoveWindow’ was not declared in this scope
hello-world.cpp:52: error: ‘cvShowImage’ was not declared in this scope
hello-world.cpp:55: error: ‘cvWaitKey’ was not declared in this scope
hello-world.cpp:58: error: ‘cvReleaseImage’ was not declared in this scope
singhg@~/Programming/opencvTest $ 

3 个答案:

答案 0 :(得分:18)

或使用 pkg-config 来帮助您填写路径和库:

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

答案 1 :(得分:7)

耶!!!这个命令有效:

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

我只需将opencv.pc文件的路径添加到.profile (or .bash_profile)即:

export PKG_CONFIG_PATH=/opt/local/lib/pkgconfig

现在我正在尝试编译使用

的文件
#include <opencv2/opencv.hpp> 

而不是

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

如果相同的命令适用于此,我会很快回复。 非常感谢所有评论。

答案 2 :(得分:1)

你很亲密。 cvReleaseImage位于opencv_core中。我是通过在http://opencv.itseez.com搜索cvReleaseImage来了解到这一点的。这对我有用:

g++ hello-world.cpp -o hello-world -I /usr/local/include/opencv -L /usr/local/lib  -lopencv_highgui -lopencv_core

如果您将/usr更改为/opt,那么它应该适合您。

修改

尝试在源代码中进行此更改:

#include "opencv.hpp"
//#include <cv.h>
//#include <highgui.h>

我认为问题源于您安装的教程代码和OpenCV 2.3之间的版本不匹配。他们更改了目录组织和库名称。