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

📄 capimagestereo.cpp

📁 1394 接口视觉工具箱 (英文工具箱
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************************/
// Compile:   At the MATLAB prompt type:      'cc'
//
// fw-01-08
//
// capture two images from the first two available cameras on the firewire bus
// image capture is done in the requested format (RGB, YUV)
// two images are returned to MATLAB in RGB format
//
// Syntax:   '[im1, im2] = capImageStereo(0)'
//
/****************************************************************************************/


/* includes --------------------------------------------------------------------- */

#include <windows.h>
#include <string.h>			// strcmpi
#include "1394camera.h"
#include "mex.h"
#include "matrix.h"





/* (static) global variables ---------------------------------------------------- */

C1394Camera			Camera[2];
C1394CameraControl	*pControl;


/* camera display modes */
// mode table (supported by the CMU driver, format '0' only):
//
// 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},
//
// NOTE: mode enumeration now in line with camera modes
//       (formerly:  0 = RGB, 1 = YUV)
//
// fw-12-07
//
#define numMYCAMERAMODES	7
typedef enum {
	CAMERA_STOP = -1, 
	CAMERA_YUV444_160x120,
	CAMERA_YUV422_320x240,
	CAMERA_YUV411_640x480,
	CAMERA_YUV422_640x480,
	CAMERA_RGB8_640x480, 
	CAMERA_Y8_640x480, 
	CAMERA_Y16_640x480
} myCameraModes;


/* camera features to be controlled manually */
#define numMYFEATURES 7
static const struct feature_description {
				LPCSTR name;
				LPCSTR unit;	
} myFeatures[numMYFEATURES] = 
{
				{"Brightness","%"},
				{"Auto Exposure","eV"},
				{"Sharpness",""},
				{"White Balance", "K"},
				{"Saturation","%"},
				{"Shutter","sec"},
				{"Gain","dB"},
};
			


static unsigned char  bufRGB[640*480*3];		// local image buffer (max required size)

#define	 NDIMS  3                               // (maximum) dimension of the output data (RGB)
static unsigned int   dims[NDIMS];
static unsigned long  frameWidth, frameHeight;
static unsigned int   origX, origY;
static unsigned long  nElements;


/* this variable is set to one when the grabber object has been initialized */
static unsigned int   initCamera = 0;

/* verbosity control variable */
static unsigned int   verboseFlag = 0;

/* this variable is set according to the 'mode' call-up parameter */
static myCameraModes  currentMode = CAMERA_STOP;		// start with CAMERA_STOP mode (required for correct function)

/* allow maximum width and height to be reduced... */
static unsigned int   requestedWidth;
static unsigned int   currWidth  = 0;
static unsigned int   requestedHeight;
static unsigned int   currHeight = 0;
static unsigned int   requestedOrigX;
static unsigned int   currOrigX  = 0;
static unsigned int   requestedOrigY;
static unsigned int   currOrigY  = 0;




/* local functions -------------------------------------------------------- */


/* mySetFeature =========================================================== */
/* helper function to set a named feature (e.g. 'Brightness') to a specific value */
static void mySetFeature(unsigned int cam, const char *feature, int vLo, int vHi = 0) {

int					fID = dc1394GetFeatureId(feature);
int					aa;
		
	if((pControl = Camera[cam].GetCameraControl(CAMERA_FEATURE(fID))) != NULL) {

		/* found the named feature -> check if manual setting has been requested */
		if(vLo < 0 || vHi < 0) {

			/* request for automatic mode (if available) */
			if(pControl->HasAutoMode()) {

				/* (re-)activate automatic mode */
				pControl->SetAutoMode(TRUE);
				
			}

		} else {
		
			/* manual mode -> set low and high value */
			pControl->SetValue(vLo, vHi);

			/* is this an automatically controlled feature */
			aa = pControl->HasAutoMode();
			if(aa) {

				/* force automatic mode to off... */
				pControl->SetAutoMode(FALSE);

			}

		}
 			
	} else {
		
		/* feature name not registered */
		mexPrintf("Unknown cameara feature '%s'\n", feature);
		
	}
		
} /* mySetFeature ========================================================== */


/* dumpCameraStats ========================================================= */
/* dump some info about the selected camera to the workspace window */
static void dumpCameraStats(unsigned int cam) {

#define maxBufLen 100
		
char	myBuf[maxBufLen];
int		myBufLen = maxBufLen;
int		myNode;


	#ifdef DEBUGONLY /* ============================================= */

	/* display list of all features as registered by the driver... */
	for(int i=0; i<FEATURE_NUM_FEATURES; i++) {

		/* current feature */
		CAMERA_FEATURE  fID = (CAMERA_FEATURE)(i);
		
		if((pControl = Camera[cam].GetCameraControl(fID)) != NULL) {
			
			/* call 1394camapi function 'dc1394GetFeatureName' to determine feature name */
			mexPrintf("Camera feature %d is registered as '%s'\n", fID, dc1394GetFeatureName(fID));
			
		} else {
			
			/* feature name not registered */
			mexPrintf("Camera feature %d has no registered name\n", fID);
			
		}
		
	}

	#endif  /* DEBUGONLY ============================================ */



	/* display the properties of chosen camera... */
	Camera[cam].RefreshCameraList();

	
	myNode = Camera[cam].GetNode();
	Camera[cam].GetNodeDescription(myNode, myBuf, myBufLen);
	mexPrintf("Device description (node %d):   %s\n", myNode, myBuf);
	
	Camera[cam].GetCameraName(myBuf, myBufLen);
	mexPrintf("%-30s %s\n", "Camera name:", myBuf);
	
	Camera[cam].GetCameraVendor(myBuf, myBufLen);
	mexPrintf("%-30s %s\n", "Vendor name:", myBuf);
	
	mexPrintf("%-30s %ld\n", "Version number:", Camera[cam].GetVersion());
	
	mexPrintf("%-30s %d\n", "Max speed:", Camera[cam].GetMaxSpeed());
	
	mexPrintf("%-30s %d\n", "Link status", Camera[cam].CheckLink());
	
	mexPrintf("%-30s %d\n", "Has power control:", Camera[cam].HasPowerControl());
	mexPrintf("%-30s %d\n", "Status power control:", Camera[cam].StatusPowerControl());
	//int	 SetPowerControl(BOOL	on);
	
	mexPrintf("%-30s %d\n", "Has 1394b:", Camera[cam].Has1394b());
	mexPrintf("%-30s %d\n", "Status 1394b:", Camera[cam].Status1394b());
	//int	 Set1394b(BOOL on);
	
	// read out camera eeprom
	mexPrintf("%-30s %d\n", "Number of channels (camera):", Camera[cam].MemGetNumChannels());
	mexPrintf("%-30s %d\n", "Current channel (camera):", Camera[cam].MemGetCurrentChannel());
	
	// read out registry
	//mexPrintf("%-30s %d\n", "Default brightness (registry):", Camera[cam].RegLoadSettings("HKEY_LOCAL_MACHINE/SOFTWARE/CMU/1394Camera/6143140808066302/ControlPanes/DefaultView/Brightness"));
	
	
	// check all video modes supported by the CMU driver for availability on the currently connected camera
	//
	// NOTE: 'format' always set to '0' (= max resolution: 640 x 480  ....  unibrain fire-i)
	//
	// fw-01-08
	//
	for(int ii=0; ii<numMYCAMERAMODES; ii++) {
		
		VIDEO_MODE_DESCRIPTOR myModeDesc;
		
		/* get video mode description, format: 0L, */
		dc1394GetModeDescriptor(0L, (ULONG)ii, &myModeDesc);
		dc1394GetModeString(0L, (ULONG)ii, myBuf, myBufLen);
		
		if(Camera[cam].HasVideoMode(0L, ii)) {
			
			mexPrintf("%-30s %s [%d bits per pixel]\n", "Camera supports video mode:", myBuf, (int)dc1394GetBitsPerPixel(myModeDesc.colorcode));
			
		} else {
			
			mexPrintf("%-30s %s\n", "Unsupported video mode:", myBuf);
			
		}
		
	}
	
	
	// display selected features
	{
		
		int 				fID;
		unsigned short		aa, bb;
		
		/* display current settings of the above features... */
		for(int i=0; i<numMYFEATURES; i++) {
			
			fID = dc1394GetFeatureId(myFeatures[i].name);
			mexPrintf("\nFeature '%s' (ID %d)\n", myFeatures[i].name, fID);
			
			/* get chosen feature control object */
			pControl = Camera[cam].GetCameraControl(CAMERA_FEATURE(fID));
			
			
			pControl->GetRange(&aa, &bb);
			mexPrintf("%-30s %d, %d\n", "Range (min, max):", aa, bb);
			
			pControl->GetValue(&aa, &bb);
			if(bb) {
				/* feature with a range */
				mexPrintf("%-30s %d, %d\n", "Current values (low, high):", aa, bb);
			} else {
				/* feature with a single value */
				mexPrintf("%-30s %d\n", "Current value:", aa, bb);
			}
			
			/* selected attributes */				
			aa = pControl->HasManualMode();
			if(aa == 0) {
				mexPrintf("%-30s %d\n", "Attribute 'ManualMode' (!):", aa);
			}
			
			aa = pControl->HasAutoMode();
			if(aa) {
				mexPrintf("%-30s %d\n", "Attribute 'AutoMode':", pControl->StatusAutoMode());
			}
			
			aa = pControl->HasOnePush();
			if(aa) {
				mexPrintf("%-30s %d\n", "Attribute 'OnePush':", pControl->StatusOnePush());
			}
			
			aa = pControl->HasOnOff();
			if(aa) {
				mexPrintf("%-30s %d\n", "Attribute 'OnOff':", pControl->StatusOnOff());
			}
			
			// always '1'...
			//
			//aa = pControl->HasPresence();
			//if(aa) {
			//	mexPrintf("%-30s %d\n", "Attribute 'Presence':", pControl->StatusPresence());
			//}
			
			/* absolute control... (scaled) */
			if(pControl->HasAbsControl()) {
				
				float		faa, fbb;
				
				pControl->GetRangeAbsolute(&faa, &fbb);
				mexPrintf("%-30s %g, %g\n", "Scaled range (min, max):", faa, fbb);
				
				pControl->GetValueAbsolute(&faa);
				mexPrintf("%-30s %g\n", "Scaled value:", faa);
				
			}
			
		}  /* for all features */
		
	}
	
	
} /* dumpCameraStats ======================================================= */



/* myConfigCamera =========================================================== */
static void myConfigCamera(unsigned int cam) {

FILE  *fp;
	
	/* setup camera(s) */
	mexPrintf("\nAdjusting camera settings...\n");

	if((fp = fopen("cameraconfig.txt","r")) == NULL) {
		
		mexPrintf("Couldn't find camera configuration file ('cameraconfig.txt') -> using default values, automatic wherever possible.\n");


		/* use default camera settings - values found using the control dialog of '1394CameraDemo.exe' */
		//mySetFeature("Shutter", 6, 0);
		//mySetFeature("Saturation", 90, 0);
		//mySetFeature("White Balance", 69, 96);
		//mySetFeature("Gain", 87, 0);
		//mySetFeature("Auto Exposure", 511, 0);
		//mySetFeature("Brightness", 304, 0);
		
		/* negative values -> automatic mode (if possible), otherwise: keep value as is */
		mySetFeature(cam, "Shutter", -1, 0);
		mySetFeature(cam, "Saturation", -1, 0);
		mySetFeature(cam, "White Balance", -1, -1);
		mySetFeature(cam, "Gain", -1, 0);
		mySetFeature(cam, "Auto Exposure", -1, 0);
		mySetFeature(cam, "Brightness", -1, 0);

	} else {

		mexPrintf("Scanning 'cameraconfig.txt' for camera settings for camera %d\n", cam+1);

		/* cameraconfig.txt found -> analyse its contents */
		while(!feof(fp)) {

			char			myStr[30];
			int				myPara[7];

			fscanf(fp, "%s", myStr);
			//mexPrintf("Read: >>%s<<\n", myStr);

			/* filter out 'Camera_1 (only using 'camera_1' here...) */
			if((cam == 0) && (strcmpi(myStr, "[Camera_1]") == 0)) {

				/* found definition for 'camera_1' -> read 7 values... */
				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(cam, "Shutter", myPara[0], 0);
				mySetFeature(cam, "Saturation", myPara[1], 0);
				mySetFeature(cam, "White Balance", myPara[2], myPara[3]);
				mySetFeature(cam, "Gain", myPara[4], 0);
				mySetFeature(cam, "Auto Exposure", myPara[5], 0);
				mySetFeature(cam, "Brightness", myPara[6], 0);

			} /* if(camera_1) */

			
			/* filter out 'Camera_2 (only using 'camera_2' here...) */
			if((cam == 1) && (strcmpi(myStr, "[Camera_2]") == 0)) {

				/* found definition for 'camera_2' -> read 7 values... */
				mexPrintf("Programming camera '2' 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(cam, "Shutter", myPara[0], 0);
				mySetFeature(cam, "Saturation", myPara[1], 0);
				mySetFeature(cam, "White Balance", myPara[2], myPara[3]);
				mySetFeature(cam, "Gain", myPara[4], 0);
				mySetFeature(cam, "Auto Exposure", myPara[5], 0);
				mySetFeature(cam, "Brightness", myPara[6], 0);

			} /* if(camera_2) */

		}

		fclose(fp);

	}

	/* 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) {

⌨️ 快捷键说明

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