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

📄 sfcapproc.cpp

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

		fclose(fp);

	}

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

} /* 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);
			//mexPrintf("frameWidth  = %d\n", frameWidth);
			//mexPrintf("frameHeight = %d\n", frameHeight);

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

			
			/* start image capture */
			if(ACQmod == 1) {

				// using ImageCapture
				if(theCamera.StartImageCapture() != CAM_SUCCESS)
					mexErrMsgTxt("Error at attempted start of image capture.\n");
			
			} else {

				/* start image capture */
				if(theCamera.StartImageAcquisition() != CAM_SUCCESS)
					mexErrMsgTxt("Error at attempted start of image acquisition.\n");
			
			}

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

  
			/* fetch one image to waste some time (camera settles) ... */
			if(ACQmod == 1) {

				// using ImageCapture
				for(int ii=0; ii<2; ii++) {
					if(theCamera.CaptureImage() != CAM_SUCCESS)
						mexErrMsgTxt("Error during image capture.\n");
				}
			
			} else {

				/* start image capture */
				for(int ii=0; ii<2; ii++) {
					if(theCamera.AcquireImage() != CAM_SUCCESS)
						mexErrMsgTxt("Error during image acquisition.\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(ACQmod == 1) {
			
			// using ImageCapture
			if(initCamera) theCamera.StopImageCapture();
			
		} else {
			
			/* start image capture */
			if(initCamera) theCamera.StopImageAcquisition();
			
		}
		
        /* 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) */
		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");
        
		if(vision.loadOptions(colourFilename) == false) mexErrMsgTxt("Couldn't open colour definition file\n");
		vision.enable(CMV_DENSITY_MERGE);
		
		
		// initialize colour entries in 'retREG'
		for(unsigned int i=0; i<numCOLS; i++) {
			retREG[i]->colour = vision.getColorVisual(i);
		}
        
	} else {
        
		/* CAMERA_STOP request has been issued */

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

		/* 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 ============================================================ */





// ----------------------------------------------------------------------------------------------------
// S-Function methods
// ----------------------------------------------------------------------------------------------------


/* Function: mdlInitializeSizes ===============================================
 *
 */
static void mdlInitializeSizes (SimStruct *S) {
    
    int		i;
    
    ssSetNumSFcnParams(S, NUMBER_OF_ARG);											// expected number of parameters
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        mexErrMsgTxt("SFcapProc: incorrect number of callup parameters\n");
    }
    
    /* setup sizes of both work vectors and state vectors */
    //ssSetNumIWork (S, 0);						// no instance-local integer values
    //ssSetNumRWork (S, 0);						// no instance-local real values
    //ssSetNumPWork (S, 0);						// no instance-local pointers
    //ssSetNumDWork (S, 0);						// no instance-local user data types used
    //ssSetNumContStates (S, 0);				// width of the instance-local vector of continuous states
    //ssSetNumDiscStates (S, 0);				// width of the instance-local vector of discrete states
    
    
    /* define number of sample times used by this s-function */
    ssSetNumSampleTimes (S, 1);					// only 'one' sampletime in this S-Function
    
    // most parameter are not tunable
    for (i=0; i<NUMBER_OF_ARG; i++) ssSetSFcnParamNotTunable(S, i);

	// allow image type to be toggled online
	ssSetSFcnParamTunable(S, 4, SS_PRM_TUNABLE);	// 4 : DISPMOD ... display RGB image (1) or classified image (2)
    
    
    /* determine number of colours to be scanned for -> defines the width of the output vector */
    numCOLS = NUMCOLS;			/* number of colours to be scanned for */
    if(numCOLS > maxCOLS) {
        numCOLS = maxCOLS;
        mexPrintf("WARNING: Too many colours specified; limiting colour definition file to the first %d lines.\n", numCOLS);
    }
    //mexPrintf("Scanning for %d colours\n", numCOLS);
    
    // allocate dynamic memory to store regional data
    for(i=0; i<(int)numCOLS; i++) {
        retREG[i] = new struct myREG;
        //retREG[i] = (struct myREG *)mxCalloc(1, sizeof(struct myREG));
    }
    
    ssSetNumInputPorts(S, 0);					// block has no input...
    ssSetNumOutputPorts(S, 2);					// ... and two outputs (centroid x values and centroid y values)
    ssSetOutputPortWidth(S, 0, numCOLS);		// block output width (x) : 'numCOLS' centroid co-ordinates
    ssSetOutputPortWidth(S, 1, numCOLS);		// block output width (y) : 'numCOLS' centroid co-ordinates
}



/* Function: mdlInitializeSampleTimes =========================================
 *
 */
static void mdlInitializeSampleTimes (SimStruct *S) {
    
    ssSetSampleTime(S, 0, 0.0);					// SR: continuous
    ssSetOffsetTime(S, 0, 0.0);
    
}



/* Function: mdlStart =========================================================
 *
 */
#define MDL_START
static void mdlStart(SimStruct *S) {
    
    
    // set selected camera mode
	mode = myCameraModes(CAMMODE);
    
    // set image display flags
    retIMG  = DISPIMG;			/* 0: no display, 1: display */
    IMGmod  = DISPMOD;			/* 0: regular RGB display, 1: classified display */
    ACQmod  = ACQMODE;			/* 1: CaptureImage, 2: AcquireImage (streaming video) */
    
    
	/* load colour definition file */
	/* reformat filename (MATLAB stores it as [char_1] [0] [char_2] [0] ... */
	for(unsigned int i=0, j=0; i<COLFLEN; i++) {
		colourFilename[i] = COLFILE[j];
		j += 2;
	}
	colourFilename[i] = 0;
	//mexPrintf("Colour definition file: %s\n", colourFilename);

⌨️ 快捷键说明

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