hehe, that’s actually, what filters do:
Mat ocv = Mat::zeros(10,10,CV_8U);
ocv(Rect(3,3,4,4)) = 3;
cout << ocv << endl;
blur(ocv,ocv,Size(3,3));
cout << ocv << endl;
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 3, 3, 3, 3, 0, 0, 0;
0, 0, 0, 3, 3, 3, 3, 0, 0, 0;
0, 0, 0, 3, 3, 3, 3, 0, 0, 0;
0, 0, 0, 3, 3, 3, 3, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 1, 1, 1, 1, 0, 0, 0;
0, 0, 1, 1, 2, 2, 1, 1, 0, 0;
0, 0, 1, 2, 3, 3, 2, 1, 0, 0;
0, 0, 1, 2, 3, 3, 2, 1, 0, 0;
0, 0, 1, 1, 2, 2, 1, 1, 0, 0;
0, 0, 0, 1, 1, 1, 1, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
(to get exactly your output above you’d probably have to dilate your square 3 pixels)
(and the blur() is just an example, there are a lot more builtin filters in opencv, you can even use your own kernels)