0
IDE:码块
opencv的版本:2.4.6
语言:C++色彩的物体跟踪代码
以下是表示用于黄色的颜色检测的代码,其中我能够成功地执行。
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat GetThresholdedImage(Mat image_here)
{
Mat image_here1=image_here;
cvtColor(image_here,image_here1,CV_BGR2HSV);
inRange(image_here1, Scalar(20, 100, 100), Scalar(30, 255, 255), image_here1);
return image_here1;
}
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //width of frames of ideo
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //height of frames of the video
Mat imgtrack(dWidth,dHeight,CV_8UC3);
imgtrack.setTo(0);
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
//imshow("MyVideo", frame);
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat imgYellowThresh=GetThresholdedImage(frame);
imshow("MyVideo", imgYellowThresh);
cout<<endl<<"moving to imgproc";
cout<<"end";
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
以下是我执行跟踪黄色物体代码。问题是,它编译和运行成功,但最终的视频输出应显示在输出窗口不显示和程序突然结束没有任何错误或例外等 我已经看过不少其他代码和页面,但找不到解决方案。
我使用opencv教程来构建我的代码。由于这里提到的代码是C风格,所以我做了一些我自己的修改来构建一个C++代码。 http://opencv-srf.blogspot.in/2010/09/object-detection-using-color-seperation.html
我还添加了一堆cout的“开始thresholding”等检查问题。直到一个cout<<"\n area if starting \n";
的陈述正在被打印,但其余的不是。 此外,我得到warning: unknown escape sequence: '\040'
与声明cout"\n about to add\n";
行。 因为我是图像处理和opencv的新手,我不知道如何工作。
请帮忙。
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat imgtrack;
int lastX=-1;
int lastY=-1;
Mat GetThresholdedImage(Mat image_here)
{
Mat image_here1=image_here;
cvtColor(image_here,image_here1,CV_BGR2HSV);
inRange(image_here1, Scalar(20, 100, 100), Scalar(30, 255, 255), image_here1);
return image_here1;
}
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //width of frames of ideo
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //height of frames of the video
Mat imgtrack(dWidth,dHeight,CV_8UC3);
imgtrack.setTo(0);
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
//imshow("MyVideo", frame);
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
cout<<"\nstarting thresholding\n";
Mat imgYellowThresh=GetThresholdedImage(frame);
cout<<"\nDone Thresholding\n";
Moments m=moments(imgYellowThresh,true);
double moment10=m.m10;
double moment01=m.m01;
double area=m.m00;
cout<<"\n area if starting\n";
if(area>1000)
{
cout<<"\n inside if\n"; //not printing any cout from here on
int posX=moment10/area;
int posY=moment01/area;
if(lastX>=0 && lastY>=0 && posX>=0 && posY>=0)
{
line(imgtrack,Point(posX,posY),Point(lastX,lastY),Scalar(255,0,0),4);
}
lastX=posX;
lastY=posY;
imshow("MyVideo", frame);
add(frame,imgtrack,frame);
cout<<"end";
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
}
请帮助使用找到等高线函数,然后计算的最大轮廓的时刻,发现面积! ! –