Ask Your Question
0

question of using iterator

asked 2018-03-08 08:21:37 -0600

Weifa Gan gravatar image

updated 2018-03-08 08:24:20 -0600

berak gravatar image

I want to set the iterator to the specified element of the matrix, but the output is nothing or messy code. The codes are following. pleace help me to understand.Thank you!

#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;
int main()
{

    Point2i pi(0,0);
    Mat n=(Mat_<float>(2,2) << 1.0f, 2.0f, 3.0f, 4.0f);
    Mat* m=& n;
    MatConstIterator ito(m, pi);
    cout <<(*ito);
    system("pause");

}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-08 08:33:33 -0600

berak gravatar image

updated 2018-03-08 08:46:02 -0600

MatConstIterator (without any template brackets) defaults to MatConstIterator_<uchar>

so, that's the wrong type. try like:

Point2i pi(0,0);
Mat_<float> m(2,2);
m << 1.0f, 2.0f, 3.0f, 4.0f;

MatConstIterator_<float> ito(&m, pi);
cout <<(*ito) << endl;

in the end, maybe you should avoid writing code like this. anything "per-pixel" is slow and error prone.

p.s, if you ever need to print out uchar values, do it like this:

cout << int(value) << endl;

else it tries to print ascii (garbage)

edit flag offensive delete link more

Comments

Thanks! But it still have some problem,reminding that the constructors do not match the parameter list.

Does MatConstIterator_<float> ito(&m, pi) match MatConstIterator_(const Mat_<_Tp>* _m, Point _pt) ?

Weifa Gan gravatar imageWeifa Gan ( 2018-03-09 00:32:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-08 08:21:37 -0600

Seen: 172 times

Last updated: Mar 08 '18