It depends on the type of image / mat. If the image is the usual 1 byte for each rgb per pixel, then this is one way to access it. This is quite effective, but not the safest way:
// The RGB values are stored in reverse order (i don't know why) struct RGB { unsigned char b, g, r; }; // Assumes 1 byte for r,g,b RGB& GetRGB(cv::Mat &mat, cv::Point p) { assert((mat.step/mat.cols) == sizeof(RGB)); RGB *data = (RGB*)mat.data; data += py * mat.cols + px; return *data; }
If Mat has a different type, you just need to change the RGB structure to fit what you need.
Try changing the type of image you are reading to a regular RGB image with this code:
Mat in, out; cvtColor(in, out, CV_8UC3);
source share