Ask Your Question
0

stores cv::Mat into a std::vector

asked 2016-10-31 08:07:06 -0500

azdoud.y gravatar image

updated 2016-10-31 08:22:47 -0500

i'am trying to write many functions computing gradient with deferent kernels which return a vector of Mat object here one exemple :

std::vector<cv::Mat> histogram::SobelDirections(cv::Mat &frame){
    Mat vectMat;
    Mat Sx;
    cv::Sobel(frame, Sx, CV_32F, 1, 0, 3);

    Mat Sy;
    cv::Sobel(frame, Sy, CV_32F, 0, 1, 3);

    imshow("x", mat2gray(Sx));
    imshow("y", mat2gray(Sy));

    vectMat.push_back(Sx);
    vectMat.push_back(Sy);

    return vectMat;

}

i get this , any help would be appreciated

Program:  C Windows system321MSVCP110D.  dll 
File:  C:\Program Files icrosoft Visual Studio Include Vector Line:  1124
Expression: vector subscript out of range

here main code

    cv::Mat mat2gray(const cv::Mat& src)
    {
        Mat dst;
        cv::normalize(src, dst, 0.0, 255.0, cv::NORM_MINMAX, CV_8U);

        return dst;
    } 

    int main(int argc, char* argv[])
    {
        Mat image =Mat::zeros(Size(320, 240), CV_8UC1);
        circle(image, Point(160, 120), 80, Scalar(255, 255, 255), -1, CV_AA);
        imshow("x", mat2gray(SobelDirections(frame).at(0));
        imshow("y", mat2gray(SobelDirections(frame).at(1));
        return 0;
    }
edit retag flag offensive close merge delete

Comments

unlikely, that it happens in the code you show, maybe we need to see, where you use that vector

berak gravatar imageberak ( 2016-10-31 08:10:59 -0500 )edit
1

i will edit the question

azdoud.y gravatar imageazdoud.y ( 2016-10-31 08:13:56 -0500 )edit
1

sorry, it was in your function, missed it ;)

berak gravatar imageberak ( 2016-10-31 08:50:08 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-10-31 08:48:47 -0500

berak gravatar image

updated 2016-10-31 08:51:14 -0500

in your SobelDirections() it should not be:

Mat vectMat;

but :

vector<Mat> vectMat;

then, you should not call SobelDirections twice (it's quite expensive !)

std::vector<cv::Mat> sd = SobelDirections(image);
imshow("x", mat2gray(sd.at(0)));
imshow("y", mat2gray(sd.at(1)));
waitKey();
edit flag offensive delete link more

Comments

ooops ! Mat vectMat it was here thank you Sir #berak :D

azdoud.y gravatar imageazdoud.y ( 2016-10-31 09:54:12 -0500 )edit
2

but it's a nice trap, indeed (much to learn from). don't you think ?

so first, you appended 1 Mat to another horizontally (cv::Mat has a push_back(), too), then you returned that, and a std::vector with a single Mat was created, - think of it , it all even compliled & linked pretty well - just, when you run that, - it turns out, that the vector only has 1 elems, not 2.

if you got a beard, scratch it now ;)

berak gravatar imageberak ( 2016-10-31 10:00:16 -0500 )edit

hhh i will do, don't worry :D

azdoud.y gravatar imageazdoud.y ( 2016-10-31 10:05:42 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-10-31 08:07:06 -0500

Seen: 263 times

Last updated: Oct 31 '16