我有一个opencv图像缓冲区,我想传递给OpenALPR :: recognize()(它接受图像路径或内存中的图像),但是出现分段错误,我似乎找不到问题。我编写了一个演示该问题的演示应用程序:
//compilation: g++ -Wall test.cpp -lopenalpr -lopencv_core -lopencv_imgproc -lopencv_highgui -o test
#include <alpr.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <ios>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main (void)
{
std::string fname = "0000024867.jpg";
std::ifstream is;
std::ostringstream ss;
alpr::Alpr openalpr("us", "/etc/openalpr/openalpr.conf");
openalpr.setTopN(20);
if (openalpr.isLoaded() == false)
{
std::cerr << "Error loading OpenALPR" << std::endl;
return 1;
}
//is.open(fname.c_str(), std::ios::binary);
//ss << is.rdbuf();
const std::string& s = ss.str();
cv::Mat image = cv::imread(fname.c_str(), CV_LOAD_IMAGE_COLOR);
std::vector <unsigned char> buff;
std::vector <int>param = std::vector<int>(2);
param[0] = cv::IMWRITE_JPEG_QUALITY;
param[1] = 70;
imencode(".jpg",image,buff,param);
std::vector<char> vec(buff.begin(), buff.end());
alpr::AlprResults results = openalpr.recognize(vec);
// Carefully observe the results. There may be multiple plates in an image,
// and each plate returns the top N candidates.
for (unsigned int i = 0; i < results.plates.size(); i++) {
alpr::AlprPlateResult plate = results.plates[i];
std::cout << "plate" << i << ": " << plate.topNPlates.size() << " results" << std::endl;
for (unsigned int k = 0; k < plate.topNPlates.size(); k++) {
alpr::AlprPlate candidate = plate.topNPlates[k];
std::cout << " - " << candidate.characters << "\t confidence: " <<
candidate.overall_confidence << std::endl;
}
}
return 0;
}
可以从此处下载图像:0000024867.jpg
发生了什么:在alpr::AlprResults results = openalpr.recognize(vec);
行上抛出了段错误。
预期结果:.recognize()
函数执行没有错误,并且代码继续循环遍历图像中找到的LP。