执行
时出现分段错误cv::Mat hist=cv::Mat(1,no_patterns,CV_32SC1,cv::Scalar(0));
no_patterns = 256
然而,它不会在第一次执行函数时崩溃,而是在第二次执行时崩溃。
如果它具有任何重要性,则getHistFromLBP和histAVG在其类中声明为静态。
背景:
getHistFromLBP函数
cv::Mat LBP::getHistFromLBP(const cv::Mat& lbp_mat,unsigned no_patterns, bool normed)
{
// It crashes here
cv::Mat hist=cv::Mat(1,no_patterns,CV_32SC1,cv::Scalar(0));
for (unsigned i=0;i<no_patterns;i++)
hist.at<int32_t>(1,i)=0;
for (int i=0;i<lbp_mat.rows;i++)
for (int j=0;j<lbp_mat.cols;j++)
{
uint32_t bin;
switch (lbp_mat.type())
{
case CV_8UC1: bin=static_cast<uint32_t>(lbp_mat.at<uint8_t>(i,j)); break;
case CV_16UC1: bin=static_cast<uint32_t>(lbp_mat.at<uint16_t>(i,j)); break;
case CV_32SC1: bin=static_cast<uint32_t>(lbp_mat.at<int32_t>(i,j)); break;
}
hist.at<uint32_t>(1,bin)+=1;
}
if (normed)
{
unsigned long sum=0;
for (int i=0;i<hist.cols;i++)
{
sum += hist.at<uint32_t>(1, i);
}
std::cout << "Found sum = " << sum << std::endl;
for (int i=0;i<hist.cols;i++)
{
std::cout<< "Working on normalizing i= " << i << std::endl;
std::cout << "Hist(i) = " << hist.at<uint32_t>(1,i) << std::endl;
hist.at<float>(i) = hist.at<uint32_t>(1, i) * 1.0 / (sum + std::numeric_limits<float>::epsilon());
}
}
return hist;
}
调用检索直方图的函数的函数。
if (sliding_window_frames.window_full())
{
// We can calculate the AFF hist and the CF-AFF Chi Square distance
std::vector<cv::Mat>* cf_mat=sliding_window_frames.getCFMat();
std::vector<cv::Mat>* front_mat=sliding_window_frames.getFrontMat();
std::vector<cv::Mat>* back_mat=sliding_window_frames.getBackMat();
std::vector<double> tmp_diffs;
for (unsigned i=0;i<cf_mat->size();i++)
{
std::cout << "Current block analyzed is: " << i << std::endl;
// This call of getHistFromLBP executes OK
cv::Mat histCF=lbp_extractor->getHistFromLBP((*cf_mat)[i],256,normed_histograms);
// This call fails with a segmentation fault
cv::Mat histFront=lbp_extractor->getHistFromLBP((*front_mat)[i],256,normed_histograms);
cv::Mat histBack=lbp_extractor->getHistFromLBP((*back_mat)[i],256,normed_histograms);
std::cout << "Found Histograms " << std::endl;
cv::Mat histAFF=lbp_extractor->histAvg(histFront,histBack,normed_histograms);
std::cout << "Found Hist AFF " << std::endl;
double chisquared=lbp_extractor->getChiSquaredDistanceFromHist(histCF,histAFF,normed_histograms);
std::cout << "Found chi squared " << std::endl;
tmp_diffs.push_back(chisquared);
std::cout << "Pushed chi squared " << std::endl;
}
...
}
以下是分段故障时的GDB回溯:
#0 0x00007fffefc1bfff in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007fffefc1d42a in __GI_abort () at abort.c:89
#2 0x00007fffefc59c00 in __libc_message (do_abort=do_abort@entry=2, fmt=fmt@entry=0x7fffefd4ed78 "*** Error in `%s': %s: 0x%s ***\n")
at ../sysdeps/posix/libc_fatal.c:175
#3 0x00007fffefc5ffc6 in malloc_printerr (action=3, str=0x7fffefd4b8be "corrupted size vs. prev_size", ptr=<optimized out>, ar_ptr=<optimized out>)
at malloc.c:5049
#4 0x00007fffefc603b8 in malloc_consolidate (av=av@entry=0x7fffeff80b00 <main_arena>) at malloc.c:4221
#5 0x00007fffefc61dfa in _int_malloc (av=av@entry=0x7fffeff80b00 <main_arena>, bytes=bytes@entry=1136) at malloc.c:3488
#6 0x00007fffefc6293e in _int_memalign (av=av@entry=0x7fffeff80b00 <main_arena>, alignment=alignment@entry=64, bytes=bytes@entry=1024)
at malloc.c:4459
#7 0x00007fffefc6435f in _mid_memalign (alignment=64, bytes=1024, address=<optimized out>) at malloc.c:3158
#8 0x00007fffefc65fef in __posix_memalign (memptr=0x7fffffffbd38, alignment=<optimized out>, size=<optimized out>) at malloc.c:5071
#9 0x00007ffff57e967b in cv::fastMalloc(unsigned long) () at /usr/local/lib/libopencv_core.so.3.3
#10 0x00007ffff5930ae6 in cv::Mat::create(int, int const*, int) () at /usr/local/lib/libopencv_core.so.3.3
#11 0x000055555556995e in cv::Mat::create(int, int, int) (this=0x7fffffffbff0, _rows=1, _cols=256, _type=4)
at /usr/local/include/opencv2/core/mat.inl.hpp:784
#12 0x00005555555698b5 in cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&) (this=0x7fffffffbff0, _rows=1, _cols=256, _type=4, _s=...)
at /usr/local/include/opencv2/core/mat.inl.hpp:420
#13 0x0000555555568ccf in LBP::getHistFromLBP(cv::Mat const&, unsigned int, bool) (lbp_mat=..., no_patterns=256, normed=true) at ../src/lbp.cpp:162
#14 0x0000555555572760 in RM_Analyzer::addNewFrame(cv::Mat, bool) (this=0x7fffffffc490, img=..., is_lbp_img=false) at ../src/rm_analyzer.cpp:86
#15 0x00005555555721ff in RM_Analyzer::analyzeVid() (this=0x7fffffffc490) at ../src/rm_analyzer.cpp:25
#16 0x0000555555577241 in main(int, char const**) (argc=15, argv=0x7fffffffe0b8) at ../bin_src/main.cpp:198
Makefile中的链接库:
LIBSLANDMARK = -lFaceAnalyser -lLandmarkDetector -ltbb -ldlib -lboost_filesystem -lboost_system
LIBSOPENCV = -lopencv_calib3d -lopencv_core -lopencv_videoio -lopencv_objdetect -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs
使用的OpenCV版本:3.3(我写了一个c ++文件写了主要和次要版本,因为我不确定它是3.2还是3.3)