Ask Your Question

iterate through a matrice, int to uchar problem

asked 2015-04-07 13:13:28 -0600

MalcolmMaya gravatar image

I'm trying to modify in a for a loop a Mat. I'm following this article. However the following code does not work :

cv::Mat tmp(20, 20, CV_32F, cv::Scalar(0,0,0));

cv::MatIterator_<uchar> new_mat= tmp.begin<uchar>();
for( int i = 0; i< (tmp.size().width) ;i++){
    for(int j= 0;j< tmp.size().height;j++){

    (*new_mat) = 1;
    std::cout << (float) (*new_mat) << ", ";
std::cout <<std::endl;
std::cout << std::endl << std::endl;

std::cout << tmp << std::endl;

std::string win = "Local Maxima";
cv::imshow(win, tmp);

Printing (float) (*new_mat) give a Matrix of 1 as explected but tmp if full of 1.4012985e-45 values and I can't seem to understand why. My guess is that it as to do with the way I'm assigning the value but I can't grasp the real problem here.

Any help is appreciated.

edit retag flag offensive close merge delete



you have to be strict/consistent with your mat type.

cv::Mat tmp(20, 20, CV_32F, cv::Scalar(0,0,0)); <-- so, this is a float Mat, so you need:

cv::MatIterator_<float> new_mat= tmp.begin<float>();

berak gravatar imageberak ( 2015-04-07 13:28:32 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-04-07 15:09:41 -0600

MalcolmMaya gravatar image

Arf, silly me, you're right ! Thanks a lot =)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-04-07 13:13:28 -0600

Seen: 241 times

Last updated: Apr 07 '15