Ask Your Question
1

Succinct way to assign values to Mat

asked Feb 24 '16

updated Feb 24 '16

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]
Preview: (hide)

1 answer

Sort by » oldest newest most voted
2

answered Feb 24 '16

berak gravatar image

updated Feb 24 '16

"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;
Preview: (hide)

Comments

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

sturkmen gravatar imagesturkmen (Feb 24 '16)edit

ok. a combination of both

berak gravatar imageberak (Feb 24 '16)edit

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

sturkmen gravatar imagesturkmen (Feb 24 '16)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 (Feb 24 '16)edit

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

berak gravatar imageberak (Feb 24 '16)edit

Question Tools

1 follower

Stats

Asked: Feb 24 '16

Seen: 6,910 times

Last updated: Feb 24 '16