我在一个多小时前完成了这段代码,对它进行了测试并且运行正常。当我回来它不再工作,并给我上面显示的错误。我快速咬了一口,回来试图再次运行它突然又恢复了。但是现在它又一次拒绝运行并且不断向我发出同样的错误。
我使用我的网络摄像头作为标准输入设备,如果我尝试使用相同的代码运行不同的程序来开始视频输入,它可以正常工作。
老实说,我不知道这里发生了什么,我的代码应该正常工作.. 有人可以提供帮助吗?
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
using namespace cv;
/// Function header
void thresh_callback(int, void* );
/** @function main */
int main( int argv , char** argc )
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return 0;
Mat edges;
namedWindow("edges",1);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
while(1)
{
RNG rng(12345);
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, COLOR_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 120, 3);
findContours( edges, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Get the moments
vector<Moments> mu(contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ mu[i] = moments( contours[i], false ); }
/// Get the mass centers:
vector<Point2f> mc( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }
/// Draw Contours
Mat drawing = Mat::zeros( edges.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, 2, 8, hierarchy, 0, Point() );
circle( drawing, mc[i], 4, color, -1, 8, 0 );
}
/// Show in separate window
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
/// Calculate the area with the moments 00 and compare with the result of the OpenCV function
printf("\t Info: Area and Contour Length \n");
for( int i = 0; i< contours.size(); i++ )
{
printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], true ) );
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
circle( drawing, mc[i], 4, color, -1, 8, 0 );
}
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
return(0);
}