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

cv:circle function draw multiple circles with a single call

本文关键字:调用 绘制 一次 函数      更新时间:2023-10-16

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

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

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

谢谢你的帮助,我终于明白是怎么回事了,这都是我的错…

当你创建一个新的Mat时,你需要将图像的高度作为第一个参数传递给它,而不是宽度。