如何将CVPixelBuffer转换为Mat或UIImage Objective-C

时间:2018-11-02 07:17:09

标签: opencv mat cvpixelbuffer

当尝试转换灰度CVPixelBuffer时出现错误:

 Thread 1: EXC_BAD_ACCESS (code=1, address=0x59b2b0290)

CVPixelBuffer的类型为kCVPixelFormatType_OneComponent8。 我的mlmodel类似于样式转换模型,输入和输出都是图像。它是Unet的一个版本 输入是彩色图像,输出是灰度图像。

.h文件中的模型所显示的 输入:

/// image as color (kCVPixelFormatType_32BGRA) image buffer, 224 pixels wide by 224 pixels high  
open var image: CVPixelBuffer  
public init(image: CVPixelBuffer)  

输出:

/// mask as grayscale (kCVPixelFormatType_OneComponent8) image buffer, 224 pixels wide by 224 pixels high  
open var mask: CVPixelBuffer  
publicinit(mask: CVPixelBuffer)  

我正在将Mat对象(OpenCV)中的图像放入模型中,并尝试将输出返回到另一个Mat对象中。

在将图像输入模型之前,先将其转换为Mat-> CVPixelBuffer-> MLMODEL-> CVPixelBuffer-> Mat

这是我使用模型的方式 一切正常,直到我尝试将输出转换为Mat(第8行)         Mat toSqeeze = obrazek.clone();

    cvtColor(toSqeeze, toSqeeze, CV_BGRA2BGR);  
    resize(toSqeeze, toSqeeze, cv::Size(224,224));  
    imageIn = [self matToImageBuffer: toSqeeze];  
    NSError *error;  
    output = [self->_squeezU predictionFromImage: imageIn error:&error];   
    Mat imageOut = [self imageBufferToMat: output.mask];  

我 使用以下方法:

(彩色)垫子-> CVPixelBuffer正常工作

- (CVImageBufferRef) matToImageBuffer: (cv::Mat) mat  
{  

cv::cvtColor(mat, mat, CV_BGR2BGRA);  

int width = mat.cols;  
int height = mat.rows;  

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:  
                        [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,  
                         [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,  
                         [NSNumber numberWithInt:width], kCVPixelBufferWidthKey,  
                         [NSNumber numberWithInt:height], kCVPixelBufferHeightKey,  
                         nil];  

CVPixelBufferRef imageBuffer;  
CVReturn status = CVPixelBufferCreate(kCFAllocatorMalloc, width, height, kCVPixelFormatType_32BGRA, (CFDictionaryRef) CFBridgingRetain(options), &imageBuffer) ;  


NSParameterAssert(status == kCVReturnSuccess && imageBuffer != NULL);  

CVPixelBufferLockBaseAddress(imageBuffer, 0);  
void *base = CVPixelBufferGetBaseAddress(imageBuffer) ;  
memcpy(base, mat.data, mat.total()*4);  
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);  

return imageBuffer;  

}
(灰度)CVPixelBuffer-> Mat出现了问题(第5行)

- (cv::Mat) imageBufferToMat: (CVImageBufferRef) buffer  
{  
cv::Mat mat ;  

CVPixelBufferLockBaseAddress(buffer, 0); // I get here <- EXC_BAD_ACCESS (code=1, address=0x59b2b0290)  

void *address = CVPixelBufferGetBaseAddress(buffer);  
int width = (int) CVPixelBufferGetWidth(buffer);  
int height = (int) CVPixelBufferGetHeight(buffer);  

mat   = cv::Mat(height, width, CV_8UC1, address, 0);     
CVPixelBufferUnlockBaseAddress(buffer, 0);  
return mat;  

}

尝试运行代码时出现错误:线程1:

EXC_BAD_ACCESS (code=1, address=0x59b2b0290)

我缩小的最接近的是输出kCVPixelFormatType_OneComponent8的灰度性质。

我做错什么了吗? 当模型输出为灰度时,还有另一种方法吗? 请帮忙。提前致谢 我很高兴在ObjectiveC或Swift中有解决方案,我可以通过网桥使用后者。

0 个答案:

没有答案