圆函数:一次调用绘制多个圆
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。
这是我得到的:https://i.stack.imgur.com/bWfwa.jpg
你知道我的代码有什么问题吗?
谢谢你的帮助,我终于明白是怎么回事了,这都是我的错…
当你创建一个新的Mat时,你需要将图像的高度作为第一个参数传递给它,而不是宽度。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 绘制的 OpenGL 点消失,绘制调用和交换缓冲区问题
- 如何在调用函数时将对象绘制到 sfml 窗口上
- 调用 sf::矩形形状绘制函数
- OpenCV c++ 断言失败调用绘制轮廓
- 未调用QQuickPaintedItem的绘制方法
- 如何调用OnEraseBkgnd()在另一个函数中重新绘制窗口?(MFC)
- DirectX 11:绘制调用金额
- 在 win32 中调用WM_PAINT之间保留绘制的工作区
- c++ Glut 显示需要调用我的绘制函数,但我无法传递任何参数
- "QGraphicsView"滚动后不调用"绘制"方法
- 调用Qpainter的方法绘制以刷新图像和更改颜色
- OpenGL 批处理:为什么我的绘制调用超出了数组缓冲区边界
- 通过统一缓冲区对象发送到 GLSL 的数据会泄漏到其他绘制调用中 (OpenGL 3.2)
- 开放框架:绘制函数被多次调用
- QGraphicsScene一直在调用QGraphicsItem绘制事件
- Qt的子类QStyledItemDelegate绘制方法永远不会被调用
- openGL绘制调用之后会发生什么
- 圆函数:一次调用绘制多个圆
- 是否有可能在绘制其他Qt组件后强制Qt调用paintEvent ?