cv:circle函数通过一次调用绘制多个圆圈

时间:2015-05-27 18:42:24

标签: c++ ios ipad opencv objective-c++

我是OpenCV库的新手,我想用它来检测从iPad的后置摄像头拍摄的视频流中的圆圈。我想出了如何使用OpenCV 2.4.2,它可以在不到10行代码中完成。但它对我不起作用,我想我错过了一些东西,因为我得到了一些奇怪的行为。

代码非常简单,每次相机捕获新帧时,都会在Objective-C回调触发器中开始。以下是我在此回调中所做的事情:

- (void)captureOutput:(AVCaptureOutput *)captureOutput
       didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{
    // Convert CMSampleBufferRef to CVImageBufferRef
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    // Lock pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer, kCVPixelBufferLock_ReadOnly);

    // Construct VideoFrame struct
    uint8_t *baseAddress = (uint8_t*)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    size_t stride = CVPixelBufferGetBytesPerRow(imageBuffer);

    // Unlock pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);

    std::vector<unsigned char> data(baseAddress, baseAddress + (stride * height));

    // Call C++ function with these arguments => (data, (int)width, (int)height)
}

这是使用OpenCV处理图像的C ++函数:

void proccessImage(std::vector<unsigned char>& imageData, int width, int height)
{
    // Create cv::Mat from std::vector<unsigned char>
    Mat src(width, height, CV_8UC4, const_cast<unsigned char*>(imageData.data()));
    Mat final;

    // Draw a circle at position (300, 200) with a radius of 30
    cv::Point center(300, 200);
    circle(src, center, 30.f, CV_RGB(0, 0, 255), 3, 8, 0);

    // Convert the gray image to RGBA
    cvtColor(src, final, CV_BGRA2RGBA);

    // Reform the std::vector from cv::Mat data
    std::vector<unsigned char> array;
    array.assign((unsigned char*)final.datastart, (unsigned char*)final.dataend);

    // Send final image data to GPU and draw it
}

从iPad的后置摄像头检索的图像采用BGRA(32位)格式。

我所期待的是来自iPad的后置摄像头的图像,在x = 300px,y = 200px,半径为30px的位置绘制了一个简单的圆圈。

这就是我得到的:http://i.stack.imgur.com/bWfwa.jpg

你知道我的代码出了什么问题吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,我终于弄清楚发生了什么,这是我的全部错误......

当您创建新Mat时,您需要将图像的高度作为第一个参数传递,而不是宽度。如果我切换参数,则正确绘制圆圈。