Post not yet marked as solved
Post marked as unsolved with 1 replies, 1,168 views
My mlmodel is similar to style transfer models input and output are images. It is a version of UnetThe input is a color image and the output is a grayscale image.this is what model's in .h file showsinput: /// image as color (kCVPixelFormatType_32BGRA) image buffer, 224 pixels wide by 224 pixels high
open var image: CVPixelBuffer
public init(image: CVPixelBuffer)output: /// mask as grayscale (kCVPixelFormatType_OneComponent8) image buffer, 224 pixels wide by 224 pixels high
open var mask: CVPixelBuffer
publicinit(mask: CVPixelBuffer)I am putting the image from Mat object (OpenCV) to the model and attempt to return output into another Mat object.before feeding image to the model I transform it Mat -> CVPixelBuffer -> MLMODEL -> CVPixelBuffer -> MatHere is how I use the modeleverything works fine until I attempt to transform output into Mat (line 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->_myModel predictionFromImage: imageIn error:&error];
Mat imageOut = [self imageBufferToMat: output.mask];Iuse the following methods:(color) Mat -> CVPixelBuffer works OK- (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;
}(grayscale) CVPixelBuffer -> Mat here are problems ( line 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;
}While attempting to run the code I get error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x59b2b0290)The closest thing I narrowed is the grayscale nature of the output kCVPixelFormatType_OneComponent8.Am I doing something wrong?Is there another approach when model output is grayscale?Please help. Thanks in advance