Pixel buffer cropping

I use the following to convert camera image data to UIImage. To save time and memory, I would like to crop the image data before turning it into a UIImage.

Ideally, I go into cropRect and return the trimmed UIImage. However, since the camera output may be different in size, based on whether I use a preliminary set of photos or videos, I probably don’t know what sizes to use for cropRect. I could use a cropRect similar to a focus or exposure point, which uses CGPoint between (0,0) and (1,1) and similarly performs a CGSizeof cropRect. Or I can get the sampleBuffer dimensions before I call the following, and go to the appropriate cropRect value. I would like advice on what I should use.

I would also like to know what is the best way to crop so as not to create the whole UIImage and then crop it. As a rule, I am only interested in 10-20% of the pixels. I guess I need to iterate over the pixels and start copying cropRect to another pixel buffer until I have all the pixels that I want.

And keep in mind that there is a possible rotation in accordance with orientation.

+ (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer orientation:(UIImageOrientation) orientation
{
    // Create a UIImage from sample buffer data
    // Get a CMSampleBuffer 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);
}

In short:

  • Should I pass in cropRect, which indicates a line between (0,0,0,0) and (1,1,1,1), or should I pass in cropRect, which indicates exact pixel locations, such as (50,50,100,100) ?
  • What is the best way to crop a pixel buffer?
+3
source share
1 answer

, pixel cropRect, float - . , .

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();
}
+2

All Articles