martinkersner / non-maximum-suppression-cpp

MIT License
40 stars 24 forks source link

fixed situation, when idx > resultVec.size() #3

Closed tischenkoalex closed 5 years ago

tischenkoalex commented 7 years ago

rectangle removal was failed because expression evaluated from left to right and it tried to get iterator out of range

martinkersner commented 7 years ago

Hi @lamantine

Could you please add example of bounding boxes on which it fails?

Martin

tischenkoalex commented 7 years ago

test.txt

Here is the example. It can be loaded with the following code:

        FileStorage fs("test.txt", FileStorage::READ);
        FileNode rects = fs["rects"];
        FileNodeIterator it = rects.begin(), it_end = rects.end();
        for (; it != it_end; ++it)
        {
            vector<float> tmp_rect;
            FileNode rect_node = *it;
            rect_node >> tmp_rect;
            rectangles.push_back(tmp_rect);
        }
tischenkoalex commented 7 years ago

But it can be easily demonstrated on another iterator:

    vector<string> test;

    test.push_back("0");
    test.push_back("1");
    test.push_back("2");
    test.push_back("3");
    test.push_back("4");

    int idx = 6;
    int offset = -1;
    vector<string>::iterator it = test.begin() + (idx + offset); // ok

    it = test.begin() + idx + offset; // fail
martinkersner commented 6 years ago

@lamantine I ran your example code and it works fine. The only thing is that I added was 6th element which you missed in your previous example.

#include <vector>
#include <string>
#include <iostream>

int main() {
    std::vector<std::string> test;

    test.push_back("0");
    test.push_back("1");
    test.push_back("2");
    test.push_back("3");
    test.push_back("4");
    test.push_back("5"); // addded, 6 + (-1) = 5

    int idx = 6;
    int offset = -1;
    std::vector<std::string>::iterator it = test.begin() + (idx + offset); // ok
    std::cout << *it << std::endl;

    it = test.begin() + idx + offset; // fail
    std::cout << *it << std::endl;

    return 0;
}

Compiled with g++ gives output

5
5