Ask Your Question
1

Succinct way to assign values to Mat

asked 2016-02-24 11:19:51 -0600

updated 2016-02-24 11:25:56 -0600

I want to assign values to a 4x3 Mat in C++. Right now, I'm doing it like this:

Mat P = Mat(Size(4, 3), CV_64F); //Create matrix with four corners of output image
P.at<double>(0,0)=0; P.at<double>(0,1)=inputImage.cols; P.at<double>(0,2)=inputImage.cols; P.at<double>(0,3)=0;
P.at<double>(1,0)=0; P.at<double>(1,1)=0; P.at<double>(1,2)=inputImage.rows; P.at<double>(1,3)=inputImage.rows;
P.at<double>(2,0)=1; P.at<double>(2,1)=1; P.at<double>(2,2)=1; P.at<double>(2,3)=1;

Is there a more succinct way like there is in other languages more like this?

P = [0, inputImage.cols, inputImage.cols, 0;
     0, 0, inputImage.rows, inputImage.rows;
     1, 1, 1, 1]
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-02-24 11:26:52 -0600

berak gravatar image

updated 2016-02-24 13:54:37 -0600

"Is there a more succinct way ?"

yes. use the template version of cv::Mat, it has overloaded ","and "<<" operators, which lets you write it like this::

cv::Mat_<double> P(3,4); // rows, cols
P << 0, inputImage.cols, inputImage.cols, 0,
     0, 0, inputImage.rows, inputImage.rows,
     1, 1, 1, 1;
edit flag offensive delete link more

Comments

@berak did you mean "it has an overloaded << operator" ?

sturkmen gravatar imagesturkmen ( 2016-02-24 12:18:18 -0600 )edit

ok. a combination of both

berak gravatar imageberak ( 2016-02-24 12:35:46 -0600 )edit

i don't know what "," is. let me try to learn :)

sturkmen gravatar imagesturkmen ( 2016-02-24 12:43:47 -0600 )edit
sturkmen gravatar imagesturkmen ( 2016-02-24 13:00:25 -0600 )edit

Beautiful. Thanks, @berak. There is a syntax error, though. The semi-colons at the end of the first two rows should be commas.

SSteve gravatar imageSSteve ( 2016-02-24 13:22:21 -0600 )edit

thanks, @SSteve, you're right. it should be commas all the way down to the last

berak gravatar imageberak ( 2016-02-24 13:53:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-24 11:19:51 -0600

Seen: 6,618 times

Last updated: Feb 24 '16