私はこれを使用しています。私のcv:Mat
はBGR(8UC3)colorFormatに設定されています。
CVImageBufferRef - > CV ::マット
- (cv::Mat) matFromImageBuffer: (CVImageBufferRef) buffer {
cv::Mat mat ;
CVPixelBufferLockBaseAddress(buffer, 0);
void *address = CVPixelBufferGetBaseAddress(buffer);
int width = (int) CVPixelBufferGetWidth(buffer);
int height = (int) CVPixelBufferGetHeight(buffer);
mat = cv::Mat(height, width, CV_8UC4, address, 0);
//cv::cvtColor(mat, _mat, CV_BGRA2BGR);
CVPixelBufferUnlockBaseAddress(buffer, 0);
return mat;
}
CV ::マット - > CVImageBufferRef(CVPixelBufferRef)
- (CVImageBufferRef) getImageBufferFromMat: (cv::Mat) mat {
cv::cvtColor(mat, mat, CV_BGR2BGRA);
int width = mat.cols;
int height = self.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;
}
こんにちは@Robert、あなたはあなたにある反対のことを行うことができますどのように知っています例えば、cv :: MatをCVPixelBufferまたはサンプルバッファに変換しますか?感謝。 – lilouch
基本的なピクセル配列がメモリ内で連続していないため、以下のコードは、iPhone 6およびiPhone 6 +とiOS 10.2で動作しません。私の更新された回答はこちらhttp://stackoverflow.com/questions/12355257/open-cv-ios-video-processing/43523459#43523459をご覧ください。 – PalmRobotZ
スウィフトはどうですか? – user924