What problem with my program? and how to solve it?

asked 2016-04-20 05:42:12 -0500

lol gravatar image

The mistake is vector subscript out of range , I don't know how to change it to right. please help me~thanks

void bo_lb(Mat &image, Mat & outputimage, int &count) {

int value = 0;

std::vector < vector<Point2i> > blob;
std::vector<Point2i>::iterator itr;

for (int y = 1; y < image.rows - 1; y++)
{

    for (int x = 1; x < image.cols - 1; x++)
    {
        value = Mpixel(image, x, y);

        if (value == 255) {
            int topvalue = Mpixel(image, x-1, y );
            int leftvalue = Mpixel(image, x , y-1);

            if (topvalue > 0 && leftvalue > 0)
            {
                if (topvalue == leftvalue) {
                    vector<Point2i> &set = blob[leftvalue];
                    set.push_back(Point2i(x, y));
                    value = leftvalue;

                }
                else
                {
                    vector<Point2i> &set2 = blob[leftvalue];
                    set2.push_back(Point(x, y));
                    value = leftvalue;
                    vector<Point2i> &set1 = blob[topvalue];
                    if (set1.size() != 0)
                    {
                        for (itr = set1.begin(); itr != set1.end(); itr++) {
                            value = leftvalue;

                        }
                        set2.insert(set2.end(), set1.begin(), set1.end());
                        set1.clear();
                    }
                }

            }
            else if (topvalue == 0 && leftvalue == 0)
            {
                vector<Point2i> vec;
                vec.push_back(Point2i(x, y));
                blob.push_back(vec);
                value = blob.size() - 1;



            }
            else if (topvalue == 0 && leftvalue > 0)
            {
                vector<Point2i> set = blob[leftvalue];


                    set.push_back(Point(x, y));
                    value = leftvalue;




            }
            else if (topvalue > 0 && leftvalue == 0) {
                vector<Point2i> &set = blob[topvalue];
                set.push_back(Point(x, y));
                value = topvalue;

            }

        }

    }
}

int cout = 0;
vector< vector<Point2i> >::iterator itr1;

for (itr1 = blob.begin(); itr1 != blob.end(); itr1++) {
    vector<Point2i> vec = *itr1;
    if (vec.size() == 0) {
        continue;
    }
    unsigned char r = rand() % 255;
    unsigned char g = rand() % 255;
    unsigned char b = rand() % 255;



    for (itr = vec.begin(); itr != vec.end(); itr1++) {
        int x = itr->x;
        int y = itr->y;

        CB(image, x, y) = b;
        CG(image, x, y) = g;
        CR(image, x, y) = r;
    }
    cout++;

}
count = cout;

}

edit retag flag offensive close merge delete

Comments

in which line do you get the exception ?

(don't leave us guessing..)

berak gravatar imageberak ( 2016-04-20 07:06:06 -0500 )edit