Ask Your Question

Revision history [back]

Well you need to know how images are stored in OpenCV. The library uses a BGR color channel creation and when you load in a standard image it gets stored in the 8 bit 3 channel data matrix format.

This means that if you have an image, you need to loop over each x and y position, read the value of each channel and perform an output of that data. The following code snippet does something that looks exactly like that. However, you still need to add the correct includes.

// Read in the data
Mat data = imread("data_location");

// Split the image in channels
// Remember Blue, Green, Red is the order used in OpenCV
vector<Mat> channels;
split(data, channels);

// Loop over the channels
int blue, green, red;
for(int rows = 0; rows < data.rows; rows ++){
   for(int cols = 0; cols < data.cols; cols++){
      blue = channels[0].at<uchar>(rows,cols);
      green = channels[1].at<uchar>(rows,cols);
      red = channels[2].at<uchar>(rows,cols);

      //Now you have all data to output a string of that location values
      cout << "Pixel [" << rows << "," << cols << "] has BGR values [" << blue << "," << green << "," << red << "]" << endl;
   }
}