C++ 无法解决opencv代码中的malloc错误
我的代码:C++ 无法解决opencv代码中的malloc错误,c++,opencv,C++,Opencv,我的代码: cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0)); std::cout << hough_acc.size( ) << " " << hough_acc[0].size() << std::endl; for (size_t i = 0; i < hough_acc.size(); i+
cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0));
std::cout << hough_acc.size( ) << " " << hough_acc[0].size() << std::endl;
for (size_t i = 0; i < hough_acc.size(); i++) {
for (size_t j = 0; j < hough_acc[0].size(); j++) {
std::cout << hough_acc[i][j] << std::endl;
img_mat.at<int> (i,j) = hough_acc[i][j];
}
}
我已经检查了Mat img_Mat
和hough_acc[][]
的大小,两者都相等。hough_acc
中的值也都是整数。我不明白为什么会有错误
完整代码:
void hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc) {
int img_w = img_a_edges.cols;
int img_h = img_a_edges.rows;
int max_votes = 0;
int min_votes = INT_MAX;
for (size_t r = 0; r < img_h; r++) {
for (size_t c = 0; c < img_w; c++) {
if(true) {
for (size_t angle = 0; angle < 180; angle++) {
double theta = (angle * M_PI / 180);
double rho = ( (c * cos(theta)) + (r * sin(theta)) );
int buff = ++hough_acc[static_cast<int>(abs(rho))][static_cast<int>(theta)];
if (buff > max_votes) {
max_votes = buff;
}
if (buff < min_votes) {
min_votes = buff;
}
}
}
}
}
double div = static_cast<double>(max_votes) / 255;
int threshold = 10;
int possible_edge = round(static_cast<double>(max_votes) / div) - threshold;
cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0));
std::cout << hough_acc.size( ) << " " << hough_acc[0].size() << std::endl;
for (size_t i = 0; i < hough_acc.size(); i++) {
for (size_t j = 0; j < hough_acc[0].size(); j++) {
std::cout << hough_acc[i][j] << std::endl;
img_mat.at<int> (i,j) = hough_acc[i][j];
}
}
imwrite("../output/ps1-2-b-1.png", img_mat);
}
void hough_line_acc(cv::Mat img_a_edges,std::vector&hough_acc){
int img_w=img_a_edges.cols;
int img_h=img_a_edges.rows;
int max_票数=0;
最小投票数=最大投票数;
对于(大小r=0;r
最大投票数){
最大投票数=buff;
}
如果(buff<最小投票数){
min_票数=buff;
}
}
}
}
}
双div=静态投票(最大投票数)/255;
int阈值=10;
int-mable_-edge=圆形(静态_-cast(最大投票数)/div)-阈值;
cv::Mat img_Mat=cv::Mat(hough_acc.size(),hough_acc[0].size(),cv_8UC1,cv::Scalar(0));
std::cout您得到的malloc错误可能是由于在可疑行之前写越界引起的。是否可能
int buff = ++hough_acc[static_cast<int>(abs(rho))][static_cast<int>(theta)];
int buff=++hough_acc[static_cast(abs(rho))][static_cast(theta)];
写在hough_acc的范围之外?
有些东西是“可疑的”,因为数组的大小是180,但你计算的角度是弧度,所以它应该(可能?)上升到6。看起来你有一个严重的系统错误…或者你的工具链坏了…也许你试图分配太多内存?矩阵的大小是362 x 180
。我想我没有这样做,但是有没有办法检查内存的总分配以及使用了多少内存?而且,如果我删除我提到的e行是可疑的。由于内存仍然被分配,我不确定是否是过度分配的原因。这应该只有261KB左右(假设一个整数的标准大小是4字节),不太多。img_mat
的数据类型是CV_8UC1
——即一个字节。然后你作为img_mat.at
访问它,将每个元素视为32位值。这是不正确的,它应该类似于img_mat.at
。不,我检查了rho的最大值,它是360,θ的最大值是硬编码的。数组ze是关于180大小的数组的362x180
,这是算法的细节,它的意思是这样的。我需要为180个不同的角度(以度为单位)运行代码,但正弦和余弦的值以弧度为单位,这就是为什么要进行转换。
int buff = ++hough_acc[static_cast<int>(abs(rho))][static_cast<int>(theta)];