Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

converting uchar to int then to string causes crash

I asked this question at stackoverflow but didn't get an answer.

I'm trying to write pixel values of an image in RGB to a file with Opencv. Basically file will be made of values like

R-G-B

125-12-54

4-47-203

There is nothing wrong with the image, display program shows it without any problems. Here's the relevant portion of the code.

  Mat rawData = Mat(1, elementcount, CV_8UC1, UArray);
image = imdecode(rawData, IMREAD_COLOR);
...
    for (int i = 0; i < rows; i++){
        for (int t = 0; t < cols; t++){
            Vec3b intensity = image.at<Vec3b>(rows, cols);
            ImageValueToString(intensity);

I've tried the following but everytime the program crashes

void ImageValueToString(Vec3b imagevalue){
   int blue = imagevalue.val[0];
   string blue_string = to_string(blue); // Crash

void ImageValueToString(Vec3b imagevalue){
   int blue = static_cast<int>(imagevalue.val[0]);
   string blue_string = to_string(blue); // Crash

void ImageValueToString(Vec3b imagevalue){
   uchar blue = imagevalue.val[0];
   int blue_int = (int)blue;
   string blue_string = to_string(blue_int); // Crash

 void ImageValueToString(Vec3b imagevalue){
   int blue = imagevalue.val[0];
   string s;
   stringstream out;
   out << blue;
   s = out.str(); // crash

The program works if I omit string conversion. This works

 int red = 5;
  string red_string = to_string(red);

But when converted from uchar, int to string conversion doesn't work. I'm baffled. Any suggestions? I must be missing something trival.

converting uchar to int then to string causes crash

I asked this question at stackoverflow but didn't get an answer.

I'm working with the following: Visual Studio 2013, VS compiler, OpenCV 3

I'm trying to write pixel values of an image in RGB to a file with Opencv. Basically file will be made of values like

R-G-B

125-12-54

4-47-203

There is nothing wrong with the image, display program shows it without any problems. Here's the relevant portion of the code.

  Mat rawData = Mat(1, elementcount, CV_8UC1, UArray);
 image = imdecode(rawData, IMREAD_COLOR);
  int rows = image.rows;
  int cols = image.cols;
   ...
    for (int i = 0; i < rows; i++){
        for (int t = 0; t < cols; t++){
            Vec3b intensity = image.at<Vec3b>(rows, cols);
            ImageValueToString(intensity);

I've tried the following but everytime the program crashes

void ImageValueToString(Vec3b imagevalue){
   int blue = imagevalue.val[0];
   string blue_string = to_string(blue); // Crash

void ImageValueToString(Vec3b imagevalue){
   int blue = static_cast<int>(imagevalue.val[0]);
   string blue_string = to_string(blue); // Crash

void ImageValueToString(Vec3b imagevalue){
   uchar blue = imagevalue.val[0];
   int blue_int = (int)blue;
   string blue_string = to_string(blue_int); // Crash

 void ImageValueToString(Vec3b imagevalue){
   int blue = imagevalue.val[0];
   string s;
   stringstream out;
   out << blue;
   s = out.str(); // crash

The program works if I omit string conversion. This works

 int red = 5;
  string red_string = to_string(red);

But when converted from uchar, int to string conversion doesn't work. I'm baffled. Any suggestions? I must be missing something trival.