opencv 标定 3D项目

来源:互联网 发布:红三兵炒股软件评价 编辑:程序博客网 时间:2024/05/22 16:23
// ZouStrereo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ZouStrereo.h"


#include "opencv2/contrib/contrib.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>

#include "CameraDS.h"
#include "Stereo.h"
#include "CvvImage.h"
//#include "MainWindow.h"

using namespace cv;


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;


int  Calibrate( int xC, int yC,double RectMM,int GroupImg)
{

 printf("标定开始\n\n");
 CStereo Stereo;

 int iXPtCount;
 int iYPtCount;
 float fSquareSize;

 FILE* file;
 file = fopen("初始化文件.txt","r");
 int temp=0;
 if(file!=NULL)
 {
  fscanf(file,"%d\n",&iXPtCount);
  
  fscanf(file,"%d\n",&iYPtCount);
  
  fscanf(file,"%d\n",&temp);
  fscanf(file,"%f\n",&fSquareSize);
  Stereo.SetPtCount(iXPtCount,iYPtCount);
  Stereo.SetSquareSize(fSquareSize);
  fclose(file);
 }else
 {

  iXPtCount = xC;
  iYPtCount = yC;
  temp = 0;
  fSquareSize = RectMM;
  Stereo.SetPtCount(iXPtCount,iYPtCount);
  Stereo.SetSquareSize(fSquareSize);

 }



 //int found = 0;
 //BOOL bInit = FALSE;
 IplImage* iplDst1 = NULL;
 IplImage* iplDst2 = NULL;
 CvSize imgSize;
 //CvvImage img1;
 //CvvImage img2;
 //HDC hDC = ::GetDC(hWnd);
 //CRect rect1;
 //CRect rect2;
 //::GetClientRect(hWnd,&rect1);
 //rect1.right = rect1.right/2;
 //::GetClientRect(hWnd,&rect2);
 //rect2.left = rect2.right/2;


 char* names_L[] = { "./L/LCameraCapture_1.jpg", "./L/LCameraCapture_2.jpg", "./L/LCameraCapture_3.jpg",
  "./L/LCameraCapture_4.jpg", "./L/LCameraCapture_5.jpg", "./L/LCameraCapture_6.jpg"};
 char* names_R[] = { "./R/RCameraCapture_1.jpg", "./R/RCameraCapture_2.jpg", "./R/RCameraCapture_3.jpg",
  "./R/RCameraCapture_4.jpg", "./R/RCameraCapture_5.jpg", "./R/RCameraCapture_6.jpg"};

 //CvSize ImageSize={640,480};
 //CvSize  CornerSize={5,7};         //棋盘角点的大小
 //int nbLines=CornerSize.height;             //行
 //int nbClos=CornerSize.width;    //列
 //int nbPoints=nbLines*nbClos;   //角点数量
 //int CornersFound;  //找到的角点数
 cvNamedWindow("L");
 cvNamedWindow("R");

 int PicNum = 6;
 for (int i=0;i<PicNum;i++)
 {
  //img=cvLoadImage(names_L[i]);
  //img1=cvCloneImage(img);

  printf("start--->image [%d]\n",i);
  Stereo.LoadImage(names_L[i],names_R[i]);

  if(Stereo.GetImageSize(imgSize))
  {
   iplDst1 = cvCreateImage(imgSize,8,3);
   iplDst2 = cvCreateImage(imgSize,8,3);
   //bInit = TRUE;
  }
  if(Stereo.FindChessboardCorners(iplDst1, iplDst2))
  {
   //img1.CopyOf(iplDst1);
   //img2.CopyOf(iplDst2);
   cvShowImage("L", iplDst1);
   cvShowImage("R", iplDst2);
   
   cvWaitKey(1000);
   
  }
  printf("end--->image [%d]\n \n",i);
  
 }
 if(Stereo.GetFound()==0)
  return -1;
 Stereo.StereoCalibrate( );
 Stereo.UndistortRectifyMap( temp );
 Stereo.StereoBMState( );
 Stereo.CalDistance( );
 Stereo.CalAccuracy();
 Stereo.Save( );
 cvReleaseImage(&iplDst1);
 cvReleaseImage(&iplDst2);
 cvDestroyAllWindows();

 printf("标定结束\n");


 return 0;
}

int  CalDistance()
{


 int cam_count;
 cam_count = CCameraDS::CameraCount();//获取摄像头数目
 printf("There are %d cameras.\n", cam_count);//输出摄像头数目


 for(int i=0; i < cam_count; i++)            //获取所有摄像头名称并显示出来
 {
  char camera_name[1024];//声明1024个字符空间,1024字节
  int retval= CCameraDS::CameraName(i,camera_name,sizeof(camera_name) );
  //根据摄像头编号返回摄像头名字
  if(retval >0)
   printf("Camera #%d's Name is '%s'.\n", i, camera_name);
  else
   printf("Can not get Camera #%d's name.\n", i);
 }
 if(cam_count==0)//如果没有摄像头,返回-1
  return -1;


 CCameraDS camera0;//产生对象,打开第一个摄像头
 CCameraDS camera1;//产生对象,打开第二个摄像头
 //if(! camera.OpenCamera(0, true))
 //打开摄像头,指定摄像头0开始,参数2指示是否自动弹出摄像头属性页。
 if(! camera0.OpenCamera(0,false,640, 480 )) //false不弹出属性窗,用代码制定宽和高
 {
  fprintf(stderr, "Can not open camera 0.\n");//不成功,if(!0),执行代码
  return -1;
 }
 if(! camera1.OpenCamera(1,false,640, 480 ))
 {
  fprintf(stderr, "Can not open camera 1.\n");//不成功,if(!0),执行代码
  return -1;
 }


 cvNamedWindow("camera1-Left");//创建窗口1
 cvNamedWindow("camera2-Right");//创建窗口2

 CStereo Stereo;
 Stereo.Load( );
 CvSize imgSize;
 BOOL bInit = FALSE;
 IplImage* temp1 = NULL;
 IplImage* temp2 = NULL;
 IplImage* iplDst1 = NULL;
 IplImage* iplDst2 = NULL;
 //BOOL bInit = FALSE;

 while(1)
 {
  //获取一帧
  temp1 = camera0.QueryFrame();
  //cvWaitKey(1000);
  temp2 = camera1.QueryFrame();

  Stereo.LoadImage(temp1,temp2);

  if(!bInit)
  {
   Stereo.GetImageSize(imgSize);
   iplDst1 = cvCreateImage(imgSize,8,3);
   iplDst2 = cvCreateImage(imgSize,8,3);
   bInit =TRUE;
  }

  Stereo.StereoBMState(iplDst1,iplDst2 );
  //img1.CopyOf(iplDst1);
  //img2.CopyOf(iplDst2);
  //img1.DrawToHDC(hDC,Rect1);
  //img2.DrawToHDC(hDC,Rect2);
  Stereo.CalDistance( );

  //显示
  cvShowImage("camera2-Right", iplDst1);
  cvShowImage("camera1-Left", iplDst2);
  if (cvWaitKey(30) == 'q')//输入按键为q,break
   break;
 }


 camera0.CloseCamera();//可不调用此函数,CCameraDS析构时会自动关闭摄像头
 camera1.CloseCamera();//可不调用此函数,CCameraDS析构时会自动关闭摄像头
 //cvDestroyWindow("camera1");
 //cvDestroyWindow("camera2");
 cvDestroyAllWindows();
 return 0;
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
 int nRetCode = 0;

 // initialize MFC and print and error on failure
 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
 {
  // TODO: change error code to suit your needs
  _tprintf(_T("Fatal Error: MFC initialization failed\n"));
  nRetCode = 1;
 }
 else
 {
  // TODO: code your application's behavior here.

///////////////////////////////////////////////////////////////////////////////////
  int xchoose = 0;
  switch(xchoose)
  {

  case 0:
   {
     Calibrate( 5,7,40.0,6);

   }
   break;
  case 1:
   {

    int cam_count;
    cam_count = CCameraDS::CameraCount();//获取摄像头数目
    printf("There are %d cameras.\n", cam_count);//输出摄像头数目


    for(int i=0; i < cam_count; i++)            //获取所有摄像头名称并显示出来
    {
     char camera_name[1024];//声明1024个字符空间,1024字节
     int retval= CCameraDS::CameraName(i,camera_name,sizeof(camera_name) );
     //根据摄像头编号返回摄像头名字
     if(retval >0)
      printf("Camera #%d's Name is '%s'.\n", i, camera_name);
     else
      printf("Can not get Camera #%d's name.\n", i);
    }
    if(cam_count==0)//如果没有摄像头,返回-1
     return -1;


    CCameraDS camera0;//产生对象,打开第一个摄像头
    CCameraDS camera1;//产生对象,打开第二个摄像头
    //if(! camera.OpenCamera(0, true))
    //打开摄像头,指定摄像头0开始,参数2指示是否自动弹出摄像头属性页。
    if(! camera0.OpenCamera(0,false,640, 480 )) //false不弹出属性窗,用代码制定宽和高
    {
     fprintf(stderr, "Can not open camera 0.\n");//不成功,if(!0),执行代码
     return -1;
    }
    if(! camera1.OpenCamera(1,false,640, 480 ))
    {
     fprintf(stderr, "Can not open camera 1.\n");//不成功,if(!0),执行代码
     return -1;
    }


    cvNamedWindow("camera1-Left");//创建窗口1
    cvNamedWindow("camera2-Right");//创建窗口2
    while(1)
    {
     //获取一帧
     IplImage *pFrame0 = camera0.QueryFrame();
     //cvWaitKey(1000);
     IplImage *pFrame1 = camera1.QueryFrame();

     //显示
     cvShowImage("camera2-Right", pFrame0);
     cvShowImage("camera1-Left", pFrame1);
     if (cvWaitKey(20) == 'q')//输入按键为q,break
      break;
    }


    camera0.CloseCamera();//可不调用此函数,CCameraDS析构时会自动关闭摄像头
    camera1.CloseCamera();//可不调用此函数,CCameraDS析构时会自动关闭摄像头
    cvDestroyWindow("camera1");
    cvDestroyWindow("camera2");
    //return 0;

   }
   break;
  case 3:
   {
     CalDistance();
    
   }
   break;
  default:
   break;
  }
  
  

 }
 system("pause");
 return nRetCode;
}
   

opencv ubuntu makefile 3D项目

#
# (C) Copyleft 2011
# Late Lee from http://www.latelee.org
#
# A simple Makefile for *ONE* project(c or/and cpp file) in *ONE*  directory
#
# note:
# you can put head file(s) in 'include' directory, so it looks
# a little neat.
#
# usage: $ make
#        $ make debug=y
###############################################################################

# cross compile...
CROSS_COMPILE =

CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
AR = $(CROSS_COMPILE)ar

ARFLAGS = cr
RM = -rm -rf
MAKE = make

CFLAGS = -Wall
#debug = y

ifeq ($(debug), y)
CFLAGS += -g
else
CFLAGS += -O3 -s
endif

DEFS =

CFLAGS += $(DEFS)

LDFLAGS = $(LIBS)



OPENCV2_4_2_INCLUDE=/root/workspace/opencv/OpenCV-2.4.0/modules
OPENCV2_4_2_ROOT_PATH=/root/workspace/opencv/OpenCV-2.4.0
INCLUDE_OPENCV=\
 -I$(INCLUDE_DIR)\
 -I$(OPENCV2_4_2_INCLUDE)/contrib/include \
 -I$(OPENCV2_4_2_INCLUDE)/legacy/include \
 -I$(OPENCV2_4_2_INCLUDE)/calib3d/include \
 -I$(OPENCV2_4_2_INCLUDE)/features2d/include \
 -I$(OPENCV2_4_2_INCLUDE)/nonfree/include \
 -I$(OPENCV2_4_2_INCLUDE)/photo/include \
 -I$(OPENCV2_4_2_INCLUDE)/objdetect/include \
 -I$(OPENCV2_4_2_INCLUDE)/video/include \
 -I$(OPENCV2_4_2_INCLUDE)/ml/include \
 -I$(OPENCV2_4_2_INCLUDE)/highgui/include \
 -I$(OPENCV2_4_2_INCLUDE)/imgproc/include \
 -I$(OPENCV2_4_2_INCLUDE)/flann/include \
 -I$(OPENCV2_4_2_INCLUDE)/core/include\
 -I$(OPENCV2_4_2_ROOT_PATH)/include/opencv\
 -I$(OPENCV2_4_2_ROOT_PATH)/include/opencv2\

INCDIRS = ./

CFLAGS += -I$(INCDIRS)
CFLAGS += $(INCLUDE_OPENCV)

PLIB=~/workspace/opencv/opencvbuild/lib/


LOCAL_SHARED_LIBRARIES =\
   $(PLIB)libopencv_calib3d.so\
   $(PLIB)libopencv_core.so\
   $(PLIB)libopencv_contrib.so \
   $(PLIB)libopencv_features2d.so \
   $(PLIB)libopencv_flann.so \
   $(PLIB)libopencv_highgui.so\
   $(PLIB)libopencv_imgproc.so \
   $(PLIB)libopencv_legacy.so\
   $(PLIB)libopencv_ml.so \
   $(PLIB)libopencv_nonfree.so\
   $(PLIB)libopencv_objdetect.so \
   $(PLIB)libopencv_photo.so \
   $(PLIB)libopencv_stitching.so \
   $(PLIB)libopencv_video.so \
   $(PLIB)libopencv_videostab.so

LDFLAGS += -L$(LOCAL_SHARED_LIBRARIES)

# source file(s), including c file(s) cpp file(s)
# you can also use $(wildcard *.c), etc.
SRC_C   := $(wildcard *.c)
SRC_CPP := $(wildcard *.cpp)

# object file(s)
OBJ_C   := $(patsubst %.c,%.o,$(SRC_C))
OBJ_CPP := $(patsubst %.cpp,%.o,$(SRC_CPP))


# executable file
target = a.out

###############################################################################

all: $(target)

$(target): $(OBJ_C) $(OBJ_CPP)
 @echo "Generating executable file..." $(notdir $(target))
 @$(CXX) $(CFLAGS) $^ -o $(target) $(LDFLAGS)

# make all .c or .cpp
%.o: %.c
 @echo "Compling: " $(addsuffix .c, $(basename $(notdir $@)))
 @$(CC) $(CFLAGS) -c $< -o $@

%.o: %.cpp
 @echo "Compling: " $(addsuffix .cpp, $(basename $(notdir $@)))
 @$(CXX) $(CFLAGS) -c $< -o $@

clean:
 @echo "cleaning..."
 @$(RM) $(target)
 @$(RM) *.o *.back *~

.PHONY: all clean


0 0
原创粉丝点击