So Tired !_! 逆水行舟, 不进则退!



Posted by Nick Xu

1. 去 下载最新版OpenCV for Linux/Mac源文件,目前版本是2.4.3。下载后解压。

2. 去 下载最新版cmake,.dmg文件,建议使用2.8.12.2版本。下载后安装。
3. 接下来开始安装opencv(注: 如果需要启用多核支持, 可以先按第6点进行修改后再编译,未经测试)

开一个终端,进入opencv解压缩的目录,如cd 然后把文件夹拽进去。
mkdir release
cd ./release
sudo make install
4. 至此opencv应该已经装上了。
 注:如果cmake出现错误,请到苹果开发者网站那里下载Command Line Tools安装。(关键)

由于本人是要写python,所以还要进行下一步配置。本人mac是rmbp15,自带的python版本是2.7,终端下输入python后输入import cv,如果提示没有找到cv,请按如下步骤操作。
1. 打开一个终端,cd ~
2. vim .profile
3. 添加export PYTHONPATH=/usr/local/lib/python2.7/site-packages/,其中python2.7根据个人python版本而定。
4. source .profile
5. 至此应该全部完成了。


I got this working with OpenCV 2.4.10 and GCC 4.9.2. I also had to make the following additional changes to the CMakeLists.txt in the root OpenCV directory:

OCV_OPTION(WITH_OPENEXR “Include ILM support via OpenEXR” OFF)
OCV_OPTION(WITH_OPENMP “Include OpenMP support” ON)
OCV_OPTION(BUILD_OPENEXR “Build openexr from source” OFF )

I originally was running into errors with “Undefined symbols for architecture x86_64 cv::imshow, cv::imread, cv::imwrite, etc,” because Homebrew installed a clang OpenCV whereas my project was using GCC. One solution would be to rebuild everything with clang but that was not appealing in my case because I wanted OpenMP support. So I instead used your solution to build a GCC version of OpenCV.



Posted by Nick Xu






Filed under: opencv, Python, 其它 No Comments

Mac OS X OpenCV Port

Posted by Nick Xu

This page is devoted to information specific to the OS X build of OpenCV. Things are changing quickly due to recent work by Willow Garage, and the transition to the CMake build system.

There are a number of ways to install OpenCV on OS X. The most straightforward way is to download the latest version of OpenCV (currently 2.2.0), and use cmake as described in the general InstallGuide and below at number 2.

Each of the numbers below are separate ways to install OpenCV for Mac OS X.


0. Getting a precompiled version ( sorry, but this is only for OpenCV 1.2 and only valid only for i386, Not 2.x - anyone know how to build a Private Framework for 2.x ? ):



open OpenCV-Private-Framework-1.2.dmg
cd /Volumes/OpenCV\ Private\ Framework/
cp -R OpenCV.framework/ /Library/Frameworks/OpenCV.framework


1. Build via MacPorts

For Snow Leopard users, you can build OpenCV 2.1.0 and above via MacPorts as a 64-bit library with support of QTKit video I/O and Cocoa GUI.

Assuming MacPorts installed, first use:


sudo port selfupdate

to update the MacPorts base and the port tree. Then, simply:


sudo port install opencv

to build OpenCV 2.1.0 in 64-bit.

If you encounter errors, try installing its dependents with +universal whenever possible.


sudo port -v install opencv +python26

to use with Python 2.6 bindings or


sudo port -v install opencv +python27

to use with Python 2.7 bindings

By default OpenCV is installed to /opt/local/ according to MacPorts conventions. The opencv_xxxx utilities are located in /opt/local/bin/, the dynamic libraries in /opt/local/lib/ and the header files in/opt/local/include/opencv/.

To use the libraries, include the required files with standard includes. For example:


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

If you use Xcode, the instructions are similar to the ones in method 3. It only differs that /usr/local becomes /opt/local.


1.1 Build via HomeBrew

For users that have and like homebrew:


sudo brew update
sudo brew install opencv

If you need the 32bit version, change the last command to sudo brew install opencv --build32


Assuming HomeBrew ( aka 'brew') is installed first:


sudo brew update
sudo brew install lame       ( for libmp3lame that opencv needs)
sudo brew install libpng     ( which theora needs -  part of the ffmpeg package )
sudo brew link    libpng     ( needed if theora/ffmpeg can't find it)
sudo brew install ffmpeg     ( for libavcodec and related stuff that opencv needs)
sudo brew install python     ( the system python is not good enough)
sudo brew install doxygen    ( if you compile opencv yourself, you'll want this)
sudo chmod 000 /System/Library/Frameworks/Python.framework/
sudo chmod 000 /Library/Frameworks/Python.framework/
   ( disable system python/s temporarily just for the next step)

sudo brew install opencv     ( finally! - this will probably bork? )

svn co cd opencv ( or get sources by hand)
cd opencv
ccmake .   ( disable everything you can that's not important, maybe you'll figure out how get it to build? don't forget to press 't' for advanced mode.  Edit the settings as you desire, then 'c' and 'g' )
cmake .
make -j8
make install ( optional )

sudo chmod 755 /System/Library/Frameworks/Python.framework/
sudo chmod 755 /Library/Frameworks/Python.framework/
   ( put it back how it was )

By default OpenCV is installed to /opt/homebrew/ 

* see also:

See MacPort install for any other details, just replace /opt/local with /opt/homebrew


2. Build from source using the CMake build system (new)

The GNU autotools methods (configure, make, make install) are being obsoleted. OpenCV has now switched to the CMake build system.


Getting the source code

Make sure you have subversion installed (built-in since Snow Leopard). If MacPorts is your primary package manager, use:


sudo port install subversion

if not using MacPorts or it gives an error, try:


sudo fink install svn-ssl

Make sure you have cmake installed. This can also be installed with MacPorts:


sudo port install cmake

Check out the source code into a clean directory and cd into the source directory after the download is complete:


# previous URL
# svn co ; cd opencv
# current (April 2012) URL
svn co ; cd opencv
  • Note: There's a "atomic bomb commit" at r3060 which largely changed the OpenCV directory structure. If you wish to work with the older structure, you can specify an earlier checkout revision:


# previous URL
# svn co -r 3059 ; cd opencv
# current (April 2012) URL
svn co -r 3059 ; cd opencv


Use CMake to build

To use CMake to generate Unix Makefiles, type:


# make a separate directory for building
mkdir build
cd build
cmake -G "Unix Makefiles" ..

You can specify many configuration options in the above command. For examples:

  • -D BUILD_TESTS=OFF -> if you don't want to build tests.
  • -D BUILD_NEW_PYTHON_SUPPORT=OFF -> if you don't want the Python interface support.
  • -D WITH_CARBON=ON -> (mainly for Leopard and below) if you want to use Carbon for UI instead of Cocoa (since r2909).
  • -D WITH_QUICKTIME=ON -> (mainly for Leopard and below) if you want to use QuickTime for Video I/O insted of QTKit (since r2924). If you're using Snow Leopard and do need Carbon and QuickTime, you must also specify -D CMAKE_OSX_ARCHITECTURES=i386-D CMAKE_C_FLAGS=-m32 and -D CMAKE_CXX_FLAGS=-m32.
  • An example configuration of OpenCV 2.2.0 on Mac OSX 10.6 without TPP or Eigen or Carbon is shown at

Finally, follow the usual "make, make install" method:


make -j8
sudo make install

append a new line "export PYTHONPATH=/usr/local/lib/python2.7/site-packages/" to ~/.profile:

If you need the CMake GUI for other options, replace cmake with ccmake. If the arrow keys fail, you can use the following commands instead:


ctrl + N : next option
ctrl + P : previous options
ctrl + D : down one page
ctrl + U : up one page

To compile with Intel Threading Building blocks, use ccmake (the Cmake GUI) to specify the lib and include paths to your installation of TBB.

After setting the build options, hit 'c' to configure. Hit 'g' to generate the config file and exit ccmake. Then execute the "make, make install" commands above.


if got  “Segmentation fault” during “import cv”:

In ccmake(ccmake -G "Unix Makefiles" .), toggle to the advanced mode pressing t.

change PYTHON_LIBRARY value to


Note that the specific path depends, of course, what version of Python you are using (2.6 or 2.7 etc.). Also, if you are using the system Python, instead of MacPorts (which is in /opt), then you probably want PYTHON_LIBRARY to be /usr/lib/libpython2.7.dylib



3. Building as an Xcode Framework using the script (obsoleted)

Note: the CMake build system should ultimately make this obsolete, one may remove this section when the CMake building is mature on the Mac. By the way, the script has been removed since revision 2528.The last revision where successfully builds, as I tested, is r2492.

OpenCV can now be built as a Mac OS X Framework useable with XCode. Just invoke the './' script and you will obtain a Universal Framework named "OpenCV.framework" meant as a Private Framework.

Additionally, a pre-built version (universal binary Private Framework, again together with a demo application) is available. See the PrivateFramework wiki page or directly download it from .

A more recent version of the framework can be found here or downloaded directly from

OpenCV 2.0 Universal Framework (tested on ppc and i386 Leopard and Snow Leopard): Here is an universal Framework for Mac ppc/i386 built from the OpenCV 2.0 released in September09. By the way, I also fixed some bugs and add some improvements:


-> a bug on Mac (carbon) that prevented cvCloseWindow/cvCloseAllWindow from working. -> a bug when 2 cameras capture images simultaneously (black bands) (see picture bellow) -> The trackbar now displays its current value (see picture bellow) and is resized according to its name.

Those fix/improvements are added to this framework.


4. Building the old UNIX way

This was removed since it no longer works.


Other Resources



  • [get | view] (2006-05-30 14:17:41, 6.2 KB) [[attachment:export.cpp]]


Collected Information


Compiling Universal Binaries

The --enable-universal-build switch has been removed again since it doesn't allow building on intel machines ... You can still try out what it did by configuring with:


./configure CFLAGS="-arch i386 -arch ppc" CXXFLAGS="-arch i386 -arch ppc" --disable-dependency-tracking

As an alternative, we now have a small build script that configures and builds twice (once for ppc, once for intel) and then merges the binaries with lipo. We can and will extend this to more architectures whenever Mac OS X 10.5 Leopard is released, since it finally comes with 64bit compilers for intel. In addition to those then natural 4 platforms (intel/ppc * 32/64), it might be nice to provide specially optimized variants for G5, G4, G3 (-mcpu=...) to maximize speed on older hardware. This however requires additional testing (and performance testing).

See also: Compiling for Multiple Architectures in

A (very old) list of supported quicktime codecs can be found at QuickTimeCodecs

Filed under: Mac OS, opencv No Comments

OS X Mountain Lion下Eclipse+opencv的C++开发编译环境配置

Posted by Nick Xu


我下载的是2.4.3 for Linux/Mac版本,编译环境需要cmake,可通过brew进来安装

cd OpenCV-2.4.3
mkdir build
cmake -G "Unix Makefiles" ..
make -j8
sudo make install






Tagged as: , , , , No Comments


Posted by Nick Xu

Filed under: opencv No Comments


Posted by Nick Xu


  原 理




θ=Num*Rop+Offset      (2)
程 序
class LaserRange
struct RangeResult * GetRange(IplImage * imgRange,IplImage * imgDst);
virtual ~LaserRange();
unsigned   int maxW;
unsigned   int maxH;
unsigned   int MaxPixel;
RangeResult * strctResult;
// Values used for calculating range from captured image data
const double gain; // Gain Constant used for converting pixel offset to angle in radians
 const double offset; // Offset Constant
 const double h_cm;  // Distance between center of camera and laser
unsigned int pixels_from_center; // Brightest pixel location from center

 void Preprocess(void * img,IplImage * imgTemp);

// Brightest pixel location from center

  strctResult=new RangeResult;strctResult->maxCol=0;

if(NULL!=strctResult) delete strctResult;
struct RangeResult * LaserRange::GetRange(IplImage * imgRange,IplImage * imgDst)
if(NULL==imgRange) return   strctResult;
pixels_from_center = abs(120-maxH);

 // Calculate range in cm based on bright pixel location, and setup specific constants
strctResult->Range= h_cm/tan(pixels_from_center * gain + offset);
return  strctResult;

void LaserRange::Preprocess(void *img, IplImage * imgTemp)
MaxPixel=0;                //处理下一帧前 最大像素值清零;
   IplImage* image = reinterpret_cast<IplImage*>(img);
cvCvtPixToPlane( image,0 ,0 ,imgTemp , 0);

for( int j=((imgTemp->width-60)/2-1); j<(imgTemp->width-40)/2+59; j++)
for(int i=5; i<imgTemp->height-5; i++)

if( ((imgTemp->imageData[(i-1)*imgTemp->widthStep+j])>MaxPixel) && ((imgTemp->imageData[(i-1)*imgTemp->widthStep+j+1])>MaxPixel) &&((imgTemp->imageData[(i-1)*imgTemp->widthStep+j-1])>MaxPixel)   )
if( ((imgTemp->imageData[(i+1)*imgTemp->widthStep+j])>MaxPixel) && ((imgTemp->imageData[(i+1)*imgTemp->widthStep+j+1])>MaxPixel) &&((imgTemp->imageData[(i+1)*imgTemp->widthStep+j-1])>MaxPixel)   )
MaxPixel=imgTemp->imageData[i*imgTemp->widthStep+j] ;


int CLaserVisionDlg::CaptureImage()
// CvCapture* capture = 0;
// capture = cvCaptureFromCAM(0);  //0表示设备号

    if( !capture )
fprintf(stderr,"Could not initialize capturing.../n");
return -1;
 // cvNamedWindow( "LaserRangeImage", 1 );
// cvvNamedWindow( "image", 1);
   cvvNamedWindow( "Dimage", 1);

IplImage* frame = 0;

if(isStop) break;

        frame = cvQueryFrame( capture ); //从摄像头抓取一副图像框架
        if( !frame )
       if( !imgOrign )
 //allocate all the buffers
            imgOrign = cvCreateImage( cvGetSize(frame), 8, 3 );    //创建一副图像
            imgOrign->origin = frame->origin;

        cvCopy( frame, imgOrign, 0 );  //将图frame复制到image
imgDest=cvCreateImage( cvSize( imgOrign->width,imgOrign->height),8,1);
cvZero( imgDest );
struct RangeResult * temp= laservsion.GetRange(imgOrign,imgDest);

        cvLine( imgOrign,cvPoint(temp->maxCol,0), cvPoint(temp->maxCol,imgOrign->height),cvScalar(100,100,255,0),1,8,0);
cvLine( imgOrign,cvPoint(0,temp->maxRow), cvPoint(imgOrign->width,temp->maxRow),cvScalar(100,100,255,0),1,8,0);

  // cvvShowImage( "image", imgOrign);
  cvSaveImage("image.bmp", imgOrign);
cvvShowImage( "Dimage", imgDest);

  CDC* pDC = GetDlgItem(IDC_Picture)->GetDC();
CDC dcmemory;
CBitmap* pBmp;
CString szFileName = "image.bmp";
pBmp->GetObject(sizeof(BITMAP), &bm);
pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcmemory, 0, 0, SRCCOPY);

    }char str[80];         // To print message
    CDC *pDCp= GetDC();
char str2[80];

 // Display frame coordinates as well as calculated range
       sprintf(str, "Pix Max Value=%d  At x= %u, y= %u, PixfromCent= %d",temp->maxPixel,temp->maxCol, temp->maxRow, temp->PixfromCent);
sprintf(str2, "Range= %f cm ",temp->Range);
pDCp->TextOut(30, 33, str);
pDCp->TextOut(50, 50, str2);

int  c = cvWaitKey(10);
//  if( c == 'q' )
//     break;

//cvReleaseCapture( &capture );
// cvDestroyWindow( "image");
 cvDestroyWindow( "Dimage");
return 0;
Filed under: opencv 1 Comment