bool CvCaptureCAM_DC1394_v2_CPP::grabFrame() { dc1394capture_policy_t policy = DC1394_CAPTURE_POLICY_WAIT; bool code = false, isColor; dc1394video_frame_t *dcFrame = 0, *fs = 0; int i, nch; if (!dcCam || (!started && !startCapture())) return false; dc1394_capture_dequeue(dcCam, policy, &dcFrame); if (!dcFrame) return false; if (/*dcFrame->frames_behind > 1 ||*/ dc1394_capture_is_frame_corrupt(dcCam, dcFrame) == DC1394_TRUE) { goto _exit_; } isColor = dcFrame->color_coding != DC1394_COLOR_CODING_MONO8 && dcFrame->color_coding != DC1394_COLOR_CODING_MONO16 && dcFrame->color_coding != DC1394_COLOR_CODING_MONO16S; if (nimages == 2) { fs = (dc1394video_frame_t*)calloc(1, sizeof(*fs)); //dc1394_deinterlace_stereo_frames(dcFrame, fs, DC1394_STEREO_METHOD_INTERLACED); dc1394_deinterlace_stereo_frames_fixed(dcFrame, fs, DC1394_STEREO_METHOD_INTERLACED); dc1394_capture_enqueue(dcCam, dcFrame); // release the captured frame as soon as possible dcFrame = 0; if (!fs->image) goto _exit_; isColor = colorStereo; } nch = isColor ? 3 : 1; for (i = 0; i < nimages; i++) { IplImage fhdr; dc1394video_frame_t f = fs ? *fs : *dcFrame, *fc = &f; f.size[1] /= nimages; f.image += f.size[0] * f.size[1] * i; // TODO: make it more universal if (isColor) { if (!frameC) frameC = (dc1394video_frame_t*)calloc(1, sizeof(*frameC)); frameC->color_coding = nch == 3 ? DC1394_COLOR_CODING_RGB8 : DC1394_COLOR_CODING_MONO8; if (nimages == 1) { dc1394_convert_frames(&f, frameC); dc1394_capture_enqueue(dcCam, dcFrame); dcFrame = 0; } else { f.color_filter = bayerFilter; dc1394_debayer_frames(&f, frameC, bayer); } fc = frameC; } if (!img[i]) img[i] = cvCreateImage(cvSize(fc->size[0], fc->size[1]), 8, nch); cvInitImageHeader(&fhdr, cvSize(fc->size[0], fc->size[1]), 8, nch); cvSetData(&fhdr, fc->image, fc->size[0]*nch); // Swap R&B channels: if (nch==3) cvConvertImage(&fhdr,&fhdr,CV_CVTIMG_SWAP_RB); if( rectify && cameraId == VIDERE && nimages == 2 ) { if( !maps[0][0] || maps[0][0]->width != img[i]->width || maps[0][0]->height != img[i]->height ) { CvSize size = cvGetSize(img[i]); cvReleaseImage(&maps[0][0]); cvReleaseImage(&maps[0][1]); cvReleaseImage(&maps[1][0]); cvReleaseImage(&maps[1][1]); maps[0][0] = cvCreateImage(size, IPL_DEPTH_16S, 2); maps[0][1] = cvCreateImage(size, IPL_DEPTH_16S, 1); maps[1][0] = cvCreateImage(size, IPL_DEPTH_16S, 2); maps[1][1] = cvCreateImage(size, IPL_DEPTH_16S, 1); char buf[4*4096]; if( getVidereCalibrationInfo( buf, (int)sizeof(buf) ) && initVidereRectifyMaps( buf, maps[0], maps[1] )) ; else rectify = false; } cvRemap(&fhdr, img[i], maps[i][0], maps[i][1]); } else cvCopy(&fhdr, img[i]); } code = true; _exit_: if (dcFrame) dc1394_capture_enqueue(dcCam, dcFrame); if (fs) { if (fs->image) free(fs->image); free(fs); } return code; }
IplImage *dc1394_frame_get_iplimage(dc1394video_frame_t *frame) { g_return_val_if_fail(frame != NULL, NULL); g_return_val_if_fail(frame->padding_bytes == 0, NULL); IplImage *img; unsigned char *imdata; dc1394video_mode_t video_mode = frame->video_mode; CvSize size = cvSize(frame->size[0], frame->size[1]); if (video_mode == DC1394_VIDEO_MODE_640x480_MONO8) { g_return_val_if_fail( (size.width * size.height * 1 * sizeof(unsigned char)) == frame->image_bytes, NULL); IplImage *tmp = cvCreateImageHeader(size, IPL_DEPTH_8U, 1); cvSetData(tmp, frame->image, size.width); img = cvCreateImage(size, IPL_DEPTH_8U, tmp->nChannels); cvCopy(tmp, img, 0); cvReleaseImageHeader(&tmp); } else if (video_mode == DC1394_VIDEO_MODE_640x480_MONO16) { g_return_val_if_fail( (size.width * size.height * 2 * sizeof(unsigned char)) == frame->image_bytes, NULL); IplImage *tmp = cvCreateImageHeader(size, IPL_DEPTH_16U, 1); cvSetData(tmp, frame->image, size.width*2); img = cvCreateImage(size, IPL_DEPTH_16U, tmp->nChannels); cvCopy(tmp, img, 0); cvReleaseImageHeader(&tmp); } else if ((video_mode == DC1394_VIDEO_MODE_FORMAT7_0) || (video_mode == DC1394_VIDEO_MODE_FORMAT7_1)) { dc1394error_t err; dc1394video_frame_t dest; IplImage *tmp; img = cvCreateImageHeader(size, IPL_DEPTH_8U, 3); /* debayer frame into RGB8 */ imdata = (unsigned char *)malloc(frame->size[0]*frame->size[1]*3*sizeof(unsigned char)); dest.image = imdata; dest.color_coding = DC1394_COLOR_CODING_RGB8; err=dc1394_debayer_frames(frame, &dest, DC1394_BAYER_METHOD_NEAREST); if (err != DC1394_SUCCESS) dc1394_log_error("Could not convert/debayer frames"); /* convert from RGB to BGR */ tmp = cvCreateImageHeader(cvSize(frame->size[0], frame->size[1]), IPL_DEPTH_8U, 3); cvSetData(tmp, imdata, frame->size[0]*3); cvCvtColor(tmp, img, CV_RGB2BGR); free(imdata); cvReleaseImageHeader(&tmp); } else { g_assert_not_reached(); } return img; }