首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有色目标跟踪码

有色目标跟踪码
EN

Stack Overflow用户
提问于 2014-04-01 18:14:44
回答 1查看 1.7K关注 0票数 0

艾德: CodeBLocks

Opencv版本:2.4.6

语言: C++

下面是黄色检测的代码,我成功地执行了该代码。

代码语言:javascript
运行
复制
#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的“启动阈值”等来检查问题。一个cout<<"\n area if starting \n";上的语句正在打印,而其余的则没有打印。此外,我得到的warning: unknown escape sequence: '\040'与语句cout"\n about to add\n";在一行。因为我对图像处理和opencv都是新手,所以我想不出该怎么做。

请帮帮忙。

代码语言:javascript
运行
复制
#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;

}
}
EN

回答 1

Stack Overflow用户

发布于 2015-02-03 04:15:28

你试过:

  1. 降低面积数
  2. 使用查找轮廓函数,然后计算最大等高线上的矩,然后再找出面积。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22793681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档