'Segmentation fault (core dumped) in image pyramid construction
I am looking at the source code of orb-slam2. I see that the source code of feature extraction written by the author contains a piece of code for building an image pyramid. I want to test and run it myself, but a Segmentation fault (core dumped) appears,The source code I tested is as follows:
#include "cmath"
#include "iostream"
#include <opencv2/core/types.hpp>
#include "opencv4/opencv2/imgproc.hpp"
#include "opencv4/opencv2/highgui.hpp"
#include "opencv4/opencv2/imgcodecs.hpp"
using namespace std;
using namespace cv;
const float scale_ = 1 / 1.2;
const int EDGE_THRESHOLD = 19;
void ComputePyramid(cv::Mat image);
std::vector<cv::Mat> mvImagePyramid;
int main(int argc, char** argv)
{
const char* filename = argc >=2 ? argv[1] : "../data/1.png";
Mat sourceImage = imread(samples::findFile(filename), COLOR_BGR2RGB);
if (sourceImage.empty()){
cout << "Error opening image " << endl;
cout << "Program Arguments: [image_name -- default ../data/1.png] " << endl;
return -1;
}
ComputePyramid(sourceImage);
namedWindow("source image", WINDOW_AUTOSIZE);
imshow("source image", sourceImage);
waitKey(0);
return 0;
}
void ComputePyramid(cv::Mat image)
{
for (int level = 0; level < 8; ++level)
{
float scale = scale_;
Size sz(cvRound((float)image.cols*scale), cvRound((float)image.rows*scale));
Size wholeSize(sz.width + EDGE_THRESHOLD*2, sz.height + EDGE_THRESHOLD*2);
Mat temp(wholeSize, image.type()), masktemp;
cout << "123" << endl;
mvImagePyramid[level] = temp(Rect(EDGE_THRESHOLD, EDGE_THRESHOLD, sz.width, sz.height));
cout << "456" << endl;
// Compute the resized image
if( level != 0 )
{
resize(mvImagePyramid[level-1], mvImagePyramid[level], sz, 0, 0, INTER_LINEAR);
copyMakeBorder(mvImagePyramid[level], temp, EDGE_THRESHOLD, EDGE_THRESHOLD, EDGE_THRESHOLD, EDGE_THRESHOLD,
BORDER_REFLECT_101+BORDER_ISOLATED);
}
else
{
copyMakeBorder(image, temp, EDGE_THRESHOLD, EDGE_THRESHOLD, EDGE_THRESHOLD, EDGE_THRESHOLD,
BORDER_REFLECT_101);
}
}
}
I added outputs 123 and 456 to void ComputePyramid(cv::Mat image) to judge whether the statement was executed successfully, but the result of its operation is as follows:
123
Segmentation fault (core dumped)
When I modify the statement below, it works fine:
mvImagePyramid[level] = temp(Rect(EDGE_THRESHOLD, EDGE_THRESHOLD, sz.width, sz.height));
[change into]-> temp(Rect(EDGE_THRESHOLD, EDGE_THRESHOLD, sz.width, sz.height));
I don't understand why this code works fine in orb-slam2 but not when I test it? I need your help, thanks!
Solution 1:[1]
It may be that your mvImagePyramid has an out-of-bounds issue. You can replace the [] operator with at() and try?
Solution 2:[2]
If you realize, after:
cout << "123" << endl;
is the first time you assign something to the vector mvImagePyramid. Initialize it using this before calling that function at main:
mvImagePyramid.resize(nlevels);
And it should work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | clove682 |
Solution 2 | careway |