Ask Your Question
0

How can I initialize Mat with integer value?

asked 2013-12-25 21:42:10 -0600

Nihad gravatar image

I tried to initialize Mat with some int values, but it yielded error. How can I initialize Mat with some integer values? Here is my code:

int main(...)
{
Mat image(3, 3, CV_8U);

for(int y=0;y<3;y++)
{
  for(int x=0;x<3;x++)
  {
    image.at<int>(y,x)=0;//error here

  }
}
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-12-26 03:21:18 -0600

Your Mat is of type CV_8U, so you should access it's elements using:

image.at<uchar>(y,x)=0

However, you can also create a matrix of all zeros using :

Mat Z = Mat::zeros(3,3, CV_8UC1);
edit flag offensive delete link more

Comments

1

... or a Mat with a value : Mat m(3,3, CV_8UC1, Scalar(23) );

berak gravatar imageberak ( 2013-12-26 03:35:25 -0600 )edit

Thanks for the comment @berak!

GilLevi gravatar imageGilLevi ( 2013-12-26 04:10:35 -0600 )edit

Thank you!

Nihad gravatar imageNihad ( 2013-12-26 04:31:59 -0600 )edit

Question Tools

Stats

Asked: 2013-12-25 21:42:10 -0600

Seen: 3,821 times

Last updated: Dec 26 '13