To process images, we have to know how to access each pixel independently. OpenCV provides a number of ways to do this. In this section, we cover two methods; the first one is easy for the programmer, while the second one is more efficient.
The first method uses the
at<> template function. In order to use it, we have to specify the type of matrix cells, such as in this short example:
Mat src1 = imread("lena.jpg", IMREAD_GRAYSCALE); uchar pixel1=src1.at<uchar>(0,0); cout << "Value of pixel (0,0): " << (unsigned int)pixel1 << endl; Mat src2 = imread("lena.jpg", IMREAD_COLOR); Vec3b pixel2 = src2.at<Vec3b>(0,0); cout << "B component of pixel (0,0):" << (unsigned int)pixel2 << endl;
The example reads an image in both grayscale and ...