static Mat iplImageToMat(const IplImage* img, bool copyData) { Mat m; if( !img ) return m; m.dims = 2; CV_DbgAssert(CV_IS_IMAGE(img) && img->imageData != 0); int imgdepth = IPL2CV_DEPTH(img->depth); size_t esz; m.step[0] = img->widthStep; if(!img->roi) { CV_Assert(img->dataOrder == IPL_DATA_ORDER_PIXEL); m.flags = Mat::MAGIC_VAL + CV_MAKETYPE(imgdepth, img->nChannels); m.rows = img->height; m.cols = img->width; m.datastart = m.data = (uchar*)img->imageData; esz = CV_ELEM_SIZE(m.flags); } else { CV_Assert(img->dataOrder == IPL_DATA_ORDER_PIXEL || img->roi->coi != 0); bool selectedPlane = img->roi->coi && img->dataOrder == IPL_DATA_ORDER_PLANE; m.flags = Mat::MAGIC_VAL + CV_MAKETYPE(imgdepth, selectedPlane ? 1 : img->nChannels); m.rows = img->roi->height; m.cols = img->roi->width; esz = CV_ELEM_SIZE(m.flags); m.datastart = m.data = (uchar*)img->imageData + (selectedPlane ? (img->roi->coi - 1)*m.step*img->height : 0) + img->roi->yOffset*m.step[0] + img->roi->xOffset*esz; } m.datalimit = m.datastart + m.step.p[0]*m.rows; m.dataend = m.datastart + m.step.p[0]*(m.rows-1) + esz*m.cols; m.step[1] = esz; m.updateContinuityFlag(); if( copyData ) { Mat m2 = m; m.release(); if( !img->roi || !img->roi->coi || img->dataOrder == IPL_DATA_ORDER_PLANE) m2.copyTo(m); else { int ch[] = {img->roi->coi - 1, 0}; m.create(m2.rows, m2.cols, m2.type()); mixChannels(&m2, 1, &m, 1, ch, 1); } } return m; }
/* * Grabs, decodes and returns the next video frame. * @overload query * @return [IplImage] Next video frame * @return [nil] Failed to read next video frame * @opencv_func cvQueryFrame */ VALUE rb_query(VALUE self) { VALUE image = Qnil; IplImage *frame = NULL; try { if (!(frame = cvQueryFrame(CVCAPTURE(self)))) { return Qnil; } image = cIplImage::new_object(frame->width, frame->height, CV_MAKETYPE(IPL2CV_DEPTH(frame->depth), frame->nChannels)); if (frame->origin == IPL_ORIGIN_TL) { cvCopy(frame, CVARR(image)); } else { cvFlip(frame, CVARR(image)); } } catch (cv::Exception& e) { raise_cverror(e); } return image; }