3

我喜欢使用 5 x 5 矩阵制作一个 11 x 11 矩阵,如下所示。有没有比这更好的方法?

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
4

1 回答 1

1

这更短,所以从这个意义上说它更好:

   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);

这产生(这是我认为你想要的):

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
于 2013-04-19T14:04:53.483 回答