Ask Your Question
0

vector Mat crashes

asked 2016-06-28 08:46:53 -0600

Bekhouche gravatar image

Hello,

I did sample code which add each time Mat to a vector

header :

std::vector<cv::Mat> Histograms

sources

 // hist -> cv::Mat
// each time add new hist
    Histograms.push_back(hist);
    std::cout << hist << std::endl;

//
for(unsigned int i = 0; i < Histograms.size(); i++) {
    std::cout << Histograms[i].cols << std::endl;
    std::cout << Histograms[i] << std::endl;
}

the cout of hist work normaly, but the cout of each Histgrams crashed, the weird thing each time crash in different row?

example of output :

59
[517, 45, 85, 46, 46, 46, 78, 84, 81, 236, 221, 132, 182, 262, 706, 134, 79, 189, 292, 438, 267, 53, 59, 66, 184, 315, 161, 85, 34, 57, 47, 135, 68, 65, 49, 65, 38, 91, 238, 44, 46, 21, 148, 183, 115, 42, 53, 331, 108, 26, 9, 180, 58, 53, 126, 37, 175, 325, 1160]
59
[536, 108, 61, 79, 36, 26, 85, 64, 42, 301, 450, 127, 92, 101, 549, 114, 128, 116, 235, 226, 120, 26, 76, 95, 133, 166, 97, 77, 41, 94, 80, 143, 146, 68, 45, 33, 82, 131, 254, 99, 56, 40, 326, 318, 148, 60, 100, 336, 183, 52, 49, 120, 80, 86, 105, 59, 153, 337, 1126]
59
[454, 95, 59, 94, 73, 72, 97, 72, 65, 127, 259, 100, 87, 100, 350, 119, 67, 96, 244, 332, 119, 66, 83, 92, 139, 318, 158, 68, 39, 93, 122, 260, 386, 140, 41, 72, 106, 102, 287, 203, 70, 31, 146, 210, 174, 90, 81, 330, 133, 84, 57, 162, 70, 90, 107, 39, 129, 334, 1023]
59
[470, 68, 85, 73, 45, 86, 118, 75, 93, 423, 537, 119, 103, 163, 661, 265, 86, 71, 128, 174, 204, 56, 44, 67, 98, 155, 77, 64, 28, 59, 63, 137, 149, 152, 68, 40, 58, 85, 192, 82, 79, 43, 204, 314, 141, 63, 125, 469, 139, 53, 30, 156, 50, 44, 46, 38, 150, 299, 1052]
59
[396, 61, 66, 77, 42, 87, 94, 88, 81, 282, 356, 163, 136, 165, 595, 180, 85, 122, 258, 306, 195, 63, 31, 94, 158, 316, 145, 102, 36, 69, 58, 252, 200, 119, 64, 46, 57, 84, 198, 69, 47, 35, 258, 331, 100, 41, 80, 337, 180, 40, 24, 168, 79, 44, 98, 52, 151, 289, 866]
59
[302, 97, 38, 91, 14, 43, 168, 41, 50, 338, 627, 163, 123, 178, 672, 136, 58, 126, 222, 451, 136, 41, 10, 45, 140, 292, 128, 89, 45, 62, 92, 166, 544, 127, 43, 29, 48, 83, 162, 87, 73, 24, 167, 385, 114, 91, 111, 410, 111, 32, 28, 162, 17, 24, 111, 18, 192, 176, 663]
59
    The program has unexpectedly finished.
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-06-28 09:57:50 -0600

You should take care of the scope of hist object, if it is outside the first loop, then your will obtain errors since your Histograms vector will contains references to one hist object. It must be one local Mat object or you have to push_back its clone, not the reference to it (in this case, the name - hist). Below is the wrong code:

    vector<cv::Mat> hists;
    vector<string> files = { "1.pgm", "2.pgm", "4.pgm" };
    int histSize = 256;

    float range[] = { 0, 256 };
    const float* histRange = { range };

    bool uniform = true; bool accumulate = false;
    Mat img;
    Mat hist;
    for (int i = 0; i < files.size(); i++)
    {
        img = cv::imread(files[i], 0);
        cv::calcHist(&img, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);
        hists.push_back(hist);
    }
    for (int i = 0; i < files.size(); i++)
    {
        cout << hists[i].rows << endl;
        cout << hists[i].t() << endl;
    }

And the two versions hereafter are correct:

    Mat img;
    for (int i = 0; i < files.size(); i++)
    {
        Mat hist;
        img = cv::imread(files[i], 0);
        cv::calcHist(&img, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);
        hists.push_back(hist);
    }
    for (int i = 0; i < files.size(); i++)
    {
        cout << hists[i].rows << endl;
        cout << hists[i].t() << endl;
    }

Or:

    Mat img;
    Mat hist;
    for (int i = 0; i < files.size(); i++)
    {
        img = cv::imread(files[i], 0);
        cv::calcHist(&img, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);
        hists.push_back(hist.clone());
    }
    for (int i = 0; i < files.size(); i++)
    {
        cout << hists[i].rows << endl;
        cout << hists[i].t() << endl;
    }
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-06-28 08:46:53 -0600

Seen: 465 times

Last updated: Jun 28 '16