📄 sfcapproc.cpp
字号:
unsigned int row, col, k;
unsigned int u, v;
unsigned int BytesPerRow = (unsigned int)(frameWidth*2);
yuv myYUV[4]; // intermediate result
k = 0;
for(row=0; row<dims[0] /* height */; row++) {
for(col = 0; col<processFrameWidth; col+=4, k++) {
/* Y16 -> YUV */
myYUV[0] = rgb_to_yuv(src[(origY+row)*BytesPerRow + firstPacketStart+col ], \
src[(origY+row)*BytesPerRow + firstPacketStart+col ], \
src[(origY+row)*BytesPerRow + firstPacketStart+col ] );
myYUV[1] = rgb_to_yuv(src[(origY+row)*BytesPerRow + firstPacketStart+col + 1], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 1], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 1] );
myYUV[2] = rgb_to_yuv(src[(origY+row)*BytesPerRow + firstPacketStart+col + 2], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 2], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 2] );
myYUV[3] = rgb_to_yuv(src[(origY+row)*BytesPerRow + firstPacketStart+col + 3], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 3], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 3] );
/* YUV -> YUYV */
u = ((unsigned int)(myYUV[0].u) << 8) + (unsigned int)(myYUV[1].u);
dst[k].u = (unsigned char)((u + ((unsigned int)(myYUV[2].u) << 8) + (unsigned int)(myYUV[3].u)) >> 1); // u
dst[k].y1 = (unsigned char)(((unsigned int)(myYUV[0].y) << 8) + (unsigned int)(myYUV[1].y)); // y1
v = ((unsigned int)(myYUV[0].v) << 8) + (unsigned int)(myYUV[1].v);
dst[k].v = (unsigned char)((v + ((unsigned int)(myYUV[2].v) << 8) + (unsigned int)(myYUV[3].v)) >> 1); // v
dst[k].y1 = (unsigned char)(((unsigned int)(myYUV[2].y) << 8) + (unsigned int)(myYUV[3].y)); // y1
}
}
//mexPrintf("yuyv buffer contains %d elements\n", k);
}
//----------------------------------------------------------
//----------------------------------------------------------
void convCameraRGB_2_YUYV(image_pixel *dst, unsigned char *src) {
unsigned int row, col, k;
unsigned int BytesPerRow = (unsigned int)(frameWidth*3);
yuv myYUV[2]; // intermediate result
k = 0;
for(row=0; row<dims[0] /* height */; row++) {
for(col = 0; col<processFrameWidth; col+=6, k++) {
/* RGB8 -> YUV */
myYUV[0] = rgb_to_yuv(src[(origY+row)*BytesPerRow + firstPacketStart+col ], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 1], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 2] );
myYUV[1] = rgb_to_yuv(src[(origY+row)*BytesPerRow + firstPacketStart+col + 3], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 4], \
src[(origY+row)*BytesPerRow + firstPacketStart+col + 5] );
/* YUV -> YUYV */
dst[k].u = (unsigned char)((myYUV[0].u + myYUV[1].u) >> 1); // u
dst[k].y1 = myYUV[0].y; // y1
dst[k].v = (unsigned char)((myYUV[0].v + myYUV[1].v) >> 1); // u
dst[k].y2 = myYUV[1].y; // y1
}
}
//mexPrintf("yuyv buffer contains %d elements\n", k);
}
//----------------------------------------------------------
//----------------------------------------------------------
// CMU driver: The YUV4,4,4 format is UYV
// -> 'convert' YUV444 (u1 y1 v1 u2 y2 v2) image to YUYV (u y1 v y2) equivalent (copy)
void convYUV444_2_YUYV(image_pixel *dst, unsigned char *src) {
unsigned int row, col, k;
unsigned int u, v;
unsigned int BytesPerRow = (unsigned int)(frameWidth*3);
k = 0;
for(row=0; row<dims[0] /* height */; row++) {
for(col = 0; col<processFrameWidth; col+=6, k++) {
/* average adjacent 'u-components' */
u = src[(origY+row)*BytesPerRow + firstPacketStart+col ];
dst[k].u = (unsigned char)((u + src[(origY+row)*BytesPerRow + firstPacketStart+col + 3]) >> 1); // u
dst[k].y1 = src[(origY+row)*BytesPerRow + firstPacketStart+col + 1]; // y1
/* average adjacent 'v-components' */
v = src[(origY+row)*BytesPerRow + firstPacketStart+col + 2];
dst[k].v = (unsigned char)((u + src[(origY+row)*BytesPerRow + firstPacketStart+col + 5]) >> 1); // u
dst[k].y2 = src[(origY+row)*BytesPerRow + firstPacketStart+col + 4]; // y2
}
}
//mexPrintf("yuyv buffer contains %d elements\n", k);
}
//----------------------------------------------------------
//----------------------------------------------------------
// CMU driver: The YUV4,2,2 format is UYUV for two adjacent pixels
// -> 'convert' YUV422 (u y1 v y2) image to YUYV (u y1 v y2) equivalent (copy)
void convYUV422_2_YUYV(image_pixel *dst, unsigned char *src) {
unsigned int row, col, k;
unsigned int BytesPerRow = (unsigned int)(frameWidth*2);
k = 0;
for(row=0; row<dims[0] /* height */; row++) {
for(col = 0; col<processFrameWidth; col+=4, k++) {
dst[k].u = src[(origY+row)*BytesPerRow + firstPacketStart+col ]; // u
dst[k].y1 = src[(origY+row)*BytesPerRow + firstPacketStart+col + 1]; // y1
dst[k].v = src[(origY+row)*BytesPerRow + firstPacketStart+col + 2]; // v
dst[k].y2 = src[(origY+row)*BytesPerRow + firstPacketStart+col + 3]; // y2
}
}
//mexPrintf("yuyv buffer contains %d elements\n", k);
}
//----------------------------------------------------------
//----------------------------------------------------------
// CMU driver: The YUV4,1,1 format is UYYVYY for four adjacent pixels
// -> convert YUV411 (u y1 y2 v y3 y4) image to YUYV (u y1 v y2, u y3 v y4) equivalent
// note: 6 input values represent 4 pixels -> bytes per row: frameWidth/4*6 = frameWidth*3/2
void convYUV411_2_YUYV(image_pixel *dst, unsigned char *src) {
unsigned int row, col, k;
unsigned int BytesPerRow = (unsigned int)(frameWidth*3/2);
// convert YUV411 (UYYVYY) to YUYV (CMVISION)
// NOTE: 'formattedOrigX' selects the correct YUV411 element (includes the first requested pixel)
// 'dims[1]' is an integer multiple of the number of pixels in each camera element (4)
// EXAMPLE: '3 pixels from pixel #4 on' -> need two packets (y1-y4) & (y5-y10)
// => firstPacketStart = 0, lastPacketStart = 6, dims[0] = 12
// | | |
// _________________v ____v__v_________
// --0--1--2--3--4--5---6--7--8--9-10-11--12-13--14-15--16--17
// -u1-y1-y2-v1-y3-y4--u2-y5-y6-v2-y7-y8--u3-y9-y10-v3-y11-y12
//
// firstPacketStart = (((int)(4/4)-1) >= 0 ? ((int)(4/4)-1) : 0 ) * 6 =
// (((int)(1)-1) >= 0 ? ((int)(1)-1) : 0 ) * 6 =
// (0 >= 0 ? 0 : 0 ) * 6 = 0 * 6 ========================== 0
// lastPacketStart = ((int)(4+3/4) >= 0 ? (int)(4+3/4) : 0 ) * 6 =
// ((int)(7/4) >= 0 ? (int)(7/4) : 0 ) * 6 =
// ((int)(1.75) >= 0 ? (int)(1.75) : 0 ) * 6 =
// (1 >= 0 ? 1 : 0 ) * 6 = 1 * 6 ========================== 6
// processFrameWidth = (lastPacketStart - firstPacketStart + 1) * 6 =
// (1 - 0 + 1) * 6 ======================================== 12
//
k = 0;
for(row=0; row<dims[0] /* height */; row++) {
for(col = 0; col<processFrameWidth; col+=6, k+=2) {
dst[k].u = src[(origY+row)*BytesPerRow + firstPacketStart+col ]; // u
dst[k].y1 = src[(origY+row)*BytesPerRow + firstPacketStart+col + 1]; // y1
dst[k].y2 = src[(origY+row)*BytesPerRow + firstPacketStart+col + 2]; // y2
dst[k].v = src[(origY+row)*BytesPerRow + firstPacketStart+col + 3]; // v
dst[k+1].u = dst[k].u;
dst[k+1].y1 = src[(origY+row)*BytesPerRow + firstPacketStart+col + 4]; // y3
dst[k+1].y2 = src[(origY+row)*BytesPerRow + firstPacketStart+col + 5]; // y4
dst[k+1].v = dst[k].v;
}
}
//mexPrintf("yuyv buffer contains %d elements\n", k);
}
//----------------------------------------------------------
//----------------------------------------------------------
// dispatcher for all conv<TYPE>_2_YUYV methods (above)
void convCurrentFrame2YUYV(myCameraModes mode, image_pixel *yuyvDest) {
unsigned char *cameraBuf; /* pointer raw data (camera) */
unsigned long cameraBufLength; /* number of bytes stored in the camera buffer (not used) */
/* get pointer to raw data (irrespective of the chosen format) */
cameraBuf = theCamera.GetRawData(&cameraBufLength);
switch(mode) {
case CAMERA_YUV444_160x120:
// convert YUV422 (UYUV) to YUYV (a mere copy - probably done inefficiently)
convYUV444_2_YUYV(yuyvDest, cameraBuf);
break;
case CAMERA_YUV422_320x240:
case CAMERA_YUV422_640x480:
// convert YUV422 (UYUV) to YUYV (a mere copy - probably done inefficiently)
convYUV422_2_YUYV(yuyvDest, cameraBuf);
break;
case CAMERA_YUV411_640x480:
// convert YUV411 (UYYVYY) to YUYV
convYUV411_2_YUYV(yuyvDest, cameraBuf);
break;
case CAMERA_RGB8_640x480:
// convert RGB8 (RGB) to YUYV
convCameraRGB_2_YUYV(yuyvDest, cameraBuf);
break;
case CAMERA_Y8_640x480:
// convert B&W (Y8) to YUYV
convY8_2_YUYV(yuyvDest, cameraBuf);
break;
case CAMERA_Y16_640x480:
// convert B&W (Y8) to YUYV
// not tested (fire-i does not support Y16, fw-01-08)
convY16_2_YUYV(yuyvDest, cameraBuf);
break;
default:
// do nothing (e.g. mode == -1)
;
} /* switch */
}
//----------------------------------------------------------
/* mySetFeature =========================================================== */
/* helper function to set a named feature (e.g. 'Brightness') to a specific value */
static void mySetFeature(const char *feature, int vLo, int vHi = 0) {
int fID = dc1394GetFeatureId(feature);
int aa;
if((pControl = theCamera.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 ========================================================== */
/* myConfigCamera =========================================================== */
static void myConfigCamera(void) {
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("Shutter", -1, 0);
mySetFeature("Saturation", -1, 0);
mySetFeature("White Balance", -1, -1);
mySetFeature("Gain", -1, 0);
mySetFeature("Auto Exposure", -1, 0);
mySetFeature("Brightness", -1, 0);
} else {
/* 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(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("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) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -