sort of, but in java
correct c++ access would be either:
img.at<uchar>(y,x);
or, using a “typed Mat”:
Mat_<uchar> img = ...
img(y,x); // most elegant, imo
or even:
uchar *ptr = img.ptr<uchar>(0); // ptr to 1st element
...
ptr[y*w+x] = ...
however, you probably should avoid slow & error-prone per-pixel code in favour of nicely optimized vector functions, using “shifted ROIs”:
so, instead of:
Mat_<uchar> fI(z.size());
Mat_<uchar> img(z);
const int m=1;
for ( int r=m; r<z.rows-m; r++ )
{
for ( int c=m; c<z.cols-m; c++ )
{
uchar v = 0;
uchar cen = img(r,c);
v |= (img(r-1,c ) > cen) << 0;
v |= (img(r-1,c+1) > cen) << 1;
v |= (img(r ,c+1) > cen) << 2;
v |= (img(r+1,c+1) > cen) << 3;
v |= (img(r+1,c ) > cen) << 4;
v |= (img(r+1,c-1) > cen) << 5;
v |= (img(r ,c-1) > cen) << 6;
v |= (img(r-1,c-1) > cen) << 7;
fI(r,c) = v;
}
}
do:
#define SHIFTED_MATS_3x3 \
int M = I.rows; \
int N = I.cols; \
Mat I7 = I(Range(1,M-2), Range(1,N-2));\
Mat I6 = I(Range(1,M-2), Range(2,N-1));\
Mat I5 = I(Range(1,M-2), Range(3,N ));\
Mat I4 = I(Range(2,M-1), Range(3,N ));\
Mat I3 = I(Range(3,M ), Range(3,N ));\
Mat I2 = I(Range(3,M ), Range(2,N-1));\
Mat I1 = I(Range(3,M ), Range(1,N-2));\
Mat I0 = I(Range(2,M-1), Range(1,N-2));\
Mat Ic = I(Range(2,M-1), Range(2,N-1));\
SHIFTED_MATS_3x3;
Mat fI = ((I7>Ic)&128) |
((I6>Ic)&64) |
((I5>Ic)&32) |
((I4>Ic)&16) |
((I3>Ic)&8) |
((I2>Ic)&4) |
((I1>Ic)&2) |
((I0>Ic)&1);
(~3 times faster on my box !)