Image 将CVPixelBufferRef样本从4:3转换为16:9

Image 将CVPixelBufferRef样本从4:3转换为16:9,image,avfoundation,crop,cmsamplebuffer,Image,Avfoundation,Crop,Cmsamplebuffer,我尝试在视频捕获管道中将4:3帧转换为16:9帧。转换后的帧需要进一步处理。因此,我需要将覆盖的框架保持为CVImageBufferRef。 我查看了这个堆栈溢出线程,并从中借用了一些想法 以下是我所做的: int cropX0 = 0, cropY0 = 60, cropHeight = 360, cropWidth = 640, outWidth = 640, outHeight = 360; //get CVPixel buffer from CMSampleBuffer CVPixel

我尝试在视频捕获管道中将4:3帧转换为16:9帧。转换后的帧需要进一步处理。因此,我需要将覆盖的框架保持为
CVImageBufferRef
。 我查看了这个堆栈溢出线程,并从中借用了一些想法

以下是我所做的:

int cropX0 = 0, cropY0 = 60, cropHeight = 360, cropWidth = 640, outWidth = 640, outHeight = 360;
//get CVPixel buffer from CMSampleBuffer
CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(cmSampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);

size_t startpos = cropY0 * bytesPerRow;
void* cropStartAddr = ((char*)baseAddress) + startpos;

CVPixelBufferRef cropPixelBuffer = NULL;
int status = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
                                          outWidth,
                                          outHeight,
                                          CVPixelBufferGetPixelFormatType(imageBuffer),
                                          cropStartAddr,
                                          bytesPerRow,
                                          NULL, 
                                          0,
                                          NULL, 
                                          &cropPixelBuffer);
if(status == 0){
    OSStatus result = 0;
}
但是在这种方法之后。如果我们从Xcode检查Croppexelbuffer。它看起来像一个损坏的图像

原图是这样的

我认为这可能是因为颜色格式是NV12 yuv像素格式
感谢您的帮助

如果您的图像数据是yuv,那么您可能需要处理多个平面数据(例如多个
行字节
、多个
基地址
和多个
宽度
高度
和多个作物),在这种情况下,您必须使用

CVPixelBufferCreateWithPlanarBytes()

那么
CVPixelBufferGetPixelFormatType(imageBuffer)
返回什么呢?

格式是kCVPixelFormatType\u 420ypcbcr8biplanarvideogrange。我想我需要分别处理UV,这意味着你要裁剪两个位图