001 #include <opencv2/opencv.hpp> 002 #include <opencv2/opencv_lib.hpp> 003 004 #define CamWidth (1920) 005 #define CamHeight (1080) 006 007 void mergeHalfImage(IplImage *imgCamL, IplImage *imgCamR, IplImage *imgWin) { 008 int sx, sy; 009 010 for(sy = 0; sy < (CamHeight / 2); sy++) { 011 for(sx = 0; sx < (CamWidth / 2); sx++) { 012 CV_IMAGE_ELEM(imgWin, uchar, sy, sx * 3 + 0) = CV_IMAGE_ELEM(imgCamL, uchar, (sy * 2), (sx * 2) * 3 + 0); 013 CV_IMAGE_ELEM(imgWin, uchar, sy, sx * 3 + 1) = CV_IMAGE_ELEM(imgCamL, uchar, (sy * 2), (sx * 2) * 3 + 1); 014 CV_IMAGE_ELEM(imgWin, uchar, sy, sx * 3 + 2) = CV_IMAGE_ELEM(imgCamL, uchar, (sy * 2), (sx * 2) * 3 + 2); 015 016 CV_IMAGE_ELEM(imgWin, uchar, sy, (sx + (CamWidth / 2)) * 3 + 0) = CV_IMAGE_ELEM(imgCamR, uchar, (sy * 2), (sx * 2) * 3 + 0); 017 CV_IMAGE_ELEM(imgWin, uchar, sy, (sx + (CamWidth / 2)) * 3 + 1) = CV_IMAGE_ELEM(imgCamR, uchar, (sy * 2), (sx * 2) * 3 + 1); 018 CV_IMAGE_ELEM(imgWin, uchar, sy, (sx + (CamWidth / 2)) * 3 + 2) = CV_IMAGE_ELEM(imgCamR, uchar, (sy * 2), (sx * 2) * 3 + 2); 019 } 020 } 021 022 return; 023 } 024 025 int main(int argc, char* argv[]) { 026 CvCapture *capCamL, *capCamR; 027 IplImage *imgCamL, *imgCamR; 028 IplImage *imgWin; 029 int key; 030 031 // (1) 左右のカメラのキャプチャ構造体を取得 032 capCamL = cvCreateCameraCapture(0); 033 capCamR = cvCreateCameraCapture(1); 034 035 if(capCamL ==NULL || capCamR ==NULL) { 036 fprintf(stderr, "Error: Can not open two cameras\n"); 037 exit(-1); 038 } 039 040 // (2) キャプチャサイズを設定 041 cvSetCaptureProperty(capCamL, CV_CAP_PROP_FRAME_WIDTH, CamWidth); 042 cvSetCaptureProperty(capCamL, CV_CAP_PROP_FRAME_HEIGHT, CamHeight); 043 044 cvSetCaptureProperty(capCamR, CV_CAP_PROP_FRAME_WIDTH, CamWidth); 045 cvSetCaptureProperty(capCamR, CV_CAP_PROP_FRAME_HEIGHT, CamHeight); 046 047 cvNamedWindow ("Stereo Capture", CV_WINDOW_AUTOSIZE); 048 imgWin = cvCreateImage(cvSize(CamWidth, (CamHeight / 2)), IPL_DEPTH_8U, 3); 049 050 // (3) 左右のカメラから画像をキャプチャし、半分のサイズに縮小・連結して表示 051 while(1) { 052 imgCamL = cvQueryFrame(capCamL); 053 imgCamR = cvQueryFrame(capCamR); 054 055 mergeHalfImage(imgCamL, imgCamR, imgWin); 056 cvShowImage("Stereo Capture", imgWin); 057 058 key = cvWaitKey(1); 059 if(key == '\x1b') 060 break; 061 } 062 063 cvReleaseCapture(&capCamL); 064 cvReleaseCapture(&capCamR); 065 cvDestroyWindow("Stereo Capture"); 066 cvReleaseImage(&imgWin); 067 068 exit(0); 069 }