Ask Your Question
0

Filling out elements of a cv::Mat manually

asked Mar 20 '13

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!

Preview: (hide)

1 answer

Sort by » oldest newest most voted
3

answered Mar 21 '13

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.

Preview: (hide)

Comments

Yes, I was using the wrong template !

Pedro Batista gravatar imagePedro Batista (Mar 25 '13)edit

Question Tools

1 follower

Stats

Asked: Mar 20 '13

Seen: 19,822 times

Last updated: Mar 21 '13