matrix.at<int>(i,j) gives wrong output [closed]

asked 2018-03-27 11:55:25 -0500

waschbaer gravatar image

updated 2018-03-27 13:02:20 -0500

I do not know where I did wrong: The result is totally wrong.

int main(int argc, char *argv[])
{
    cv::Mat a(3,3,CV_32SC1);
    a.at<int>(2,2)=5;
    for(int i =0; i <a.rows; i++){
        for(int j =0; j <a.cols;j++){
            cout<<a.at<int>(i,j);
        }
    }
    return 0;
}

Solution:

I forgot to init value for each entry.

Init a 3 by 3 signed int Mat with zero:

cv::Mat a(3,3,CV_32SC1, Scalar(0));
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by LBerger
close date 2018-03-27 14:28:20.841586

Comments

wrong , as in ?

(3x3 Mat, 8 elements are not initialized , only 1 is. so what ?)

(besides, your lack of output formatting will make it hard to spot anything, like cout << anumber << " " would make it more transparent)

berak gravatar imageberak ( 2018-03-27 11:56:52 -0500 )edit

Puuhhh.. Yes, stupid questions.

waschbaer gravatar imagewaschbaer ( 2018-03-27 12:00:56 -0500 )edit
1

waschbaer, maybe it's just bad code, not a stupid question.

maybe you can solve it on your own, and even write an answer to it ? (highly welcome !)

(believe it or not, you're not alone with the problem , and some writeup would help future users !)

berak gravatar imageberak ( 2018-03-27 12:05:14 -0500 )edit
2

Thank you berak, you are the most friendly person I meet ever in the open source community.

waschbaer gravatar imagewaschbaer ( 2018-03-27 13:02:55 -0500 )edit