First, you should access its pixels with type uchar
(unsigned char
) instead of int
.
And when cout
, you must explicitly convert to other types like int
in order to print it correctly because cout
will work weird when printing unsigned char
when its value is larger than 127
. Otherwise, it will print some garbage value (or nothing) as it is just a non-printable ASCII character which is getting printed anyway. Check out Why "cout" works weird for "unsigned char"?.
cout << setw(10) << (int) img.at<uchar>(i,j);
^^^^^ ^^^^^