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

cv:circle function draw multiple circles with a single call

我是 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

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

提前致谢。

感谢您的帮助,我终于弄明白了,这全是我的错...

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