AVFoundationを使用してビデオを撮影しています。私はkCVPixelFormatType_420YpCbCr8BiPlanarFullRange
フォーマットで録画しています。 YpCbCr形式のY面から直接グレースケール画像を作成したい。AVFoundation - Yプレーンからグレースケール画像を取得する(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
私はCGBitmapContextCreate
を呼び出すことによってCGContextRef
を作成しようとしましたが、問題は色空間とピクセル形式を選択するのがわかりません。
<Error>: CGBitmapContextCreateImage: invalid context 0x0
<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaPremultipliedLast; 192 bytes/row.
PS:私の英語のため申し訳ありませんが、私は上記のコードを実行すると
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
/* Get informations about the Y plane */
uint8_t *YPlaneAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);
size_t width = CVPixelBufferGetWidthOfPlane(imageBuffer, 0);
size_t height = CVPixelBufferGetHeightOfPlane(imageBuffer, 0);
/* the problematic part of code */
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef newContext = CGBitmapContextCreate(YPlaneAddress,
width, height, 8, bytesPerRow, colorSpace, kCVPixelFormatType_1Monochrome);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
UIImage *grayscaleImage = [[UIImage alloc] initWithCGImage:newImage];
// process the grayscale image ...
}
は、私はこのエラーを得ました。
おかげでたくさん!これは、カメラからグレースケールの画像を取得するための最速のアプローチであることは間違いありませんか? – user961912
@ user961912:YpCbCrでのビデオフレームへのアクセスは、WWDCプレゼンテーションに準拠したネイティブフォーマットであるため、高速になるはずです。次のステップでは、最終的に画像で何をするかによって異なります。バーコードスキャンのようないくつかのアプリケーションでは、CGImageやUIImageを作成する必要はありません。 – Codo
ちょっとコド、この回答ありがとう!私は "同じ"結果のビットを達成しようとしている、違いは、私はグレースケールの効果を達成しようとしている、各フレーム(ライブビデオストリーム)、あなたは正しい方向に私を向けることができる任意のチャンス? –