裁剪像素缓冲区

3
我正在使用以下方法将相机的图像数据转换为UIImage。为了节省时间和内存,我想在将其转换为UIImage之前裁剪图像数据。
理想情况下,我传入一个cropRect,并得到一个裁剪后的UIImage。但是,由于相机输出的大小可能基于我是否使用照片或视频预设而不同,因此我可能不知道要用于cropRect的尺寸。我可以使用类似于焦点或曝光点的cropRect,它使用介于(0,0)和(1,1)之间的CGPoint,并对cropRect的CGSize执行类似操作。或者,在调用以下内容之前获取sampleBuffer的尺寸,并传入适当的cropRect。我想知道应该使用哪种方法。
我还想知道如何最好地进行裁剪,以避免创建整个UIImage,然后将其裁剪回来。通常,我只关心保留约10-20%的像素。我假设我必须迭代像素,并开始将cropRect复制到不同的像素缓冲区中,直到获得所有所需的像素。
请注意,根据方向可能会发生旋转。
+ (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer orientation:(UIImageOrientation) orientation
{
    // Create a UIImage from sample buffer data
    // Get a CMSampleBuffer's Core Video image buffer for the media data
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    // Lock the base address of the pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer, 0); 

    // Get the number of bytes per row for the pixel buffer
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

    // Get the number of bytes per row for the pixel buffer
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    // Get the pixel buffer width and height
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    // Create a device-dependent RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    // Create a bitmap graphics context with the sample buffer data
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
                                                 bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
    // Unlock the pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    // Free up the context and color space
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace);

    // Create an image object from the Quartz image
    UIImage *image = [UIImage imageWithCGImage:quartzImage scale:(CGFloat)1.0 orientation:orientation];
    // Release the Quartz image
    CGImageRelease(quartzImage);

    return (image);
}

总之:

  1. 我应该传递一个指定在(0,0,0,0)和(1,1,1,1)之间的矩形的cropRect,还是传递一个指定像素位置如(50,50,100,100)的cropRect?
  2. 最好怎样裁剪像素缓冲区?
1个回答

2

我认为你应该使用像素作为cropRect的单位,因为你必须在某个时候将浮点值转换为像素值。以下代码未经测试,但应该能给你一个思路。

CGRect cropRect = CGRectMake(50, 50, 100, 100); // cropRect
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVReturn lock = CVPixelBufferLockBaseAddress(pixelBuffer, 0);
if (lock == kCVReturnSuccess) {
    int w = 0;
    int h = 0;
    int r = 0;
    int bytesPerPixel = 0;
    unsigned char *buffer;
    w = CVPixelBufferGetWidth(pixelBuffer);
    h = CVPixelBufferGetHeight(pixelBuffer);
    r = CVPixelBufferGetBytesPerRow(pixelBuffer);
    bytesPerPixel = r/w;
    buffer = CVPixelBufferGetBaseAddress(pixelBuffer);
    UIGraphicsBeginImageContext(cropRect.size); // create context for image storage, use cropRect as size
    CGContextRef c = UIGraphicsGetCurrentContext();
    unsigned char* data = CGBitmapContextGetData(c);
    if (data != NULL) {
        // iterate over the pixels in cropRect
        for(int y = cropRect.origin.y, yDest = 0; y<CGRectGetMaxY(cropRect); y++, yDest++) { 
            for(int x = cropRect.origin.x, xDest = 0; x<CGRectGetMaxX(cropRect); x++, xDest++) {
                int offset = bytesPerPixel*((w*y)+x); // offset calculation in cropRect
                int offsetDest = bytesPerPixel*((cropRect.size.width*yDest)+xDest); // offset calculation for destination image
                for (int i = 0; i<bytesPerPixel; i++) {
                    data[offsetDest+i]   = buffer[offset+i];
                }
            }
        }
    } 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

关掉,我想。看起来每次读取一行的时候会少一点,所以我得到了对角线条纹。我今晚稍后会调试它。我必须注意以下几点:图像来自相机,因此它们都是横向的(您必须将其向右旋转90度以与我的视图的x,y对齐)。此外,图像很大,因此上面使用的cropRect很小 - 我需要将该裁剪矩形从我的视图尺寸转换为图像尺寸。我会在这里发布任何更正。 - mahboudz
我正在正确的轨道上。每行有12个字节的填充。行被填充到32的倍数?以下行纠正了图像,但我的坐标仍然偏离(但我认为这只是一个简单的翻译):int offset = bytesPerPixel * (((w + 12) * y) + x); // 在cropRect中进行偏移量计算更好的修复方法是:int offset = bytesPerPixel * x + r * y; // 在cropRect中进行偏移量计算目标pixbuff也应该是32字节对齐的吗? - mahboudz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接