Ask Your Question
0

Filling out elements of a cv::Mat manually

asked 2013-03-20 13:41:22 -0600

Hello everyone. I need to be able to set all the values of a matrix manually. I tried used the .at member of the Mat class but it always segfaults.

here is what I want to do

Mat test;
    test=Mat::zeros(Img.rows, Img.cols, CV_32FC2);


    for( int y = 0; y < Img.rows; y++ )
    {
      for( int x = 0; x < Img.cols; x++ )
      {
         if (some conditions are met)
          test[y][x][channel 1] = OtherMatrix[y][x];
         else
          test[y][x][channel 2] = OtherMatrix[y][x];              

      }

    }

Get it? depending on those conditions, I want to fill the different channels of test with values from another matrix already calculated.

Thanks in advance!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-03-21 04:05:42 -0600

Guanta gravatar image

The at-statement should work, e.g.:test.at<cv::Vec2f>[y][x][0] = 1.0 (or: test.at<cv::Vec2f>(y,x)[0] = 1.0).

Alternatively and much more convenient: instead of

cv::Mat test = cv::Mat::zeros(Img.rows, Img.cols, CV_32FC2);

use the templated Matrix-version, i.e.

cv::Mat2f test = cv::Mat2f::zeros(Img.rows, Img.cols);

then you don't need to use .at<> and the rest of your code should work as is.

edit flag offensive delete link more

Comments

Yes, I was using the wrong template !

Pedro Batista gravatar imagePedro Batista ( 2013-03-24 21:58:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-03-20 13:41:22 -0600

Seen: 19,667 times

Last updated: Mar 21 '13