So Tired!_! for our good life, must to do something. let's do it!

9Dec/12

基于opencv的测量实验结果

Posted by Nick Xu

 

画出来的框只是为了方便查看和调试,实际结果是背后那一堆字符

物体左上角的位置在15.75mm左右宽度,右上角的位置在104.81mm左右的宽度

物体左上角的位置在7.12mm左右高度,左上角的位置在56.17mm左右高度

实际偏差还没有具体的比对,暂时没有误差数据出来...

Filed under: opencv, Python, 其它 No Comments
1Dec/12

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 ? ):

see: http://www.ient.rwth-aachen.de/cms/software/opencv/

 

wget http://www.ient.rwth-aachen.de/~asbach/OpenCV-Private-Framework-1.2.dmg
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

OLD METHOD:

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)

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

or:
svn co https://code.ros.org/svn/opencv/trunk/opencv 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: 

   http://www.tsd.net.au/blog/opencv-python-bindings-macports

   http://wiki.nuigroup.com/Installing_OpenCV_on_Mac_OS_X

   https://github.com/mxcl/homebrew/issues/issue/1792

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 https://code.ros.org/svn/opencv/trunk/opencv ; cd opencv
# current (April 2012) URL
svn co http://code.opencv.org/svn/opencv/trunk/opencv ; 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 https://code.ros.org/svn/opencv/trunk/opencv -r 3059 ; cd opencv
# current (April 2012) URL
svn co http://code.opencv.org/svn/opencv/trunk/opencv -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 http://imagebin.ca/view/A2uboY.html.

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
/opt/local/lib/libpython2.7.dylib

and PYTHON_INCLUDE_DIR to
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7

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 make_frameworks.sh 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 make_framework.sh script has been removed since revision 2528.The last revision where make_framework.sh successfully builds, as I tested, is r2492.

OpenCV can now be built as a Mac OS X Framework useable with XCode. Just invoke the './make_frameworks.sh' 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 http://www.ient.rwth-aachen.de/~asbach/OpenCV-Private-Framework-1.1.dmg .

A more recent version of the framework can be found here http://www.ient.rwth-aachen.de/cms/software/opencv/ or downloaded directly from http://www.ient.rwth-aachen.de/~asbach/OpenCV-Private-Framework-1.2.dmg

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.

http://nick.workao.org/wp-content/uploads/remote_image/2012/12/095445lne.png http://nick.workao.org/wp-content/uploads/remote_image/2012/12/095446Nn3.png

 

4. Building the old UNIX way

This was removed since it no longer works.

 

Other Resources

Links:

Attachments:

  • [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 http://developer.apple.com/documentation/Porting/Conceptual/PortingUnix/

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

Filed under: Mac OS, opencv No Comments
9Nov/12

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

Posted by Nick Xu

1.下载源码编译安装opencv

http://opencv.org/

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

1
2
3
4
5
cd OpenCV-2.4.3
mkdir build
cmake -G "Unix Makefiles" ..
make -j8
sudo make install

opencv的库到此已经安装完成,接下来开始配置eclipse

新建一个c++的空白项目,从opencv源代码的samples/cpp目录下复制drawing.cpp到项目里,然后试一下编译,看看会不会人品爆发编译成功:)

先看一下我的项目结构,main.cpp是我自己把文件名改了,无所谓了

选中项目,按command+i键,进入项目属性对话框

设置完毕后应该可以编译了,还有错误的话根据错误提示慢慢找原因吧。

Tagged as: , , , , No Comments
6Nov/12

opencv识别物体,初步实验结果

Posted by Nick Xu

Filed under: opencv No Comments
6Nov/12

【转】如何用摄像头来测距(opencv)

Posted by Nick Xu

       最近一直忙着找工作,blog都长草了,今天把以前作的一个东西放上来充充门面吧。记得在哪看到过老外做的这个东西,觉得很好玩,就自己也做了一个。在摄像头下面固定一个激光笔,就构成了这个简易的测距装置。看一下图吧。

  原 理

  假设激光束是与摄像头的光轴完全平行,激光束的中心落点在在摄像头的视域中是最亮的点。激光束照射到摄像头视域中的跟踪目标上,那么摄像头可以捕捉到这个点,通过简单的图像处理的方法,可以在这侦图像中找到激光束照射形成的最亮点,同时可以计算出Y轴上方向上从落点到图像中心的象素的个数。这个落点越接近图像的中心,被测物体距离机器人就越远。由下图图可以计算距离D:

(1)

  等式中h是一个常量,是摄像头与激光发射器之间的垂直距离,可以直接测量获得。

θ可通过下式计算:
θ=Num*Rop+Offset      (2)
其中:Num是从图像中心到落点的像素个数
Rop是每个像素的弧度值
Offset是弧度误差
合并以上等式可以得到:
(3)
Num可以从图像上计算得到。Rop和Offset需要通过实验计算获得。首先测量出D的准确值,然后根据等式(1)可以计算出准确的θ,根据等式(2)可到只含有参数Rop和Offset的方程。在不同的距离多次测量D的准确值计算θ,求解方程组可以求出Rop和Offset。这里Rop=0.0030354,Offset=0.056514344。
程 序
头文件:
class LaserRange
{
public:
struct RangeResult * GetRange(IplImage * imgRange,IplImage * imgDst);
LaserRange();
virtual ~LaserRange();
private:
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);
};
cpp文件:
LaserRange::LaserRange():gain(0.0030354),offset(0),h_cm(4.542)
{
maxW=0;
maxH=0;
MaxPixel=0;
pixels_from_center=0;

// Brightest pixel location from center

  strctResult=new RangeResult;strctResult->maxCol=0;
strctResult->maxRow=0;
strctResult->maxPixel=0;
strctResult->Range=0.0;
}

LaserRange::~LaserRange()
{
if(NULL!=strctResult) delete strctResult;
}
struct RangeResult * LaserRange::GetRange(IplImage * imgRange,IplImage * imgDst)
{
if(NULL==imgRange) return   strctResult;
Preprocess(imgRange,imgDst);
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);
strctResult->PixfromCent=pixels_from_center;
strctResult->maxCol=maxW;
strctResult->maxRow=maxH;
strctResult->maxPixel=MaxPixel;
//strctResult->Range=0.0;
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*imgTemp->widthStep+j])>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)   )
{
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*(imgTemp->widthStep)+j+1])>MaxPixel)
{
if((imgTemp->imageData[i*(imgTemp->widthStep)+j-1])>MaxPixel)
{
MaxPixel=imgTemp->imageData[i*imgTemp->widthStep+j] ;
maxW=j;
maxH=i;
}
}
}
}
}
}

}

调用函数:
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);

for(;;)
{
IplImage* frame = 0;

if(isStop) break;

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

        cvCopy( frame, imgOrign, 0 );  //将图frame复制到image
  //cvShowImage("LaserRangeImage",imgOrign);
if(!imgDest)
{
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);

 //在PictureBox上显示图片
  CDC* pDC = GetDlgItem(IDC_Picture)->GetDC();
CDC dcmemory;
BITMAP bm;
dcmemory.CreateCompatibleDC(pDC);
CBitmap* pBmp;
CString szFileName = "image.bmp";
HBITMAP hBk = (HBITMAP)::LoadImage(NULL,szFileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(NULL!=hBk)
{
pBmp=CBitmap::FromHandle(hBk);
pBmp->GetObject(sizeof(BITMAP), &bm);
dcmemory.SelectObject(pBmp);
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);
ReleaseDC(pDCp);

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

 }
//cvReleaseCapture( &capture );
//cvDestroyWindow("LaserRangeImage");
// cvDestroyWindow( "image");
 cvDestroyWindow( "Dimage");
return 0;
}
原文:http://blog.csdn.net/xylary/article/details/1843809
Filed under: opencv 1 Comment
   
site
site