从CvVideoCamera将Mat转换为UIImage

时间:2015-07-05 08:54:31

标签: c++ ios objective-c opencv uiimage

我使用CvVideoCamera委托方法从- (void)processImage:(Mat&)image实时存储框架。

存储图像后,我将它们全部平均为一张图像,以使用以下代码复制长曝光照片:

Mat merge(Vector<Mat> frames, double alpha)
{
    Mat firstFrame = frames.front();
    Mat exposed = firstFrame * alpha;

    for (int i = 1; i < frames.size(); i++) {

        Mat frame = frames[i];
        exposed += frame * alpha;
    }
    return exposed;
}

获得平均图像后,我将其转换回UIImage,但我回来的图像处于一个奇怪的颜色空间,是否有人知道如何解决这个问题?

转换代码:

+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
{
    NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
    CGColorSpaceRef colorSpace;
    if ( cvMat.elemSize() == 1 ) {
        colorSpace = CGColorSpaceCreateDeviceGray();
    }
    else {
        colorSpace = CGColorSpaceCreateDeviceRGB();
    }
    CGDataProviderRef provider = CGDataProviderCreateWithCFData( (__bridge CFDataRef)data );
    CGImageRef imageRef = CGImageCreate( cvMat.cols, cvMat.rows, 8, 8 * cvMat.elemSize(), cvMat.step[0], colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault, provider, NULL, false, kCGRenderingIntentDefault );
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease( imageRef );
    CGDataProviderRelease( provider );
    CGColorSpaceRelease( colorSpace );
    return finalImage;
}

这是一个例子(注意:中间的飞机是因为我正在录制电脑显示器) enter image description here

enter image description here

0 个答案:

没有答案