Opencv Rect只创建一个矩形

时间:2014-06-17 07:14:49

标签: c++ opencv particle-filter

在我尝试实现粒子滤镜时,我首先在我的图像(红色)中围绕汽车手动绘制了一个矩形(x,y,w,h),然后我拍了50个粒子,并给它们分配了噪音,即x = x +噪声(0,15),y = y +噪声(0,15)。

然后我想以绿色为每个粒子拉出所有矩形,但不是显示50个矩形,而是只显示一个矩形。

#include<opencv2\core\core.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\highgui\highgui.hpp>

#include<stdio.h>
#include<iostream>
#include<random>

using namespace cv;
using namespace std;



const int N = 50;// no of particles

string intToString(int number){

    //this function has a number input and string output
    std::stringstream ss;
    ss << number;
    return ss.str();
}



int main()
{


    Mat frame;
    frame = imread("f (1).png");
    namedWindow("Out");


    //locating the car manually
    Rect car(175, 210, 42, 31);


    //making a rectangle around the car
    rectangle(frame, car, Scalar(0, 0,255), 1, 8, 0);



    //getting tht height and width of the frame
    const int FRAME_HEIGHT = frame.rows;
    const int FRAME_WIDTH = frame.cols;


    //Particle filter initialization

    Mat Init = (Mat_<float>(4, 1) << car.x, car.y, 0, 0);


    //for a gaussian noise distribution
    std::default_random_engine generator;
    std::normal_distribution<double> distribution(0, 15);


    //Initializing the particles
    std::vector<cv::Mat> particle(N, cv::Mat(4, 1, CV_32F));


    cout << car.x << " " << car.y << "\n";

    for (int i = 0; i < N; i++)
    {
        particle[i].at<float>(0, 0) = Init.at<float>(0, 0) + distribution(generator);
        particle[i].at<float>(1, 0) = Init.at<float>(1, 0) + distribution(generator);
        particle[i].at<float>(2, 0) = 0.0;
        particle[i].at<float>(3, 0) = 0.0;
        cout << particle[i] << "\n";
    }

    for (int i = 0; i < N; i++)
    {
        int x = particle[i].at<float>(0, 0);
        int y = particle[i].at<float>(1, 0);

        rectangle(frame, Rect(x, y, 42, 31), Scalar(0, 255, 0), 1, 8, 0);
    }

    imshow("Out", frame);


    waitKey();





    return 0;
}

输出看起来像这样

enter image description here

粒子x y坐标位于下图中,第一个条目是手动硬编码的汽车,其余都是粒子坐标。

enter image description here

1 个答案:

答案 0 :(得分:4)

不幸的是,问题可能出在矢量初始化中。在该行:

 //Initializing the particles
    std::vector<cv::Mat> particle(N, cv::Mat(4, 1, CV_32F));

你要做的是创建N个对象,初始化为4的大小为float类型。所以这似乎没问题......等等! 如果向量中的所有Mat对象共享相同的矩阵数据怎么办?我们都知道Mat容器有标题和数据。当标题彼此分配或执行复制构造函数时,不会复制数据。就像我们的情况一样。究竟是什么让我们想象一下矢量构造函数可能如何工作:

template<class T>
vector::vector (size_type n, const value_type& val = value_type(),
                 const allocator_type& alloc = allocator_type()) {
    // how it can be written? Probably in the following way:
    this->resize(n); // allocates using allocator
    for(int i=0;i<n;i++)
         this->innerBuffer[i] = alloc(val); // runs copy constructor with argument val
                    // in our case, this is cv::Mat(const cv::Mat &)
}

如果您考虑复制cv :: Mat容器,仅复制标头。如果您想要克隆数据本身,则必须直接指定它,例如使用Mat :: clone()方法。并且向量构造函数肯定不使用clone()。因此,虽然标题不同,但未克隆数据且所有Mat对象共享相同的数据!

您可以轻松检查这是否属实(如果不是,我会感到惊讶):更改代码片段:

 for (int i = 0; i < N; i++)
    {
        particle[i].at<float>(0, 0) = Init.at<float>(0, 0) + distribution(generator);
        particle[i].at<float>(1, 0) = Init.at<float>(1, 0) + distribution(generator);
        particle[i].at<float>(2, 0) = 0.0;
        particle[i].at<float>(3, 0) = 0.0;
        cout << particle[i] << "\n"; // it is obvius that HERE particles are different
                 // beacuse in the next loop run you overwrite them!
    }

    for (int i = 0; i < N; i++)
    {
        int x = particle[i].at<float>(0, 0);
        int y = particle[i].at<float>(1, 0);

        rectangle(frame, Rect(x, y, 42, 31), Scalar(0, 255, 0), 1, 8, 0);
    }

 for (int i = 0; i < N; i++)
    {
        particle[i].at<float>(0, 0) = Init.at<float>(0, 0) + distribution(generator);
        particle[i].at<float>(1, 0) = Init.at<float>(1, 0) + distribution(generator);
        particle[i].at<float>(2, 0) = 0.0;
        particle[i].at<float>(3, 0) = 0.0;

    }

    for (int i = 0; i < N; i++)
    {
        cout << particle[i] << "\n";

        int x = particle[i].at<float>(0, 0);
        int y = particle[i].at<float>(1, 0);

        rectangle(frame, Rect(x, y, 42, 31), Scalar(0, 255, 0), 1, 8, 0);
    }

我希望所有粒子都是一样的。

参考:here

 vector(size_type __n, const value_type& __value,
00199          const allocator_type& __a = allocator_type())
00200       : _Base(__n, __a)
00201       { this->_M_impl._M_finish = std::uninitialized_fill_n(this->
00202                                 _M_impl._M_start,
00203                                 __n, __value); }
00204