Ask Your Question

How to convert a colour image into text format

asked 2014-04-17 12:00:42 -0500

Hi folks,

I am lil new to this area,If I am @ right place then some one can help me regarding this,

My objective is to get a text with its colour information or a coloured text from a colured image.

please help me out or suggest some thing

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-04-18 04:20:24 -0500

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;
edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2014-04-17 12:00:42 -0500

Seen: 769 times

Last updated: Apr 18 '14