capprocstereo.cpp

来自「1394 接口视觉工具箱 (英文工具箱」· C++ 代码 · 共 1,871 行 · 第 1/4 页

CPP
1,871
字号
	}

	/* end of 'adjust settings' */
	mexPrintf("done.\n");


	/* display current settings (for all connected cameras) */
	if(verboseFlag) {
		dumpCameraStats(cam);
	}


} /* myConfigCamera ========================================================== */



/* mySetupCamera ============================================================= */
static void mySetupCamera(myCameraModes mode) {
    
int		numCameras = 0;
    
	/* check for 1st camera... needs to be done for the Camera object to be initialized properly!?  fw-01-08 */
	if(Camera[0].CheckLink() != CAM_SUCCESS) {

		mexErrMsgTxt("Error while checking for connected cameras.\n");

	} else {

		numCameras++;

	}

	/* check for 2nd camera... needs to be done for the Camera object to be initialized properly!?  fw-01-08 */
	if(Camera[1].CheckLink() != CAM_SUCCESS) {

		mexErrMsgTxt("Error while checking for connected cameras.\n");

	} else {

		numCameras++;

	}


	/* this is a stereo driver  --  abort if there are not at least 2 cameras... */
	if(numCameras < 2) {

		mexErrMsgTxt("Couldn't find at least two cameras on the bus.\n");

	} else {

		/* ensure that there are at least 2 cameras on the bus */
		mexPrintf("Detected %d cameras...\n", numCameras);
		
	}


    /* only initialize the camera if no CAMERA_STOP request has been issued */
    if(myCameraModes(mode) != CAMERA_STOP) {
        
        /* stop all cameras - if necessary */
		if(initCamera) {

			//mexPrintf("Stopping all cameras\n");
			
			for(int cam=0; cam<numCameras; cam++) {

				/* select current camera */
				Camera[cam].SelectCamera(cam);

				/* stop selected camera */
				Camera[cam].StopImageCapture();

			} /* for */

			//mexPrintf("All cameras stopped\n");

			/* all cameras stopped */
			initCamera = 0;

			/* indicate mode change */
			currentMode = mode;

		} /* if */
        
		
		/* (re-)initialize all cameras */
		for(int cam=0; cam<numCameras; cam++) {

			////mexPrintf("Scanning for connected cameras...\n");
			if(Camera[cam].CheckLink() != CAM_SUCCESS)
				mexErrMsgTxt("Error while checking for connected cameras.\n");

			/* select current camera */
			Camera[cam].SelectCamera(cam);

			if(Camera[cam].InitCamera() != CAM_SUCCESS)
				mexErrMsgTxt("Error during initialization of the camera.\n");
			////mexPrintf("Camera successfully initialized\n");
        

			/* set image buffer, define pixel format */
			// 0:		{160 ,120 ,COLOR_CODE_YUV444},
			// 1:		{320 ,240 ,COLOR_CODE_YUV422},
			// 2:		{640 ,480 ,COLOR_CODE_YUV411},
			// 3:		{640 ,480 ,COLOR_CODE_YUV422},
			// 4:		{640 ,480 ,COLOR_CODE_RGB8},
			// 5:		{640 ,480 ,COLOR_CODE_Y8},
			// 6:		{640 ,480 ,COLOR_CODE_Y16}

			/* only set up camera, if the requested mode is supported by the connected camera */
			if(Camera[cam].HasVideoMode(0L, mode)) {

				/* camera supports the requested mode */
				switch(mode) {
            
					case CAMERA_YUV444_160x120:
                
						/* 160x120 YUV444, 30 fps */
						//mexPrintf("Mode %d -> Selecting YUV444 sink (30 fps)\n", mode);
						Camera[cam].SetVideoFormat(0);
						Camera[cam].SetVideoMode(0);			/* 0: YUV(4:4:4) */
						Camera[cam].SetVideoFrameRate(4);		/* 4: 30 fps */
                
						break;
                
					case CAMERA_YUV422_320x240:
                
						/* 320x240 YUV422, 30 fps */
						//mexPrintf("Mode %d -> Selecting YUV422 sink (30 fps)\n", mode);
						Camera[cam].SetVideoFormat(0);
						Camera[cam].SetVideoMode(1);			/* 1: YUV(4:2:2) */
						Camera[cam].SetVideoFrameRate(4);		/* 4: 30 fps */
                
						break;
                
					case CAMERA_YUV411_640x480:
					default:

						/* 640x480 YUV411, 30 fps */
						mexPrintf("Mode: %d -> Selecting YUV411 sink (30 fps)\n", mode);
						Camera[cam].SetVideoFormat(0);
						Camera[cam].SetVideoMode(2);			/* 2: YUV(4:1:1) */
						Camera[cam].SetVideoFrameRate(4);		/* 4: 30 fps */

						break;
                    
					case CAMERA_YUV422_640x480:
                
						/* 640x480 YUV422, 15 fps */
						//mexPrintf("Mode %d -> Selecting YUV422 sink (15 fps)\n", mode);
						Camera[cam].SetVideoFormat(0);
						Camera[cam].SetVideoMode(3);			/* 3: YUV(4:2:2) */
						Camera[cam].SetVideoFrameRate(3);		/* 3: 15 fps */
                
						break;
                
					case CAMERA_RGB8_640x480:
                
						/* 640x480 RGB, 15 fps */
						mexPrintf("Mode: %d -> Selecting RGB sink (15 fps)\n", mode);
						Camera[cam].SetVideoFormat(0);
						Camera[cam].SetVideoMode(4);			/* 4: RGB8 */
						Camera[cam].SetVideoFrameRate(3);		/* 3: 15 fps */
                
						break;
                
					case CAMERA_Y8_640x480:
                
						/* 640x480 B&W, 8 bit per pixel, 30 fps */
						mexPrintf("Mode: %d -> Selecting B&W sink (30 fps)\n", mode);
						Camera[cam].SetVideoFormat(0);
						Camera[cam].SetVideoMode(5);			/* 5: Mono, 8-bit */
						Camera[cam].SetVideoFrameRate(4);		/* 4: 30 fps */
                
						break;
                
					case CAMERA_Y16_640x480:
                
						/* 640x480 B&W 16 bit per pixel, 15 fps */
						mexPrintf("Mode: %d -> Selecting B&W sink (15 fps)\n", mode);
						Camera[cam].SetVideoFormat(0);
						Camera[cam].SetVideoMode(6);			/* 6: Mono, 16-bit */
						Camera[cam].SetVideoFrameRate(3);		/* 3: 15 fps */
                
						break;
                
				} /* switch */
        

				// set dimension of the output variable
				Camera[cam].GetVideoFrameDimensions(&frameWidth, &frameHeight);
	
				/* fix dimensions */
				dims[0] = (unsigned int)frameHeight;
				dims[1] = (unsigned int)frameWidth;
				dims[2] = 3;

				/* total number of bytes kept in the buffer */
				nElements = dims[0] * dims[1] * dims[2];

				
				if(Camera[cam].StartImageCapture() != CAM_SUCCESS)
					mexErrMsgTxt("Error at attempted start of image capture.\n");

				/* load camera settings or use default values (can be found using '1394CameraDemo.exe') */
				myConfigCamera(cam);

  
				/* fetch one image to waste some time (camera settles) ... */
				for(int ii=0; ii<2; ii++) {
					if(Camera[cam].CaptureImage() != CAM_SUCCESS)
						mexErrMsgTxt("Error during image capture.\n");
				}


		//		/* doesn't work... */
		//		Sleep( 250 );

				/* camera initialized -> set 'initFlag' */
				initCamera = 1;

				/* indicate mode change */
				currentMode = mode;


			} else {

				/* camera does not support the requested mode */
				mexErrMsgTxt("Requested mode is not supported by connected camera.\n");

			}
        
		}  /* for all cameras */


	} else {
        
		/* CAMERA_STOP request has been issued */
        //mexPrintf("Stopping camera...\n");
        
        /* stop capture - if currently ongoing */
		if(initCamera) {

			mexPrintf("Stopping all cameras\n");
			
			for(int cam=0; cam<numCameras; cam++) {

				/* select current camera */
				Camera[cam].SelectCamera(cam);

				/* stop selected camera */
				Camera[cam].StopImageCapture();

			} /* for */

		}

		//mexPrintf("All cameras stopped\n");

        /* indicate that we can free the grabber (in mexFunction) */
        initCamera = 0;
        
		/* indicate mode change */
		currentMode = mode;

    }
    
} /* mySetupCamera ============================================================ */



/* mySetupVision ============================================================= */
static void mySetupVision(myCameraModes mode, unsigned int Wx, unsigned int Wy) {
    

    /* only (re-)initialize the vision if no CAMERA_STOP request has been issued */
    if(mode != CAMERA_STOP) {
        
			/* dynamically allocate memory for conversion buffers (CMVISION) */
			yuvBuf  = new yuv[Wx*Wy];
			//mexPrintf("yuvBuf = %08x\n", yuvBuf);

			yuyvBuf = new image_pixel[Wx/2*Wy];
			//mexPrintf("yuyvBuf = %08x\n", yuyvBuf);

			img     = new rgb[Wx*Wy];
			//mexPrintf("img = %08x\n", img);

			// initialize class 'vision' (CMVision)
			if(!vision.initialize(Wx, Wy))
				mexErrMsgTxt("Vision init failed.\n");
        
			vision.loadOptions(colourFilename);
			vision.enable(CMV_DENSITY_MERGE);
        
	} else {
        
		/* CAMERA_STOP request has been issued */

		// destroy class 'vision'
		vision.close();

		/* destroy dynamically allocated buffer for 'yuv' data */
		if(yuvBuf != NULL) {
			delete yuvBuf;
			yuvBuf = NULL;
			//mexPrintf("yuv buffer destroyed.\n");
		}

		/* destroy dynamically allocated buffer for 'yuyv' data */
		if(yuyvBuf != NULL) {
			delete yuyvBuf;
			yuyvBuf = NULL;
			//mexPrintf("yuyv buffer destroyed.\n");
		}

		/* destroy dynamically allocated buffer for 'img' data (output) */
		if(img != NULL) {
			delete img;
			img = NULL;
			//mexPrintf("img buffer destroyed.\n");
		}

    }
    
} /* mySetupVision ============================================================ */



/* mexFunction ============================================================= */
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) {
    
mxArray		    *pArray[2];					/* output data */
mxArray		    *pArray2[2];           		/* output data (optional, RGB image) */
unsigned char	*MATbuf;            		/* pointer to MATLAB memory */
    
myCameraModes	mode;



// ######### CMVision ##########
unsigned int	retIMG;
int				i, j;
int				numCOL, maxCOL;                 // number of detected colours
int				numREG;
int				COLIDs[CMV_MAX_COLORS];			// list of colour IDs to be scanned for
int				COLdetected[CMV_MAX_COLORS];	// list of detected colour IDs
    
// return detected coordinates in a structure
mxArray			*fout;
double			*pfout;
int				fi;							// field index


    /************************************************************************/
    /* Check arguments and read input (number of pixels)			        */
    /************************************************************************/
    
	/* should be possible to stop the camera using 'capProcStereo(-1);' */
	if(nlhs==0 && nrhs ==1) {

		mode = myCameraModes((int)(mxGetScalar(prhs[0])));
		if(mode == CAMERA_STOP) {

			/* stop acquisition - if currently ongoing */
			if(initCamera) {

				/* stop camera */
				mySetupCamera(CAMERA_STOP);

				/* stop vision */
				mySetupVision(CAMERA_STOP, 0, 0);

			}

			return;
			
		}

	}

	
	if(!((nlhs==2 || nlhs==4) && (nrhs==1 || nrhs==2 || nrhs==3 || nrhs==4 || nrhs==6 || nrhs==8))) {
        
        mexErrMsgTxt("Usage:   [Region_StructureArray1, Region_StructureArray2, [RGB_Data1 ,RGB_Data2]] = capProcStereo(mode [, 'colour_filename'][, colour_list][, verbose][, sizeX, sizeY][, originX, originY])\n    " \
					 "mode:    -1 STOP\n" \
					 "              0 YUV444 (160x120, 30 fps)\n" \
					 "              1 YUV422 (320x240, 30 fps)\n" \
					 "              2 YUV411 (640x480, 30 fps)\n" \
					 "              3 YUV422 (640x480, 15 fps)\n" \
					 "              4 RGB8   (640x480, 15 fps)\n" \
					 "              5 Y8     (640x480, 30 fps)\n" \
					 "              6 Y16    (640x480, 15 fps)\n" \
					 "    verbose:  0 silent\n" \
					 "              1 verbose\n" \
					 "    default colour file: testcolors.txt\n" );

    }
    else {
        
        // defaults
		strcpy(colourFilename, defColourFilename);
		retIMG 			= 0;		
		maxCOL			= defCOL;			// default: just one colour
		COLIDs[0]		= 0;				// default colour '0'
		verboseFlag     = 0;
		requestedWidth  = 0;
		requestedHeight = 0;
		requestedOrigX  = 0;
		requestedOrigY  = 0;

		// extra LHS arguments? -> flag the returning of RGB image data
		if(nlhs == 4) {
			
			retIMG = 1;
			
		}
		
        /* get 'mode' parameter (always required) */
        mode = myCameraModes((int)(mxGetScalar(prhs[0])));
        //mexPrintf("mode = %d\n", mode);
        //mexPrintf("initCamera = %d\n", initCamera);
	
		/* optional RHS: colour file name */
		if(nrhs >= 2) {
			mxGetString(prhs[1], colourFilename, colourFilenameLength);
			//mexPrintf("colour file: %s\n", colourFilename);
		}
        
		/* optional RHS: list of colours to be scanned for */
		if(nrhs >= 3) {
		
			/* get list of colours */
			if(!mxIsEmpty(prhs[2])) {
			
				if(!mxIsNumeric(prhs[2]) && !mxIsComplex(prhs[2]))
					mexErrMsgTxt("The specified colour list has to be a vector with up to 16 colour IDs\n");
				
				maxCOL = (unsigned int)mxGetNumberOfElements(prhs[2]);
				if(maxCOL > CMV_MAX_COLORS) {
					mexWarnMsgTxt("Too many colours specified. Reducing to maximum permitted (32)\n");
					maxCOL = CMV_MAX_COLORS;
				}
				
				// read colour vector
				pfout = mxGetPr(prhs[2]);
				for(fi=0; fi<maxCOL; fi++) {
					
					COLIDs[fi] = (unsigned int)(*(pfout+fi))-1;
					
					if((COLIDs[fi] < 0) || (COLIDs[fi] >= CMV_MAX_COLORS))
						mexErrMsgTxt("Colour(s) outside the valid range (1 ... 32).\n");
				}
				
			}
			
		}
        
		/* optional RHS: verbosity flag */
		if(nrhs >= 4) {
	        verboseFlag     = (unsigned int)(mxGetScalar(prhs[3]));
		}

		/* optional: size */
		if(nrhs >= 6) {
			requestedWidth  = (unsigned int)(mxGetScalar(prhs[4]));
			requestedHeight = (unsigned int)(mxGetScalar(prhs[5]));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?