⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 capimage.cpp

📁 1394 接口视觉工具箱 (英文工具箱
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		}

		fclose(fp);

	}

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


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


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



/* mySetupCamera ============================================================= */
static void mySetupCamera(myCameraModes mode) {
    

    /* only initialize the camera if no CAMERA_STOP request has been issued */
    if(mode != CAMERA_STOP) {
        
        ////mexPrintf("Scanning for connected cameras...\n");
		if(theCamera.CheckLink() != CAM_SUCCESS)
			mexErrMsgTxt("Error while checking for connected cameras.\n");

		if(theCamera.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(theCamera.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);
					theCamera.SetVideoFormat(0);
					theCamera.SetVideoMode(0);			/* 0: YUV(4:4:4) */
					theCamera.SetVideoFrameRate(4);		/* 4: 30 fps */
                
					break;
                
				case CAMERA_YUV422_320x240:
                
					/* 320x240 YUV422, 30 fps */
					//mexPrintf("Mode %d -> Selecting YUV422 sink (30 fps)\n", mode);
					theCamera.SetVideoFormat(0);
					theCamera.SetVideoMode(1);			/* 1: YUV(4:2:2) */
					theCamera.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);
					theCamera.SetVideoFormat(0);
					theCamera.SetVideoMode(2);			/* 2: YUV(4:1:1) */
					theCamera.SetVideoFrameRate(4);		/* 4: 30 fps */

					break;
                    
				case CAMERA_YUV422_640x480:
                
					/* 640x480 YUV422, 15 fps */
					//mexPrintf("Mode %d -> Selecting YUV422 sink (15 fps)\n", mode);
					theCamera.SetVideoFormat(0);
					theCamera.SetVideoMode(3);			/* 3: YUV(4:2:2) */
					theCamera.SetVideoFrameRate(3);		/* 3: 15 fps */
                
					break;
                
				case CAMERA_RGB8_640x480:
                
					/* 640x480 RGB, 15 fps */
					//mexPrintf("Mode: %d -> Selecting RGB sink (15 fps)\n", mode);
					theCamera.SetVideoFormat(0);
					theCamera.SetVideoMode(4);			/* 4: RGB8 */
					theCamera.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);
					theCamera.SetVideoFormat(0);
					theCamera.SetVideoMode(5);			/* 5: Mono, 8-bit */
					theCamera.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);
					theCamera.SetVideoFormat(0);
					theCamera.SetVideoMode(6);			/* 6: Mono, 16-bit */
					theCamera.SetVideoFrameRate(3);		/* 3: 15 fps */
                
					break;
                
			} /* switch */

			
			/* get frame size (= default image size) */
			theCamera.GetVideoFrameDimensions(&frameWidth, &frameHeight);

			/* fix dimensions */
			dims[0] = (unsigned int)frameHeight;
			dims[1] = (unsigned int)frameWidth;
			dims[2] = 3;

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


			/* start image capture */
			if(theCamera.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();

  
			/* fetch one image to waste some time (camera settles) ... */
			for(int ii=0; ii<2; ii++) {
				if(theCamera.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");

		}

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

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

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



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

    
    /************************************************************************/
    /* Check arguments and read input (number of pixels)			        */
    /************************************************************************/
    
    if(!(nlhs<=1 && (nrhs==1 || nrhs==2 || nrhs==4 || nrhs==6))) {
        
        mexErrMsgTxt("Usage:   data = capImage(mode [, 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");

    }
    else {
        
        /* get 'mode' parameter */
        mode = myCameraModes((int)(mxGetScalar(prhs[0])));
        //mexPrintf("mode = %d\n", mode);
        //mexPrintf("initCamera = %d\n", initCamera);

        // defaults
		verboseFlag     = 0;
		requestedWidth  = 0;
		requestedHeight = 0;
		requestedOrigX  = 0;
		requestedOrigY  = 0;


		/* optional: verbosity flag */
		if(nrhs >= 2) {
	        verboseFlag     = (unsigned int)(mxGetScalar(prhs[1]));
		}
        
		/* optional: size */
		if(nrhs >= 4) {
			requestedWidth  = (unsigned int)(mxGetScalar(prhs[2]));
			requestedHeight = (unsigned int)(mxGetScalar(prhs[3]));
		}
        
		/* optional: origin */
		if(nrhs == 6) {
			requestedOrigX  = (unsigned int)(mxGetScalar(prhs[4]));
			requestedOrigY  = (unsigned int)(mxGetScalar(prhs[5]));
		}
        
    }
    

    /* initialize camera - this is done everytime there's a mode change... */
    if(currentMode != mode) {
        
        /* stop acquisition - if currently ongoing */
		if(initCamera) theCamera.StopImageCapture();

		/* (re)initialize camera */
        mySetupCamera(mode);

    }
    
    
    /* resize... if necessary */
    if(	currWidth  != requestedWidth  || \
		currHeight != requestedHeight || \
		currOrigX  != requestedOrigX  || \
		currOrigY  != requestedOrigY     ) {
        
		/* set requested ORIGIN (X)  of the image to be returned to MATLAB */
		if(requestedOrigX >= 0 && requestedOrigX < frameWidth) {
			
			/* change of origin (X) */
			origX = requestedOrigX;
			
		} else {
			
			/* default origin (X) */
			origX = 0;
			//mexPrintf("Requested origin-x (%d) outside valid limits (0 ... %d). Using default origin (0).\n", requestedOrigX, frameWidth-1);
			
		}

		/* remember that the origin has been set */
		currOrigX = requestedOrigX;

		
		/* set requested WIDTH of the image to be returned to MATLAB */
		if(requestedWidth > 0 && requestedWidth <= frameWidth-origX) {
			
			/* requested width is valid -> reduce width of output array */
			dims[1] = requestedWidth;
			
		} else {

			/* requested width exceeds framewidth -> allocate as much as available */
			dims[1] = frameWidth - origX;

		}
		
		/* remember that the width has been set */
		currWidth = requestedWidth;
			

		/* set requested ORIGIN (Y)  of the image to be returned to MATLAB */
		if(requestedOrigY >= 0 && requestedOrigY < frameHeight) {
			
			/* change of origin (Y) */
			origY = requestedOrigY;
			
		} else {
			
			/* default origin (X) */
			origY = 0;
			//mexPrintf("Requested origin-y (%d) outside valid limits (0 ... %d). Using default origin (0).\n", requestedOrigY, frameHeight-1);
			
		}
		
		/* remember that the origin has been set */
		currOrigY = requestedOrigY;


		/* set requested HEIGHT of the image to be returned to MATLAB */
		if(requestedHeight > 0 && requestedHeight <= frameHeight-origY) {
			
			/* requested height is valid -> reduce height of output array */
			dims[0] = requestedHeight;
			
		} else {

			/* requested height exceeds frameheight -> allocate as much as available */
			dims[0] = frameHeight - origY;

		}

		/* remember that the height has been set */
		currHeight = requestedHeight;
			
		
		#ifdef ERASE
		// debug only
		mexPrintf("requestedWidth  = %d\n", requestedWidth);
		mexPrintf("currWidth       = %d\n", currWidth);
		mexPrintf("dims[1]         = %d\n", dims[1]);
		mexPrintf("requestedHeight = %d\n", requestedHeight);
		mexPrintf("currHeight      = %d\n", currHeight);
		mexPrintf("dims[0]         = %d\n", dims[0]);
		mexPrintf("requestedOrigX  = %d\n", requestedOrigX);
		mexPrintf("currOrigX       = %d\n", currOrigY);
		mexPrintf("origX           = %d\n", origX);
		mexPrintf("requestedOrigY  = %d\n", requestedOrigY);
		mexPrintf("currOrigY       = %d\n", currOrigY);
		mexPrintf("origY           = %d\n", origY);
		#endif

    }


    /* create a MATLAB array with 'm_width' columns, 'm_height' rows, 'real numbers'	*/
    pArray = mxCreateNumericArray(3, (int const *)&dims[0], mxUINT8_CLASS, mxREAL);
    //mexPrintf("pArray created\n");
    
    
    /* acquire image -- make sure camera is actually running... */
    if(initCamera == 1) {
        
        //mexPrintf("Capturing image\n");
		if(theCamera.CaptureImage() != CAM_SUCCESS)
			mexErrMsgTxt("Error during image capture.\n");
        
       	/* convert camera data to RGB and copy it to 'bufRGB' (input format automatically honoured) */
		if(theCamera.getRGB(bufRGB, nElements) != CAM_SUCCESS)
			mexErrMsgTxt("Error during conversion to RGB.\n");
        
        /* transfer data from DMA buffer to the MATLAB buffer...		 */
        MATbuf = (unsigned char *)mxGetPr(pArray);

		/* reformat data for MATLAB */
		//mexPrintf("before copy (always in BGR format...)\n");
		for(unsigned int row=0; row<dims[0]; row++) /* height */ {
			////mexPrintf("row = %d  --  ", row);
			for(unsigned int col=0; col<dims[1]; col++) /* width */ {
				////mexPrintf("%d ", col);
				for(unsigned int rgb=0; rgb<3; rgb++)
					MATbuf[row+col*dims[0]+rgb*dims[0]*dims[1]] = 
					(unsigned char)(*(bufRGB+(origY+row)*(frameWidth*3)+(origX+col)*3+(rgb)));	// B,G,R
			}
		}
		//mexPrintf("after copy\n");
        

    } /* initCamera == 1 */
    
    
    /* set return pointer */
    plhs[0] = pArray;
    
   
} /* mexFunction ============================================================ */

⌨️ 快捷键说明

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