converting uchar to int then to string causes crash [closed]
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.
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
I've edited the questions according to your feedback. I'm using the following
So I don't think rows and cols could be out of bounds?
yes,
image.at<Vec3b>(rows, cols)
is out of boundsimage.at<Vec3b>(rows-1, cols-1)
is the maximum legit valuebut you want to use neither of those, but the values from your iteration, no ?
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.