cv :: Mat与UIImageView宽度不匹配?

时间:2013-03-06 22:54:00

标签: ios opencv width avfoundation frame

我正在使用AVFoundation捕获视频帧,使用opencv处理并在新iPad上的UIImageView中显示结果。 opencv进程执行以下操作(“inImg”是视频帧):

cv::Mat testROI = inImg.rowRange(0,100);
testROI = testROI.colRange(0,10);
testROI.setTo(255); // this is a BGRA frame.

然而,我没有在画框的左上角有一个垂直的白条(100行x 10列),而是从右上角到左下角有100条阶梯式水平线,每条长10像素

经过一番调查,我意识到显示帧的宽度似乎比cv :: Mat宽8个像素。 (即,第2行的第9个像素位于第1行的第1个像素的正下方)。

视频帧本身正确显示(行之间没有位移)。 当AVCaptureSession.sessionPreset是AVCaptureSessionPresetMedium(帧行= 480,cols = 360)但是当它是AVCaptureSessionPresetHigh(帧行= 640,cols = 480)时不出现问题。

全屏显示360列。 (我尝试遍历并逐个像素地修改cv :: Mat。像素1-360显示正确.361-368消失,369显示在像素1下面。)

我尝试了imageview.contentMode(UIViewContentModeScaleAspectFill和UIViewContentModeScaleAspectFit)和imageview.clipsToBound(YES / NO)的组合,但没有运气。

可能是什么问题? 非常感谢你。

我使用以下代码创建AVCaptureSession:

NSArray* devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

if ([devices count] == 0) {
    NSLog(@"No video capture devices found");
    return NO;
}


for (AVCaptureDevice *device in devices) {
     if ([device position] == AVCaptureDevicePositionFront) {
           _captureDevice = device;
     }
}


NSError* error_exp = nil;
if ([_captureDevice lockForConfiguration:&error_exp]) {
    [_captureDevice setWhiteBalanceMode:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance];
    [_captureDevice unlockForConfiguration];
}
// Create the capture session
_captureSession = [[AVCaptureSession alloc] init];
_captureSession.sessionPreset = AVCaptureSessionPresetMedium;


// Create device input
NSError *error = nil;
AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:_captureDevice error:&error];

// Create and configure device output
_videoOutput = [[AVCaptureVideoDataOutput alloc] init];

dispatch_queue_t queue = dispatch_queue_create("cameraQueue", NULL); 
[_videoOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue); 

_videoOutput.alwaysDiscardsLateVideoFrames = YES; 

OSType format = kCVPixelFormatType_32BGRA;

_videoOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:format]forKey:(id)kCVPixelBufferPixelFormatTypeKey];


// Connect up inputs and outputs
if ([_captureSession canAddInput:input]) {
    [_captureSession addInput:input];
}

if ([_captureSession canAddOutput:_videoOutput]) {
    [_captureSession addOutput:_videoOutput];
}

AVCaptureConnection * captureConnection = [_videoOutput connectionWithMediaType:AVMediaTypeVideo];

if (captureConnection.isVideoMinFrameDurationSupported)
    captureConnection.videoMinFrameDuration = CMTimeMake(1, 60);
if (captureConnection.isVideoMaxFrameDurationSupported)
    captureConnection.videoMaxFrameDuration = CMTimeMake(1, 60);

if (captureConnection.supportsVideoMirroring)
    [captureConnection setVideoMirrored:NO];

[captureConnection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];

收到帧时,会调用以下内容:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
@autoreleasepool {

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    OSType format = CVPixelBufferGetPixelFormatType(pixelBuffer);
    CGRect videoRect = CGRectMake(0.0f, 0.0f, CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer));

    AVCaptureConnection *currentConnection = [[_videoOutput connections] objectAtIndex:0];

    AVCaptureVideoOrientation videoOrientation = [currentConnection videoOrientation];
    CGImageRef quartzImage;

    // For color mode a 4-channel cv::Mat is created from the BGRA data
    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
    void *baseaddress = CVPixelBufferGetBaseAddress(pixelBuffer);

    cv::Mat mat(videoRect.size.height, videoRect.size.width, CV_8UC4, baseaddress, 0);

    if ([self doFrame]) { // a flag to switch processing ON/OFF
            [self processFrame:mat videoRect:videoRect videoOrientation:videoOrientation];  // "processFrame" is the opencv function shown above
    }

    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
    quartzImage = [self.context createCGImage:ciImage fromRect:ciImage.extent];
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

    UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0 orientation:UIImageOrientationUp];

    CGImageRelease(quartzImage);

    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];

1 个答案:

答案 0 :(得分:1)

我假设您正在使用构造函数Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP),并且AUTO_STEP为0并假设行跨度为width*bytesPerPixel

这通常是错误的 - 将行与一些较大的边界对齐是非常。在这种情况下,360不是16的倍数,而368是;强烈暗示它与16像素边界对齐(可能是为了协助处理16×16块的算法?)。

尝试

cv::Mat mat(videoRect.size.height, videoRect.size.width, CV_8UC4, baseaddress, CVPixelBufferGetBytesPerRow(pixelBuffer));