Pixel-level access and common operations
One of the most fundamental operations in image processing is pixel-level access. Since an image is contained in the Mat
matrix type, there is a generic access form that uses the at<>
template function. In order to use it, we have to specify the type of matrix cells, for example:
Mat src1 = imread("stuff.jpg", CV_LOAD_IMAGE_GRAYSCALE); uchar pixel1=src1.at<uchar>(0,0); cout << "First pixel: " << (unsigned int)pixel1 << endl; Mat src2 = imread("stuff.jpg", CV_LOAD_IMAGE_COLOR); Vec3b pixel2 = src2.at<Vec3b>(0,0); cout << "First pixel (B):" << (unsigned int)pixel2[0] << endl; cout << "First pixel (G):" << (unsigned int)pixel2[1] << endl; cout << "First pixel (R):" << (unsigned int)pixel2[2] << endl;
Note that color images use the Vec3b
type, which is an array of three unsigned chars. Images with a fourth alpha (transparency) channel would be accessed using the type...