Bryanyan
Bryanyan

Reputation: 677

Making a big matrix using sub-matrices in OpenCV

I like to make a 11 x 11 matrix using 5 x 5 matrices as follow. Is there any way better than this?

int csz = 5;
Mat zz = Mat::zeros(csz, csz, CV_32FC1);
Mat oo = Mat::ones(csz, csz, CV_32FC1);
Mat hh = 0.5 * Mat::ones((csz*2 + 1), 1, CV_32FC1);//column matrix
cv::Mat chkpat1((csz * 2 + 1), (csz * 2 + 1), CV_32FC1);
chkpat1(Range(0, 5),Range(0, 5)) = zz;//first quadrant
chkpat1(Range(0, 5),Range(6, 11)) = oo;//second quadrant
chkpat1(Range(5, 11),Range(0, 5)) = oo;//third quadrant
chkpat1(Range(6, 11),Range(6, 11)) = oo;//fourth quadrant
chkpat1(Range(0, 11),Range(5, 6)) = hh;//middle column  
chkpat1(Range(5, 6),Range(0, 11)) = hh.t();//middle row

Upvotes: 3

Views: 213

Answers (1)

Bull
Bull

Reputation: 11941

This is shorter, so in that sense it is better:

   cv::Mat chkpat1(11, 11, CV_32FC1, cv::Scalar(1.0f));
   chkpat1(cv::Rect(0, 0, 5, 5)) = cv::Scalar(0.0f);
   chkpat1(cv::Rect(0, 5, 11, 1)) = cv::Scalar(0.5f);
   chkpat1(cv::Rect(5, 0, 1, 11)) = cv::Scalar(0.5f);

This produces (which is what I think you wanted):

0    0    0    0    0    0.5    1    1    1    1    1
0    0    0    0    0    0.5    1    1    1    1    1
0    0    0    0    0    0.5    1    1    1    1    1
0    0    0    0    0    0.5    1    1    1    1    1
0    0    0    0    0    0.5    1    1    1    1    1
0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5
1    1    1    1    1    0.5    1    1    1    1    1
1    1    1    1    1    0.5    1    1    1    1    1
1    1    1    1    1    0.5    1    1    1    1    1
1    1    1    1    1    0.5    1    1    1    1    1
1    1    1    1    1    0.5    1    1    1    1    1

Upvotes: 1

Related Questions