In opencv 2.2, I’d use the C++ interface.
cv::Mat in = /* your image goes here,
assuming single-channel image with 8bits per pixel */
for(int row = 0; row < in.rows; ++row) {
unsigned char* inp = in.ptr<unsigned char>(row);
for (int col = 0; col < in.cols; ++col) {
if (*inp++ == 0) {
std::cout << '1';
} else {
std::cout << '0';
}
std::cout << std::endl;
}
}