立體視覺影象對,獲取與儲存

LZEP2015發表於2014-12-07

利用DirectShow獲取雙目攝像頭得到的棋盤影象,按s鍵進行儲存:

#include <cstdlib>
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include"camerads.h"
const char* win_name="OpenCV";
CCameraDS m_CamDS[2];
IplImage *pFrame1,*pFrame2;
IplImage* Frame,*Rectify_Frame1,*Rectify_Frame2;
CvFont font;
void DrawText(CvRect* roi,char* s,int i)
{
	cvSetImageROI(Frame,roi[i]);
	switch(i)
	{
	 case 0:  cvCopy(pFrame1,Frame);  break;
	 case 1:  cvCopy(pFrame2,Frame); break;
	 case 2:  cvCopy(Rectify_Frame1,Frame); break;
	 case 3:  cvCopy(Rectify_Frame2,Frame); break;
    }
    cvResetImageROI(Frame);
    switch(i)
	{
	 case 0:  cvPutText(Frame,s,cvPoint(130,315),&font,CV_RGB(0,0,255));  break;
	 case 1:  cvPutText(Frame,s,cvPoint(70,655),&font,CV_RGB(0,0,255)); break;
	 case 2:  cvPutText(Frame,s,cvPoint(550,315),&font,CV_RGB(0,0,255)); break;
	 case 3:  cvPutText(Frame,s,cvPoint(500,655),&font,CV_RGB(0,0,255)); break;
    }
}
bool Camera_Info()
{
	if(IsWindowVisible(FindWindow(NULL,win_name)));

	//獲取攝像頭數目
	int m_iCamCount=CCameraDS::CameraCount();
	printf("There are %d camera here!\n",m_iCamCount);

	if(m_iCamCount!=2)
	{
		return false;
	}


	char m_CamName1[30],m_CamName2[30];
	int ret1=m_CamDS[0].CameraName(0,m_CamName1,sizeof(m_CamName1));
	int ret2=m_CamDS[1].CameraName(0,m_CamName2,sizeof(m_CamName2));
	if(ret1>0&&ret2>0)
	{
		printf("Camera1 name is %s,\nCamera2 name is %s\n",m_CamName1,m_CamName2);
	}
	else
	{
		printf("Can not get two camera name\n");
		return false;
	}
    
    if((! m_CamDS[0].OpenCamera(0, false, 320, 240)) || ((pFrame1 = m_CamDS[0].QueryFrame()) == NULL))
	{
			printf("Cannot open camera 2\n");
			return false;
	}
	
	if((! m_CamDS[1].OpenCamera(1, false, 320, 240)) || ((pFrame2 = m_CamDS[1].QueryFrame()) == NULL))
	{
			printf("Cannot open camera 2\n");
			return false;
	}
		
    printf("Two Camera is already Initialize\n");

    return true;
}
int main(void)
{
	//Get Camera Info
    bool ret=Camera_Info();
    if(!ret)
    {
    	printf("Failed to get camera Information\n");
    	return false;
    }
	
	//Draw the Image Into GUI
	char* s[4]={"camera1","camera1_rectify","camera2","camera2_rectify"};
	CvRect roi[4];
	roi[0]=cvRect(50,50,320,240);
	roi[1]=cvRect(470,50,320,240);
	roi[2]=cvRect(50,390,320,240);
	roi[3]=cvRect(470,390,320,240);
    
	cvInitFont(&font,CV_FONT_HERSHEY_COMPLEX,1.0,1.0,0,1,8);

	bool isFirst=true;
	int width,height;
    
    cvNamedWindow(win_name);
    char c;
	char path1[20],path2[20];
	int count=1;
    while(1)
    {
    	pFrame1=m_CamDS[0].QueryFrame();
    	pFrame2=m_CamDS[1].QueryFrame();

        if(isFirst)
        {
        	width=pFrame1->width;
        	height=pFrame1->height;
        	Frame=cvCreateImage(cvSize(pFrame1->width*2+200,pFrame1->height*2+200),pFrame1->depth,pFrame1->nChannels);
        	Rectify_Frame1=cvCreateImage(cvGetSize(pFrame1),pFrame1->depth,pFrame1->nChannels);
        	Rectify_Frame2=cvCreateImage(cvGetSize(pFrame2),pFrame2->depth,pFrame2->nChannels);
        	printf("width=%d,height=%d\n",width,height);
			cvSet(Frame,cvScalar(255,255,255,255),0);
        	isFirst=false;
        }
        else
        {
        	Rectify_Frame1=pFrame1;
        	Rectify_Frame2=pFrame2;
            
            for(int i=0;i<4;i++)
        	DrawText(roi,s[i],i);
            cvShowImage(win_name,Frame);
            

            sprintf(path1,"E:\\Img\\l%d.bmp",count);
			sprintf(path2,"E:\\Img\\r%d.bmp",count);
	        //cvShowImage(win_name,Frame);
	        c=cvWaitKey(300);
	        if(c=='s') { cvSaveImage(path1,pFrame1); cvSaveImage(path2,pFrame2); count=count+1; }
	        if(c==27||count>10) break; 
        }
    }

    //cvWaitKey(0);
    cvDestroyAllWindows();
    cvReleaseImage(&Frame);
    cvReleaseImage(&Rectify_Frame1);
    cvReleaseImage(&Rectify_Frame2);

	m_CamDS[0].CloseCamera();
	m_CamDS[1].CloseCamera();

	
    
    return 0;

}



相關文章