2010-12-07 199 views
2

我正在制作一个应用程序,它使用OpenCV解析iPhone摄像头的输出并在屏幕上显示结果。我知道iPhone相机横向读取,所以需要在屏幕上显示结果之前进行转换。我想我只需要缩放,旋转-90度然后平移(tx =图像宽度),但那不起作用。我在网上发现了一些示例代码,下面是示例代码,但对于左侧以外的任何方向都不起作用。如何将iPhone摄像头坐标转换为视图坐标?

什么是从摄像机视图转换为用户视图的正确方法?

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 
{ 
    //lock the pixel buffer 
    CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    CVPixelBufferLockBaseAddress(pixelBuffer, 0); 

    //declare variables 
    int bufferHeight = CVPixelBufferGetHeight(pixelBuffer); 
    int bufferWidth = CVPixelBufferGetWidth(pixelBuffer); 

    //a bunch of OpenCV stuff that finds features in an image and 
    // returns a cameraRect 

    //translate to view coordinates 
    float scaleX = 320/(float)bufferHeight; 
    float scaleY = 460/(float)bufferWidth; 
    deviceRect.origin.x = 320 - ((cameraRect.y + cameraRect.height) * scaleX); 
    deviceRect.origin.y =  (cameraRect.x * scaleY); 

    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); 
} 

//elsewhere 
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 

编辑:如果不工作,我的意思是,屏幕上显示的矩形不对应匹配的对象,它是在预览层可见。我正在检测脸部,并且取决于屏幕上绘制的手机deviceRect的方向与预览图层中的脸部有关或很少。在横向左侧,deviceRect与可见面完全匹配。

+0

你说它不起作用 - 你能提供一些它不工作的细节吗? – Greg 2010-12-07 20:25:09

回答

0
CGRectMake((640-puntos[1])*480/640-135, puntos[0]*320/480+25,5, 5); 

我正在做这个转换,它正在或多或少的工作。

相关问题