将矢量复制到另一个矢量时出错

时间:2016-06-30 11:20:42

标签: c++ vector

有两个向量" ImgCoordinatesList" &安培; " ImgCoordinatesListCopy&#34 ;.

第一个向量连续接收被推回的值。(x,y,Type - 我之前已经定义了一个结构,它有int,int,string,它的对象是list_obj)

一旦功能" ImgCreation"如果被调用,第一个向量的内容应该被复制到" ImgCoordinatesListCopy"向量和此复制向量中的内容应用于进一步处理。

但是我所做的方法产生了错误。

void SliceImageNew::BuildImgCoordinatesList(const long X, const long Y, string Type)
{
    //Creates the Structure for one x,y,type coordinate
    list_obj.x = X;
    list_obj.y = Y;
    list_obj.Coordinates_Type = Type;

    //Pushes the structure into a vector which eventually forms a vector of structs
    ImgCoordinatesList.push_back(list_obj);
}

void SliceImageNew::ImgCreation()
{
    ImgCoordinatesListCopy.resize(ImgCoordinatesList.size());
    copy(ImgCoordinatesList.begin(), ImgCoordinatesList.end(), ImgCoordinatesListCopy.begin());
    //ImgCoordinatesListCopy = ImgCoordinatesList; //Copy the contents into another vector and create image with the copy vector

    ImgCoordinatesList.erase(ImgCoordinatesList.begin(), ImgCoordinatesList.end());//Clear the vector after copying the contents into another vector
    PlotImgCoordinates();
    //SaveSliceImg();
    //ClearImgCoordinatesList();

}

void SliceImageNew::PlotImgCoordinates()
{
    static int SliceImgCount = 1;
    Mat SliceImg(Size(1920, 1080), CV_16UC3); // Blank Image with White Background and 1920*1080 Dimensions
    for (int i = 1; i!=ImgCoordinatesListCopy.size(); i++)
    {
        //Color differentiation between Mark and Jump Lines
        if (ImgCoordinatesListCopy[i].Coordinates_Type == "Mark")
        {
            //cout << "This is a mark line" << endl;
            line(SliceImg, Point(ImgCoordinatesListCopy[i - 1].x, ImgCoordinatesListCopy[i - 1].y), Point(ImgCoordinatesListCopy[i].x, ImgCoordinatesListCopy[i].y), Scalar(255, 255, 155), 4, 2, 0);
        }
        else
        {
            //cout << "This is a jump line" << endl;
            line(SliceImg, Point(ImgCoordinatesListCopy[i - 1].x, ImgCoordinatesListCopy[i - 1].y), Point(ImgCoordinatesListCopy[i].x, ImgCoordinatesListCopy[i].y), Scalar(255, 100, 155), 4, 2, 0);
        }


    }
//Creating Legends for the Plot
    putText(SliceImg, "Mark Line", cvPoint(1600, 40),
        FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2);
    line(SliceImg, Point(1540, 35), Point(1590, 35), Scalar(255, 255, 155), 4, 2, 0);

    putText(SliceImg, "Jump Line", cvPoint(1600, 80),
        FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2);
    line(SliceImg, Point(1540, 75), Point(1590, 75), Scalar(255, 100, 155), 4, 2, 0);

    //Providing unique names for every picture that is being saved
    name << "Slice" << SliceImgCount << ".jpg";

    // Saving the image 
    imwrite(name.str(), SliceImg);
    SliceImgCount++; //Increment the count to provide unique names to the images
    waitKey(0);
}

我附上了一张图片,其中显示了代码和生成的错误image

在调试时,图像中突出显示的行产生了错误!

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

找到了解决方案!

我搞砸了&#34; SliceImageNew&#34;的对象声明。类。经过校正后,它可以正常工作并完美地绘制图像。