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

📄 capproc.cpp

📁 1394 接口视觉工具箱 (英文工具箱
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				mexPrintf("Programming camera '1' with: ");
				for(int i=0; i<7; i++) {

					fscanf(fp, "%d", &myPara[i]);

					mexPrintf("%d ", myPara[i]);

				}
				mexPrintf("\n");

				/* reset string */
				myStr[0] = 0;

				/* use user set of camera settings (file: cameraconfig.txt) */
				mySetFeature("Shutter", myPara[0], 0);
				mySetFeature("Saturation", myPara[1], 0);
				mySetFeature("White Balance", myPara[2], myPara[3]);
				mySetFeature("Gain", myPara[4], 0);
				mySetFeature("Auto Exposure", myPara[5], 0);
				mySetFeature("Brightness", myPara[6], 0);

			} /* if(camera_1) */

		}

		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);
			//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;

			/* total number of bytes kept in the 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 ============================================================ */



/* 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;            		/* output data */
mxArray		    *pArray2;           		/* output data (optional, RGB image) */
unsigned char	*MATbuf;            		/* pointer to MATLAB memory */

myCameraModes	mode;

unsigned int	retIMG;
int				i, j;

// ######### CMVision ##########
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 for proper number of input and output arguments 				*/
    /************************************************************************/

	/* should be possible to stop the camera using 'capProc(-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==1 || nlhs==2) && (nrhs==1 || nrhs==2 || nrhs==3 || nrhs==4 || nrhs==6 || nrhs==8))) {
        
        mexErrMsgTxt("Usage:   [Region_StructureArray [,RGB_Data]] = capProc(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;

		// second LHS argument? -> flag the returning of RGB image data
		if(nlhs == 2) {
			
			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]));
		}
        
		/* optional: origin */
		if(nrhs == 8) {
			requestedOrigX  = (unsigned int)(mxGetScalar(prhs[6]));
			requestedOrigY  = (unsigned int)(mxGetScalar(prhs[7]));
		}
        
    }

    
    /* resize... if necessary */
    if(	currentMode != mode            || \
        currWidth   != requestedWidth  || \
		currHeight  != requestedHeight || \
		currOrigX   != requestedOrigX  || \
		currOrigY   != requestedOrigY     ) {
        

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

			/* stop camera */
	        mySetupCamera(CAMERA_STOP);

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

⌨️ 快捷键说明

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