Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV绘制圆以跟踪球问题_C++_Opencv - Fatal编程技术网

C++ OpenCV绘制圆以跟踪球问题

C++ OpenCV绘制圆以跟踪球问题,c++,opencv,C++,Opencv,我正在自学OpenCV,今天我编写了以下代码来跟踪一个球在我的计算机网络摄像头上滚动,并(尝试)在它的质心上画一个填充的灰色圆圈: #include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; Point getBlobCentroid(Mat blobImage); int main() { Mat bGround, fGround, d

我正在自学OpenCV,今天我编写了以下代码来跟踪一个球在我的计算机网络摄像头上滚动,并(尝试)在它的质心上画一个填充的灰色圆圈:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

Point getBlobCentroid(Mat blobImage);

int main()
{
    Mat bGround, fGround, diff;
    Point p = (500, 280);
    VideoCapture cap(0);

    while (true)
    {
        cap >> fGround; //assign frame from camera to newest image
        cvtColor(fGround, fGround, CV_BGR2GRAY); //convert to grayscale

        bGround.create(fGround.size(), fGround.type()); 
        absdiff(bGround, fGround, diff); //subtract current frame from old frame

        threshold(diff, diff, 50, 255, CV_THRESH_BINARY); //convert to binary
        erode(diff, diff, NULL, Point(-1,-1), 3, 0, BORDER_DEFAULT);

        imshow("Thresholded", diff);

        circle(fGround, getBlobCentroid(diff), 6, 127, -1, 8, 16);
        imshow("Natural Image with Tracking", fGround);

        fGround.copyTo(bGround); //move forward in time
        waitKey(1);
    }

    return 0;
}

Point getBlobCentroid(Mat blobImage)
{
    int rowSum=0, colSum=0, count = 1;

    for(int i=0; i<blobImage.rows; i++)
    {
        for (int j=0; j<blobImage.cols; j++)
        {
            if (blobImage.at<uchar>(i,j) == 255)
            {
                rowSum+=i;
                colSum+=j;
                count++;
            }
        }
    }
    Point centroid = (rowSum, colSum)/count;
    return centroid;
}
#包括
#包括
使用名称空间cv;
使用名称空间std;
Point getBlobCentroid(Mat blobImage);
int main()
{
Mat bGround、fGround、diff;
p点=(500280);
视频捕获上限(0);
while(true)
{
cap>>fGround;//将相机的帧指定给最新图像
cvtColor(fGround、fGround、CV_BGR2GRAY);//转换为灰度
创建(fGround.size(),fGround.type());
absdiff(bGround,fGround,diff);//从旧帧中减去当前帧
阈值(diff,diff,50255,CV_THRESH_BINARY);//转换为二进制
侵蚀(差异,差异,空,点(-1,-1),3,0,边界_默认值);
imshow(“阈值化”,差异);
圆(fGround,getBlobCentroid(diff),6127,-1,8,16);
imshow(“带跟踪的自然图像”,fGround);
fGround.copyTo(bGround);//及时向前移动
等待键(1);
}
返回0;
}
Point getBlobCentroid(Mat blobImage)
{
int rowSum=0,colSum=0,count=1;

对于(int i=0;我不确定这是问题所在,因为算法工作正常(至少看起来是这样)-我记得我读到Mat::create()只覆盖数据,如果数据不相同的话-一旦我第一次将fGround复制到bGround,是否会创建()语句做什么了?无论如何,bGround必须是相同的大小和类型,或者不能在它们上使用absdiff。有更好的方法吗?不确定这是问题所在,因为算法工作正常(至少看起来是这样)-我想我记得读过Mat::create()只有在数据不相同的情况下才重写数据,这样-一旦我第一次将fGround复制到bGround,create()语句还会做什么吗?无论如何,bGround必须具有相同的大小和类型,或者不能对它们使用absdiff。有更好的方法吗?
fGround.copyTo(bGround); //move forward in time
// so, that's your idea. compare bg & fg, get the centroid of the diff.
// but then, if you follow your while loop there, (waitkey, back to the top ... )

bGround.create(fGround.size(), fGround.type()); 
// aww, so you're never using, what you copied before

absdiff(bGround, fGround, diff); 
// so, in the end, you're always comparing fGround to an empty bGround img