converting uchar to int then to string causes crash [closed]

asked 2015-11-07 00:49:27 -0600

carl82 gravatar image

updated 2015-11-07 01:57:04 -0600

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.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2015-11-07 03:48:33.348258

Comments

Vec3b intensity = image.at<Vec3b>(rows, cols); <-- i guess, you wanted (i,t) ?

(rows,cols) is out of bounds (and will give you UB).

also have a look here for formatting examples and here

berak gravatar imageberak ( 2015-11-07 01:37:07 -0600 )edit

I've edited the questions according to your feedback. I'm using the following

int rows = image.rows;
int cols = image.cols;

So I don't think rows and cols could be out of bounds?

carl82 gravatar imagecarl82 ( 2015-11-07 02:04:36 -0600 )edit

yes, image.at<Vec3b>(rows, cols)is out of bounds

image.at<Vec3b>(rows-1, cols-1) is the maximum legit value

but you want to use neither of those, but the values from your iteration, no ?

berak gravatar imageberak ( 2015-11-07 02:10:25 -0600 )edit

LOOOOOOLLLLLL

OMG I'm a retard. I'm thinking what is this guy talking about, rows and cols will always be rows-1 because of for loop. And I just noticed that I'm not using my loop variables i and t. And to think that I lost something like 5 hours to this is just... Anyway thanks a lot. I'll never program anything when sleep deprived. I'll be jumping off the nearest bridge now.

carl82 gravatar imagecarl82 ( 2015-11-07 03:34:09 -0600 )edit