Reputation: 31
I am new to C++ programming and I meet this problem in one part of my code, where the error is sometimes memory allocation type error and sometimes it is double free error.
Erroneous code is as below.
cv::Mat obstacles = cv::Mat::ones(output.size(), CV_8UC1);
for (int r=0; r<obstacles.rows; ++r) {
int x_cord = cvRound((r - intercet)/slope);
if (x_cord >= 0 && x_cord <= disp_size){
for (int c=0; c<obstacles.cols; ++c) {
int d = output.at<int>(r,c);
if ((d/(256/disp_size)) <= x_cord+5){//<= x_cord+5 && d>= x_cord-5){
obstacles.at<int>(r,c) = 0; //error is in this line
}
}
}
}
If I remove obstacles.at<int>(r,c) = 0;
line, there will not be any errors.
I do not understand this, because r
and c
are just the obstacles
matrix row and column numbers respectively.
Any help in this regard is highly appreciated.
Upvotes: 2
Views: 105
Reputation: 20160
Your Mat has type CV_8UC1
which is a 8 Bit = 1 Byte data type.
You try to access as .at<int>
but int is a 32 Bit data type.
Please try to use unsigned char or other 8 bit type as shown below:
cv::Mat obstacles = cv::Mat::ones(output.size(), CV_8UC1);
for (int r=0; r<obstacles.rows; ++r) {
int x_cord = cvRound((r - intercet)/slope);
if (x_cord >= 0 && x_cord <= disp_size){
for (int c=0; c<obstacles.cols; ++c) {
int d = output.at<uchar>(r,c);
if ((d/(256/disp_size)) <= x_cord+5){//<= x_cord+5 && d>= x_cord-5){
obstacles.at<uchar>(r,c) = 0; //error is in this line
}
}
}
}
Upvotes: 1