如何使用opencv和c++从网络摄像头框架中找到轮廓
How to find contours from a webcam frame using opencv and c++?
我的目标是通过从网络摄像头捕获帧来找到轮廓。我能够用静态图像做到这一点,但后来我试图在网络摄像头框架中使用相同的概念,它给了我这个错误:
"OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN
(type0) && ((1 << type0) & fixedDepthMask) != 0)) in cv::_OutputArray::create, f
ile C:builds2_4_PackSlave-win64-vc11-sharedopencvmodulescoresrcmatrix.cpp
, line 1486"
这是我在程序中用来查找等高线的代码;
Rng rng(12345);
Mat captureframe,con,threshold_output;
vector<vector<Point> > contours; //
vector<Vec4i> hierarchy;
while(true)
{
capturedevice>>captureframe;
con = captureframe.clone();
cvtColor(captureframe,con,CV_BGR2GRAY);
threshold( con, threshold_output, thresh, 255, THRESH_BINARY );
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
}
imshow("contour drawing",drawing);
}
我认为问题出在以下两行:
con = captureframe.clone();
cvtColor(captureframe,con,CV_BGR2GRAY);
在第一行中,您将con
作为captureFrame
的克隆,这意味着con
是3通道图像,而在第二行中,您正试图使con
成为灰度图像,这是1通道,因此您正在获得与图像类型相关的故障。
你应该尝试做以下(我不确定你的代码是否会在此之后运行,但你不应该在此之后得到当前错误):
con.create(captureframe.rows , captureframe.cols, CV_8UC1);
cvtColor(captureframe,con,CV_BGR2GRAY);
朋友们,非常感谢你们的帮助。我终于发现我的错误,我的申报有问题。我在网上寻找一些参考资料,然后我偶然发现了这个代码的对象检测。这家伙实际上是这样声明"轮廓"的——"std::vector <std::向量&>>contours;我的声明是"矢量轮廓"。我的声明适用于静态图像,但在从网络摄像头找到轮廓时,它给了我这个错误。谁能给我解释一下以上两种声明的区别吗?此外,根据skm的建议,我通过使用con.create(frame.rows,frame.cols,cv_8uc1)将帧捕获转换为1通道深度图像,然后将其转换为灰度图像。这一步非常关键。所以,这里是我完整的工作代码!!由于
VideoCapture capturedevice;
capturedevice.open(0);
Mat frame,con;
Mat grayframe;
std::vector < std::vector < cv::Point > >contours; //this is very important decalartion
while(true)
{
capturedevice>>frame;
con.create(frame.rows,frame.cols,CV_8UC1);
cvtColor(frame,con,CV_BGR2GRAY);
cv::findContours (con, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cv::drawContours (frame, contours, -1, cv::Scalar (0, 0, 255), 2);
imshow("frame", frame);
waitKey(33);
}
相关文章:
- 在没有Xcode的情况下在Mac捆绑包中嵌入框架
- catch框架有没有办法比较流或文件
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 使用Qt框架在c ++类中创建API调用
- 如何在ECS框架中更新组件数据和通知系统
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- 用于C++的静态二进制检测或二进制重写工具和框架
- GoogleMock是否仍然打算与任何测试框架一起使用?
- 带有框架的 QGraphicsTextItem 的 QPropertyAnimation 会使文本抖动
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 修改生成文件以简化框架 #include 路径
- 无敌框架并受到伤害等级
- 如何在提升构建中设置环境变量以进行提升单元测试框架?
- 使用 gtest 框架在单元测试代码中检查目标对象的私有变量的最佳实践是什么?
- Xcode 中的加速框架以获得 A 的 QR 分解,但找不到 zgeqrf
- 是否可以在 Xcode C++项目中包含框架?
- 我的精灵使用名为 ASGE 的C++框架卡在屏幕边缘
- 静态链接到 CMake 中的 macOS 框架
- Chromium 嵌入式框架
- 如何使用opencv和c++从网络摄像头框架中找到轮廓